Skip to content

mysql alter add inside of schema file fails #251

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

Closed
jmhodges opened this issue Jan 10, 2020 · 6 comments
Closed

mysql alter add inside of schema file fails #251

jmhodges opened this issue Jan 10, 2020 · 6 comments

Comments

@jmhodges
Copy link
Contributor

jmhodges commented Jan 10, 2020

Say a schema file contains

alter table people add column is_cool tinyint;
update people set is_cool = 0 where 1 = 1;

sqlc will error out with:

Failed to parse UPDATE query: Failed to determine type of a parameter's column: Column [is_cool] not found in table [people]

Since update commands (and similar) don't actually change the schema, we could get away with dropping them completely. But perhaps all we're missing is updating the right bits of memory between query parses?

(An unedited version of this was found in a codebase that's in production.)

@jmhodges jmhodges changed the title mysql update inside of schema queries fails mysql update inside of schema file fails Jan 10, 2020
@jmhodges
Copy link
Contributor Author

jmhodges commented Jan 10, 2020

(The third line was

alter table subscriptions modify include_subdomains tinyint not null;

in case you're wondering why this wasn't a not null situation.)

@cmoog
Copy link
Contributor

cmoog commented Jan 12, 2020

The problem here isn't the update statement but the fact that we currently don't parse the alter statement. I can't seem to find where sqlparser holds information for alter queries...

Am I missing something obvious here?? Looks like the only things it parses are the Action and TableName.

@jmhodges
Copy link
Contributor Author

Yeah, this def seems to be a missing feature in sqlparser itself.

This section of their current sql.y is useful to understand:

https://github.com/vitessio/vitess/blob/3670303cca67e6833393a80667b45491b866b0d1/go/vt/sqlparser/sql.y#L1291-L1294

Those lines drop everything after the ADD part of an ALTER sometable ADD statement, and create a sqlparser.DDL with just the Action string set to ALTER and the Table string set to the table_name it parses out.

That file, at minimum, needs to be patched to parse out the types at set them on that sqlparser.DDL like similar files do.

@jmhodges jmhodges changed the title mysql update inside of schema file fails mysql alter add inside of schema file fails Jan 13, 2020
@jmhodges
Copy link
Contributor Author

jmhodges commented Jan 13, 2020

I've changed the title of this issue. (I was hesitant about changing the summary. @kyleconroy would you prefer a new ticket?)

The https://github.com/pingcap/parser mentioned by another user in #150 seems to support this, but I'm not sure if we should switch. I'm not sure what the decision making was on our selection here (and don't have a horse in this race).

@clintberry you mentioned vitess being interested in sqlc. How amenable would vitess be to patches for fuller ALTER support (and are there already some in progress)?

@jmhodges
Copy link
Contributor Author

Okay, I heard from Andrés Taylor that vitess is down for us to add more ALTER support to sqlparser. He says they're consciously trying to increase the SQL they support and, if we get stuck, that he's up to help us out.

I've made a tracking ticket in their repo vitessio/vitess#5705

@kyleconroy
Copy link
Collaborator

kyleconroy commented Aug 25, 2020

The new mysql:beta engine parses this code correctly and does not error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants