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

MySQL: Add ANSI_QUOTES support to MigrationsTableExists() #475

Merged
merged 1 commit into from
Nov 15, 2023

Conversation

toru
Copy link
Contributor

@toru toru commented Sep 14, 2023

This PR addresses #444

Problem

Dbmate currently fails to function against MySQL instances enabled with the ANSI_QUOTES sql_mode.

My initial investigation report can be found here.

Why

The ANSI_QUOTES mode does not allow double-quote characters to be used for expressing string literals. Therefore ANSI_QUOTES-enabled MySQL rejects the following query produced by Dbmate's MySQL driver:

SHOW TABLES LIKE "schema_migrations"

From: https://github.com/amacneil/dbmate/blob/6245b8c/pkg/driver/mysql/mysql.go#L236

Fix & Thought Process

MySQL driver's quotedMigrationsTableName() complies with ANSI_QUOTES. Therefore I initially attempted to replicate other command implementations over to MigrationsTableExists() like so:

-       err := db.QueryRow(fmt.Sprintf("SHOW TABLES LIKE \"%s\"",
-               drv.migrationsTableName)).
-               Scan(&match)
+       err := db.QueryRow("show tables like ?", drv.quotedMigrationsTableName()).Scan(&match)

This would be ideal for two reasons:

  • Code-base consistency
  • Delegating string interpolation to the sql package

However the query produced by the above code was considered invalid by MySQL (with and without ANSI_QUOTES). It appears that the LIKE predicate is picky about how the condition is quoted. Fair enough, I went a step further and tried this code instead:

- err := db.QueryRow("show tables like ?", drv.quotedMigrationsTableName()).Scan(&match)
+ err := db.QueryRow("show tables like '?'", drv.quotedMigrationsTableName()).Scan(&match)

Unfortunately, this time, the sql package's string interpolation was negatively affected. It seems to not appreciate the quotes around the ? interpolation character.

Error: sql: expected 0 arguments, got 1

At this point I understood the agony of the person who last worked on this part of the code, and why MySQL driver's MigrationsTableExists() has an inconsistent fmt.Sprintf based implementation. One code consistency I was able to pick up was lower-casing the entire query.

The final code change may appear lazy but in fact a lot of thought went into it. Cheers!

@toru toru changed the title MySQL: Add ANSI_QUOTE support to MigrationsTableExists() MySQL: Add ANSI_QUOTES support to MigrationsTableExists() Sep 14, 2023
@toru
Copy link
Contributor Author

toru commented Sep 14, 2023

Apologies. My example code diff was incorrect. I am cursed with a very strong muscle memory of typing: show create table.

Copy link
Collaborator

@dossy dossy left a comment

Choose a reason for hiding this comment

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

This is still not ideal as we should really escape values, but considering that even database/sql hasn't provided a driver-level escaping mechanism in nearly 7 years suggests that we will either have to implement one ourselves, or just 🙈 and hope for the best.

@dossy dossy merged commit 5386efb into amacneil:main Nov 15, 2023
@dossy dossy linked an issue Nov 15, 2023 that may be closed by this pull request
@toru
Copy link
Contributor Author

toru commented Nov 15, 2023

Appreciate your understanding @dossy! Agreed, the current structure is not ideal. These edge-case code paths can quickly become an attack vector. I suppose we can make peace today by calling it necessary evil. It would indeed be cool to revise the code at some point, like you say, even if it means bringing the escaping responsibility to Dbmate's layer.

@toru
Copy link
Contributor Author

toru commented Nov 15, 2023

For posterity, what prompted me to submit this PR was DigitalOcean's managed MySQL service. At the time of this comment, their instances are bootstrapped with the ANSI_QUOTES SQL option.

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

Successfully merging this pull request may close these issues.

Dbmate Unable to Create SchemaMigrations Table
2 participants