Search in sources :

Example 6 with PrimaryKey

use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.

the class CreateTableChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    try {
        Table example = (Table) new Table().setName(getTableName()).setSchema(getCatalogName(), getSchemaName());
        ChangeStatus status = new ChangeStatus();
        Table tableSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
        status.assertComplete(tableSnapshot != null, "Table does not exist");
        if (tableSnapshot != null) {
            for (ColumnConfig columnConfig : getColumns()) {
                Column columnSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(columnConfig).setRelation(tableSnapshot), database);
                status.assertCorrect(columnSnapshot != null, "Column " + columnConfig.getName() + " is missing");
                if (columnSnapshot != null) {
                    ConstraintsConfig constraints = columnConfig.getConstraints();
                    if (constraints != null) {
                        if ((constraints.isPrimaryKey() != null) && constraints.isPrimaryKey()) {
                            PrimaryKey tablePk = tableSnapshot.getPrimaryKey();
                            status.assertCorrect((tablePk != null) && tablePk.getColumnNamesAsList().contains(columnConfig.getName()), "Column " + columnConfig.getName() + " is not part of the primary key");
                        }
                        if (constraints.isNullable() != null) {
                            if (constraints.isNullable()) {
                                status.assertCorrect((columnSnapshot.isNullable() == null) || columnSnapshot.isNullable(), "Column " + columnConfig.getName() + " nullability does not match");
                            } else {
                                status.assertCorrect((columnSnapshot.isNullable() != null) && !columnSnapshot.isNullable(), "Column " + columnConfig.getName() + " nullability does not match");
                            }
                        }
                    }
                }
            }
        }
        return status;
    } catch (Exception e) {
        return new ChangeStatus().unknown(e);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) PrimaryKey(liquibase.structure.core.PrimaryKey) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 7 with PrimaryKey

use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.

the class AddColumnChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        for (AddColumnConfig column : getColumns()) {
            Column snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), column.getName()), database);
            result.assertComplete(snapshot != null, "Column " + column.getName() + " does not exist");
            if (snapshot != null) {
                PrimaryKey snapshotPK = ((Table) snapshot.getRelation()).getPrimaryKey();
                ConstraintsConfig constraints = column.getConstraints();
                if (constraints != null) {
                    result.assertComplete(constraints.isPrimaryKey() == ((snapshotPK != null) && snapshotPK.getColumnNames().contains(column.getName())), "Column " + column.getName() + " not set as primary key");
                }
            }
        }
    } catch (Exception e) {
        return result.unknown(e);
    }
    return result;
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) PrimaryKey(liquibase.structure.core.PrimaryKey)

Example 8 with PrimaryKey

use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.

the class PrimaryKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    try {
        PrimaryKey example = new PrimaryKey();
        Table table = new Table();
        table.setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (StringUtil.trimToNull(getTableName()) != null) {
            table.setName(getTableName());
        }
        example.setTable(table);
        example.setName(getPrimaryKeyName());
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            if (tableName != null) {
                throw new PreconditionFailedException("Primary Key does not exist on " + database.escapeObjectName(getTableName(), Table.class), changeLog, this);
            } else {
                throw new PreconditionFailedException("Primary Key " + database.escapeObjectName(getPrimaryKeyName(), PrimaryKey.class) + " does not exist", changeLog, this);
            }
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PrimaryKey(liquibase.structure.core.PrimaryKey) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 9 with PrimaryKey

use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.

the class PrimaryKeyTest method setColumn_outOfOrder.

@Test
public void setColumn_outOfOrder() {
    PrimaryKey pk = new PrimaryKey();
    pk.addColumn(1, new Column("id2"));
    pk.addColumn(0, new Column("id1"));
    assertEquals(2, pk.getColumnNamesAsList().size());
    assertEquals("id1", pk.getColumnNamesAsList().get(0));
    assertEquals("id2", pk.getColumnNamesAsList().get(1));
}
Also used : PrimaryKey(liquibase.structure.core.PrimaryKey) Test(org.junit.Test)

Example 10 with PrimaryKey

use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.

the class PrimaryKeyTest method setColumn_singlePKColumn.

@Test
public void setColumn_singlePKColumn() {
    PrimaryKey pk = new PrimaryKey();
    pk.addColumn(0, new Column("id"));
    assertEquals(1, pk.getColumnNamesAsList().size());
}
Also used : PrimaryKey(liquibase.structure.core.PrimaryKey) Test(org.junit.Test)

Aggregations

PrimaryKey (liquibase.structure.core.PrimaryKey)12 Table (liquibase.structure.core.Table)6 Column (liquibase.structure.core.Column)4 Test (org.junit.Test)4 Change (liquibase.change.Change)2 Schema (liquibase.structure.core.Schema)2 BigInteger (java.math.BigInteger)1 ColumnConfig (liquibase.change.ColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 CreateTableChange (liquibase.change.core.CreateTableChange)1 DropPrimaryKeyChange (liquibase.change.core.DropPrimaryKeyChange)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 DatabaseDataType (liquibase.datatype.DatabaseDataType)1 LiquibaseDataType (liquibase.datatype.LiquibaseDataType)1 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)1 PreconditionErrorException (liquibase.exception.PreconditionErrorException)1 PreconditionFailedException (liquibase.exception.PreconditionFailedException)1 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)1