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

SQLServer renameTable() problem #6566

Open
aimeos opened this issue Oct 21, 2024 · 9 comments
Open

SQLServer renameTable() problem #6566

aimeos opened this issue Oct 21, 2024 · 9 comments

Comments

@aimeos
Copy link
Contributor

aimeos commented Oct 21, 2024

Bug Report

Q A
Version 3.9/4.2

Summary

SQL Server uses sp_rename function for renaming tables but using quotes for target table name results in wrong table name.

How to reproduce

$schemaManager->renameTable('[test]', '[dbo.test2]');
// -> sp_rename '[test]', '[dbo.test2]'

The new table is now named "[dbo.test2]" instead of "test2". This does only occur in SQLServer platform, all other platforms use quoting as expected so this is inconsistent between SQLServer platform and all other platforms.

Expected behaviour

Quotes for the target table name should be automatically removed by the SQLServer platform here to get consistent results:

$this->quoteStringLiteral($newName),

$schemaManager->renameTable('[test]', '[dbo.test2]');
// -> sp_rename '[test]', 'test2'
@morozov
Copy link
Member

morozov commented Nov 16, 2024

Can sp_rename move the table to a different schema? See the details in #6602.

As currently designed, the behavior of renameTable() isn't portable across supported platforms. Unlike other platforms where the second parameter is a SQL expression representing the new name, the SQL Server platform (due to the implementation details) accepts a literal.

@aimeos
Copy link
Contributor Author

aimeos commented Nov 17, 2024

No, sp_rename can't move tables to a different schema and only accepts a literal as new name.

@morozov
Copy link
Member

morozov commented Dec 12, 2024

@aimeos so what do you expect to happen as a result of such a call to renameTable()?

@aimeos
Copy link
Contributor Author

aimeos commented Dec 13, 2024

@morozov Using sp_rename '[test]', 'dbo.test2' will cause an error thrown by SQL Server

@morozov
Copy link
Member

morozov commented Dec 13, 2024

There won't be an error. "dbo.test2" is a valid table name, so that will be the new name. Could you check if #6602 solves your problem?

@aimeos
Copy link
Contributor Author

aimeos commented Dec 16, 2024

Yes, it fixes the problem. You are right, dbo.test2 is a vaild column name for sp_rename() and maybe renaming columns could be improved further by using
$newColumnName = $newColumn->getShortestName($newColumn->getNamespaceName());
instead of
$newColumnName = $newColumn->getName();

https://github.com/doctrine/dbal/pull/6602/files#diff-269c4df546691d197440efad484aabdcef49ff891af599481406d8b1c1a2296dR419

@morozov
Copy link
Member

morozov commented Dec 16, 2024

maybe renaming columns could be improved further by using
$newColumnName = $newColumn->getShortestName($newColumn->getNamespaceName());
instead of
$newColumnName = $newColumn->getName();

This would implicitly drop part of the data supplied by the user. Not the best idea.

The proper approach is to parse the name as an UnqualifiedName (see #6646). This way, dbo.test would be considered an invalid value unless quoted.

@aimeos
Copy link
Contributor Author

aimeos commented Dec 17, 2024

The proper approach is to parse the name as an UnqualifiedName (see #6646). This way, dbo.test would be considered an invalid value unless quoted.

If the value is quoted and passed to the sp_rename function that way, it will again rename to w wrong table name. When dbo.test or [dbo].[test] is passed to renameColumn(), the only valid value for the second parameter for sp_rename is test

@morozov
Copy link
Member

morozov commented Dec 17, 2024

You're missing this part:

The proper approach is to parse the name [...]

Please take a look at #6646 for context.

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