Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get schema was not parsing statements with autoincrement #430

Merged
merged 1 commit into from
Jan 16, 2023

Conversation

brunocalza
Copy link
Collaborator

@brunocalza brunocalza commented Jan 3, 2023

Summary

When getting the schema of a table we need to parse the statement that is stored in SQLite. Sometimes that statement may come with an AUTOINCREMENT (because of the protocol rules that add the AUTOINCREMENT keyword automatically when an INTEGER PRIMARY KEY is found). However, the AUTOINCREMENT keyword is not parsable. So we're getting an error.

Context

Closes #428

Implementation overview

Simply remove the AUTOINCREMENT before parsing.

I don't think removing keywords that are not parsable such as STRICT and AUTOINCREMENT is a good long-term approach. But this is good enough to solve the bug.

This is also related to tablelandnetwork/go-sqlparser#41. It is kind of weird that the String method produces and string that is not compliant with the Tableland SQL. But that's more a matter of definition. Up until now, we've been using the String method as a way to get the SQL statement that will be executed in SQLite. I'll leave this discussion for the future.

@brunocalza brunocalza added the bug Something isn't working label Jan 12, 2023
@brunocalza brunocalza self-assigned this Jan 12, 2023
@brunocalza brunocalza marked this pull request as ready for review January 16, 2023 13:59
@brunocalza brunocalza requested a review from jsign as a code owner January 16, 2023 13:59
Comment on lines +249 to +251
if strings.Contains(strings.ToLower(createStmt), "autoincrement") {
createStmt = strings.Replace(createStmt, "autoincrement", "", -1)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm, OK, looks fine, but I'm a bit worried we can replace any other stuff with autoincrement that shouldn't be replaced. For example, if a column name is autoincrement, something bad would happen.

Of course, this is a very weird border case, but still possible.

I can suggest two options:
1- Simply accept that risk, since it would be very weird if this happened; and wait for a better overall solution, probably related to the other chat about generating "spec compatible" strings.
2- We can do a similar strategy as with strict, looking for a more concrete position to replace. For example, maybe look for primary key autoincrement (not sure if that would be correct, but you get the idea), and only replace it there. That will probably get rid of this problem while still being a simple-ish solution.

I'm fine with either.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I just checked and we're not allowing AUTOINCREMENT as a column name (and in any other place), even if we enclose it with brackets or similar. So we should be safe.

@brunocalza brunocalza merged commit 46bcf9d into jsign/testnetmainnetsplit Jan 16, 2023
@brunocalza brunocalza deleted the bcalza/fixgetschema branch January 16, 2023 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants