tlberglund / groovy-liquibase

Yet Another Groovy DSL for Liquibase
Other
85 stars 66 forks source link

primaryKeyName constraint doesn't appear to work with addColumn #41

Closed ChrisDevo closed 10 years ago

ChrisDevo commented 10 years ago

While trying to replace a primary key column in an existing table, the primary key name for my Oracle and PostgreSQL tables isn't being set (instead the default is used for each DBMS).

I first dropped the existing primary key column using dropColumn:

dropColumn(columnName: 'foo_id', tableName: 'foo')

Then I tried to add the column using addColumn:

addColumn(tableName: 'foo') {
    column(name: 'new_foo_id', type: '${primaryKeyType}') {
        constraints(primaryKey: true, primaryKeyName: 'pk_foo')
    }
}

This creates the primary key, but it doesn't create the primary key name.

To get around this, I did the following:

addColumn(tableName: 'foo') {
    column(name: 'new_foo_id', type: '${primaryKeyType}')
}
addPrimaryKey(columnNames: 'new_foo_id', tableName: 'foo', 
    constraintName: 'pk_foo')
stevesaliman commented 10 years ago

I've done some poking around, mainly running Liquibase (the latest master branch) with a debug log level, and I believe this is an issue with Liquibase itself because the debug log shows that the primary key constraint is part of the change set when Liquibase computes the checksum, which means that the Groovy change set parsed correctly.

Looking at this from a Liquibase point of view, this may be more of a design feature than a bug. The createTable change in Liquibase maps to a "create table" SQL statement, which can include a "primary key" clause. The alterTable change in Liquibase maps to an "alter table" SQL statement. I don't know if all the databases Liquibase supports are able to add a column and a primary key constraint in the same SQL statement.

That said, Liquibase could keep track of the fact that you have a primary key in your column definition and issue the SQL to create the primary key as a second SQL statement, but that would have to be implemented in Liquibase itself.