use of liquibase.structure.core.Column in project liquibase by liquibase.
the class ColumnComparator method findDifferences.
@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
exclude.add("name");
exclude.add("type");
exclude.add("autoIncrementInformation");
if (!GlobalConfiguration.DIFF_COLUMN_ORDER.getCurrentValue()) {
exclude.add("order");
}
ObjectDifferences differences = chain.findDifferences(databaseObject1, databaseObject2, accordingTo, compareControl, exclude);
differences.compare("name", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
differences.compare("type", databaseObject1, databaseObject2, new ObjectDifferences.DatabaseObjectNameCompareFunction(Column.class, accordingTo));
boolean autoIncrement1 = ((Column) databaseObject1).isAutoIncrement();
boolean autoIncrement2 = ((Column) databaseObject2).isAutoIncrement();
if (autoIncrement1 != autoIncrement2 && !compareControl.isSuppressedField(Column.class, "autoIncrementInformation")) {
// only compare if autoIncrement or not since there are sometimes expected differences in start/increment/etc value.
differences.addDifference("autoIncrement", autoIncrement1, autoIncrement2);
}
return differences;
}
use of liquibase.structure.core.Column 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.Column 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.Column in project liquibase by liquibase.
the class AddAutoIncrementChange method checkStatus.
@Override
public ChangeStatus checkStatus(Database database) {
ChangeStatus result = new ChangeStatus();
Column example = new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), getColumnName());
try {
Column column = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
if (column == null) {
return result.unknown("Column does not exist");
}
result.assertComplete(column.isAutoIncrement(), "Column is not auto-increment");
if ((getStartWith() != null) && (column.getAutoIncrementInformation().getStartWith() != null)) {
result.assertCorrect(getStartWith().equals(column.getAutoIncrementInformation().getStartWith()), "startsWith incorrect");
}
if ((getIncrementBy() != null) && (column.getAutoIncrementInformation().getIncrementBy() != null)) {
result.assertCorrect(getIncrementBy().equals(column.getAutoIncrementInformation().getIncrementBy()), "Increment by incorrect");
}
if (getGenerationType() != null && column.getAutoIncrementInformation().getGenerationType() != null) {
result.assertCorrect(getGenerationType().equals(column.getAutoIncrementInformation().getGenerationType()), "Generation type is incorrect");
}
if (getDefaultOnNull() != null && column.getAutoIncrementInformation().getDefaultOnNull() != null) {
result.assertCorrect(getDefaultOnNull().equals(column.getAutoIncrementInformation().getDefaultOnNull()), "Default on null is incorrect");
}
return result;
} catch (DatabaseException | InvalidExampleException e) {
return result.unknown(e);
}
}
use of liquibase.structure.core.Column in project liquibase by liquibase.
the class ColumnExistsPrecondition method checkUsingSnapshot.
private void checkUsingSnapshot(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
Column example = new Column();
if (StringUtil.trimToNull(getTableName()) != null) {
example.setRelation(new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(new Schema(getCatalogName(), getSchemaName())));
}
example.setName(database.correctObjectName(getColumnName(), Column.class));
try {
if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
throw new PreconditionFailedException("Column '" + database.escapeColumnName(catalogName, schemaName, getTableName(), getColumnName()) + "' does not exist", changeLog, this);
}
} catch (LiquibaseException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
Aggregations