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

Handle database with escapable identifiers in name #717

Merged
merged 3 commits into from
May 9, 2024
Merged

Handle database with escapable identifiers in name #717

merged 3 commits into from
May 9, 2024

Conversation

jharder
Copy link
Contributor

@jharder jharder commented May 7, 2024

In the case where a MySQL database has a hyphen in it, the updated migrations in 4.3.0 cause the migration to fail with a syntax error. The previous Phinx-backed migrations did not encounter this.

Looking into the code, there were only two locations that did not escape the database name, setConnection() and createDatabase(), which have been updated to use the quoteTableName() method. Additionally, there were a couple other instances where the database name was being manually quoted with backticks, so I refactored those to use the quoteTableName() method as well for consistency.

Tests have been updated to explicitly create and drop a database with the configured name plus a hyphenated suffix component. All tests continue to pass. The dropDatabase() method which runs before createDatabase() in the test setup had previously had quoting hard-coded into the query, which explains why the original syntax error did not occur at that point. After refactoring, the dropDatabase() method continued to work without regression.

jharder added 3 commits May 7, 2024 09:44
In MySQL a hyphen (-) in a database name is not prohibited but requires the name to be escaped with backtick (`).

The methods setConnection() and createDatabase() passed the database name unaltered which caused migrations to fail for systems with hyphens in the database name.

This change wraps the database name in quoteTableName(), and also identified a couple other cases where the name should be or was inconsistently quoted.
Adding a test case to cover databases with hyphen in its name.
Looks like the TABLE_SCHEMA was being quoted already, so this double-quoted and caused some tests to fail.
@dereuromark dereuromark added the bug label May 7, 2024
@markstory markstory merged commit 830e480 into cakephp:4.x May 9, 2024
9 checks passed
@markstory markstory added this to the 4.x (CakePHP 5) milestone May 9, 2024
@markstory
Copy link
Member

Thank you 🎉

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

Successfully merging this pull request may close these issues.

3 participants