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 escaping database name for SqlServerAdapter::dropDatabase #2287

Merged
merged 5 commits into from
Jun 5, 2024

Conversation

MasterOdin
Copy link
Member

@MasterOdin MasterOdin commented Jun 4, 2024

PR fixes a bug introduced in #2286 that broke dropping databases in sqlserver. I've fixed the syntax so that it actually works for database names that need escaping, discovering as well that the escaping of column names was also broken. Per https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms176027(v=sql.105)?redirectedfrom=MSDN, to escape an identifier that has brackets, need to double up the brackets, not use \.

@MasterOdin MasterOdin marked this pull request as ready for review June 5, 2024 02:42
Comment on lines +1210 to +1211
ALTER DATABASE %1$s SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
DROP DATABASE %1$s;',
Copy link
Member Author

Choose a reason for hiding this comment

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

By using %1$s, I'm able to replace both instances with a single value. Downside is that cannot use " around the string like do in other places. Could use a heredoc, and then run the $sql through sprintf on a separate line, but that didn't feel in line with other places sprintf is used.

@@ -1176,7 +1176,7 @@ public function createDatabase(string $name, array $options = []): void
{
$databaseName = $this->quoteColumnName($name);
if (isset($options['collation'])) {
$this->execute(sprintf('CREATE DATABASE %s COLLATE [%s]', $databaseName, $options['collation']));
$this->execute(sprintf('CREATE DATABASE %s COLLATE %s', $databaseName, $options['collation']));
Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure why the collate argument had brackets around it as it's not an identifier and shouldn't be created as such. I also tried ' and " and it didn't work either.

This has probably been broken for a long time, and I guess no one is creating databases with specific collations.

@MasterOdin MasterOdin merged commit ef38e41 into 0.x Jun 5, 2024
12 checks passed
@MasterOdin MasterOdin deleted the fix-database-escaped-characters branch June 5, 2024 02:46
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.

1 participant