doctrine / migrations

Doctrine Database Migrations Library
https://www.doctrine-project.org/projects/migrations.html
MIT License
4.68k stars 385 forks source link

Unable to create migration version table within non-default schema #1321

Closed thsmrtone1 closed 1 year ago

thsmrtone1 commented 1 year ago

DB platform: MSSQL (sqlsrv)

Using the following config:

doctrine_migrations:
    migrations_paths:
        'DoctrineMigrations': '%kernel.project_dir%/migrations'
    storage:
        table_storage:
            table_name: 'alt_schema.MigrationVersions'

The first time I execute the migration, it will successfully run, and will automatically create the MigrationVersions table within the alt_schema schema. However, on subsequent migrations, I will receive the following error:

An exception occurred while executing a query: SQLSTATE [42S01, 2714]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named 'MigrationVersions' in the database.
kipit commented 1 year ago

Hello!

Not sure about MSSQL — I mainly use PostgreSQL — but with the help of my colleague @Pol-Valentin we face the same issue and investiguate it. Doctrine Migrations, with the help of Doctrine DBAL, check if the version table should be created using AbstractSchemaManager::tableExists… And this check is performed by comparing the list of all tables with the name of the version table. It may be an issue with the table name comparison… But, for what I read in the code, it should work. The list of tables is retrieved using this SQL query:

SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects  WHERE type = 'U' AND name != 'sysdiagrams' AND category != 2 ORDER BY name

and the table name is always returned as qualified name except on the dbo schema :

    protected function _getPortableTableDefinition($table)
    {
        if ($table['schema_name'] !== 'dbo') {
            return $table['schema_name'] . '.' . $table['table_name'];
        }

        return $table['table_name'];
    }

👉 So… This not seems to be your issue, but I’m curious about what returns this query on your side:

SELECT name, SCHEMA_NAME (uid) AS schema_name FROM sysobjects  WHERE type = 'U' AND name != 'sysdiagrams' AND category != 2 ORDER BY name

👉 Can you give us the version of your SQL Server and your Doctrine Migrations and DBAL version ?

Edit: Bad library repository 😬

thsmrtone1 commented 1 year ago

@kipit Thanks for the response! I actually figured out that I had an incorrect value for my schema_filter config value, and it was actually excluding the schema I was trying to use for the migration versions table. Once this was corrected, I no longer had this issue.

However, I seem to have uncovered a different issue regarding schema_filter in which I will open a different issue for.

You can close this issue