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

Using explicit inclusion in schema_filter causes unexpected behavior when diffing database #1327

Open
thsmrtone1 opened this issue Feb 24, 2023 · 2 comments

Comments

@thsmrtone1
Copy link

Bug Report

Q A
BC Break yes/no
Version 3.5.5
  • Azure SQL Database (reproduces on docker MSSQL Server 2019)
  • sqlsrv driver
  • DBAL 3.5.3
  • DoctrineMigrationsBundle 3.2.2
  • doctrine migrations 3.5.5
  • ORM 2.14.1

Summary

Perhaps I've stumbled upon unsupported usage of schema_filter. If so, please let me know. Please note that this example is using a MSSQL database, which seems to use "schemas" much differently than other platforms. I have not tested on other platforms.

I'm trying to use the schema_filter config parameter to limit my project to one specific schema (instead of excluding a number of schemas). For example:

# doctrine.yaml
doctrine:
  dbal:
    schema_filter: ~^my_schema.~

All entities have been defined with both a schema and table name, for example:

#[ORM\Entity()]
#[ORM\Table(name: 'my_table', schema: 'my_schema')]
class MyTable {
  // ...
}

For the purposes of this bug report, assume that my_table already exists in my_schema in the database.

When executing bin/console doctrine:migrations:diff, I would expect a blank migration to be created, since there are no changes needed, however a migration is being created containing the following:

$this->addSql('DROP TABLE my_schema.my_table');

It appears that the issue is likely caused by this line in DiffGenerator::createToSchema():

foreach ($toSchema->getTables() as $table) {
    $tableName = $table->getName();

    if ($schemaAssetsFilter($this->resolveTableName($tableName))) {  // <--- Issue here
        continue;
    }

    $toSchema->dropTable($tableName);
}

When $tableName is "my_schema.my_table", then resolveTableName() will remove "my_schema." from the name. RegexSchemaAssetFilter will then return false because it does not match the schema_filter in the config, thus making the migration drop the table.

@NoiseByNorthwest
Copy link

NoiseByNorthwest commented Jul 17, 2023

I'm facing the same issue, this is partly due to the resolveTableName applied on the fully qualified table name here https://github.com/doctrine/migrations/blob/3.6.x/lib/Doctrine/Migrations/Generator/DiffGenerator.php#L139 which seems to be a non-sense since schema_filter is meant to be applied on full table names.

EDIT: This is BTW exactly what @thsmrtone1 has described...

@NoiseByNorthwest
Copy link

There is a second cause here https://github.com/doctrine/dbal/blob/3.6.x/src/Schema/AbstractSchemaManager.php#L1644

The namespace list is not filtered accordingly to schema_filter, adding $schemaNames = $this->filterAssetNames($schemaNames); does the job.

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

No branches or pull requests

2 participants