doctrine / migrations

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

Virtual column always generates migration SQL #1292

Open theoaksoft opened 1 year ago

theoaksoft commented 1 year ago

Bug Report

Q A
BC Break no
Version 3.5.2
ORM Version 2.13.4
DBAL Version 3.5.1

Summary

I have a virtual column defined as a computed column on one of my SQL Server database tables. After running doctrine-migration diff the first time, the column is created as defined. However, everytime I run doctrine-migration diff, the system attempts to alter the column with the column definition again.

Current behavior


#[Column(
        type: "boolean",
        insertable: false,
        updatable: false,
        columnDefinition: "AS CONVERT(bit, (case
      when CONVERT(date, GETDATE()) between start_date and end_date then 1
      else 0
      end))",
        generated: "ALWAYS"
    )]
    private bool $currentPeriod = false;

First migration sql:

$this->addSql('ALTER TABLE finance_accounting_periods ADD currentPeriod AS CONVERT(bit, (case
              when CONVERT(date, GETDATE()) between start_date and end_date then 1
              else 0
              end))');

Subsequent migration sql:

$this->addSql('ALTER TABLE finance_accounting_periods ALTER COLUMN currentPeriod AS (case
              when CONVERT(date, GETDATE()) between [start_date] and [end_date] then 1
              else 0
              end)');

How to reproduce

  1. Create an entity mapped to an sql server table
  2. Add a virtual column and add its definition as a computed column
  3. Run doctrine-migration diff & doctrine-migration migrate to persist to the database
  4. Run doctrine-migration diff again

Expected behavior

After the initial migration SQL, no other column alteration sql is expected to be generated again. On SQL server, computed columns are expected to be dropped and recreated. So, the current behaviour causes doctrine-migration migrate to fail.

stof commented 1 year ago

doctrine/migrations's diff command generates a migration with the SQL queries provided by the schema diff tool of doctrine/dbal. So that's something to be fixed in DBAL. However, I think using columnDefinition is indeed not supported well in the schema tool as that's a case where you bypass it to create the column (and so it is not able to introspect it properly)