Pergunta

I want to start with a specific example where a table is created with a column having a constraint indicating this column is a foreign key:

<changeSet id="1" author="A1">
    <createTable tableName="TABLE_A">
        <!-- Other columns -->
        <column name="FK_TABLE_B" type="BIGINT">
            <!-- the 'TABLE_B' will be renamed soon ! -->
            <constraints references="TABLE_B(ID_PK)"
                foreignKeyName="JUST_UNIQUE_I_GUESS" nullable="false" />
        </column>
    </createTable>
</changeSet>

Now a first question: Assume TABLE_B would be renamed to TABLE_NEW... how would i want to change the above constraint so it points to the renamed Tables column ID_PK?

I see a few possibilities:

  • modify the changeSet itself (no Problem with a H2 inMemory DB... but cmon... thats not the idear right?)
  • Drop the whole column in my own changeSet and add it again with the new constraint which could make sence since the column name will probably change to FK_TABLE_NEW anyways... but thats not a real possibilitie for a productive environment
  • somehow alter that constraint -> BUT HOW!? the documentation is no help at all...

The next question then is why to have this way of constraints if there is a

<addForeignKeyConstraint ...>

as well?

So currently i dont feel Liquibase at all just because this was my very first try to change an existing changeSet and my biggest question is: Is this a issue of not understanding the best practise or acctually a problem of the "not so verbouse" documentation?

Thanks for any help!

What i tried

assumed above constraints definition somehow leads to the same as a when done with a

<addForeignKeyConstraint ...>

tag but with less attributes. So i thought i could just use the

<dropForeignKeyConstraint ...>

tag first delete the ForeignKeyConstraint and then add a new one. But it still tells me it wont find table TABLE_B when trying to execute the first changeSet in a H2 in-memory DB.

My Changeset looked like this:

<changeSet id="1" author="A2">
    <dropForeignKeyConstraint baseTableName="TABLE_A"
        constraintName="JUST_UNIQUE_I_GUESS" />

    <addForeignKeyConstraint
        constraintName="JUST_UNIQUE_I_GUESS"
        referencedTableName="TABLE_NEW" baseColumnNames="FK_TABLE_B"
        baseTableName="TABLE_A" referencedColumnNames="ID_PK" />
</changeSet>

Background Information

Since were currently just building a POC using a in-memory DB only (H2) its no big deal to just change the first changeSet until we have quite of a final scheme-state... but how to deal with such things if you already have a existing DB, millions of entrys and stuff? Currently i highly doubt Liquibase is the right decision for a company with 1k+ developpers without hiring Liquibase Experts...

Foi útil?

Solução

You should only modify your changesets as long as they (resp. the software/database that go along with the changesets) have not been released.

We keep our changeset files (along with the code) in our source code repository. During development phase everyone is allowed to change changesets defined for the version currently under development.

As soon as the version is released the changeset files are considered to be fixed and should not be changed anymore. (With the release the software is shipped and customers then have a database that reflects whatever the changesets define).

So after release you will have to create new changeSets that

  1. drop the foreign key constraint
  2. rename your table
  3. add a new foreign key constraint

More or less, you need to do exactly what you would do with pure sql on the database as well. At this point liquibase will more or less just translate your changeSets to sql and apply them to the database. So once you found a way to do your changes with slq it should also be possible to put those into liquibase changesets.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top