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 SchemaManagerFunctionalTestCase::testSwitchPrimaryKeyOrder() #6819

Merged
merged 1 commit into from
Mar 4, 2025

Conversation

morozov
Copy link
Member

@morozov morozov commented Mar 4, 2025

The original test was reworked in #5714 (3.5.x). Prior to the rework, the final assertion would look like this (which was correct):

self::assertEquals(
[
'DROP INDEX `primary` ON test',
'ALTER TABLE test ADD PRIMARY KEY (bar_id, foo_id)',
],
$sql,
);

After the rework, the assertion started looking like this (incorrect, note the column order):

self::assertSame(['foo_id', 'bar_id'], $primaryKey->getColumns());

The version of the test after the rework produces the $diff but doesn't use it for anything (it's an unused variable). The diff was supposed to be applied to the database schema, and then the assertion should have been made that the new column order is now effective.

Skipping the test

The test passes on MySQL and SQLite because the corresponding DBALs APIs were designed after MySQL. Specifically, DROP INDEX PRIMARY on MySQL (and, for some reason, on SQLite) drops the primary key. The Postgres implementation gets away with a hack – instead of dropping the index, it generates the PK name and drops the constraint:

if ($name === '"primary"') {
if (str_ends_with($table, '"')) {
$constraintName = substr($table, 0, -1) . '_pkey"';
} else {
$constraintName = $table . '_pkey';
}
return $this->getDropConstraintSQL($constraintName, $table);

This hack is not implemented for other platforms, so they don't support dropping the PK and fail with a SQL error, which is a bug. This hack cannot be ported to the other platforms easily, because the auto-generated name there is less predictable. This issue could be addressed by replacing the "primary index" with PrimaryKeyConstraint, which, once introspected from the database, would hold the actual constraint name.

@morozov morozov force-pushed the fix-test-switch-primary-key-order branch from f0a8799 to 4093dd9 Compare March 4, 2025 03:05
@morozov morozov added this to the 4.2.3 milestone Mar 4, 2025
@morozov morozov marked this pull request as ready for review March 4, 2025 03:12
@morozov morozov merged commit be9c119 into doctrine:4.2.x Mar 4, 2025
66 checks passed
@morozov morozov deleted the fix-test-switch-primary-key-order branch March 4, 2025 14:50
@morozov morozov removed the Bug label Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants