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);
}
}
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;
}
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);
}
}
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));
}
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());
}
Aggregations