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) throws PreconditionFailedException, PreconditionErrorException {
try {
PrimaryKey example = new PrimaryKey();
Table table = new Table();
table.setSchema(new Schema(getCatalogName(), getSchemaName()));
if (StringUtils.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_inOrder.
@Test
public void setColumn_inOrder() {
PrimaryKey pk = new PrimaryKey();
pk.addColumn(0, new Column("id1"));
pk.addColumn(1, new Column("id2"));
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 UnexpectedPrimaryKeyChangeGenerator method fixUnexpected.
@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
// if (!diffResult.getObjectDiff(Table.class).getUnexpected().contains(pk.getTable())) {
PrimaryKey pk = (PrimaryKey) unexpectedObject;
DropPrimaryKeyChange change = new DropPrimaryKeyChange();
change.setTableName(pk.getTable().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(pk.getTable().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(pk.getTable().getSchema().getName());
}
change.setConstraintName(pk.getName());
Index backingIndex = pk.getBackingIndex();
control.setAlreadyHandledUnexpected(backingIndex);
return new Change[] { change };
// }
}
use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.
the class CockroachDBIntegrationTest method descPrimaryKey.
@Test
public void descPrimaryKey() throws Exception {
if (getDatabase() == null) {
return;
}
final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
executor.execute(new RawSqlStatement("DROP TABLE IF EXISTS pk"));
executor.execute(new RawSqlStatement("CREATE TABLE pk (\n" + "a INT8 NOT NULL,\n" + "b INT8 NOT NULL,\n" + "c INT8 NOT NULL,\n" + "d INT8 NOT NULL,\n" + "CONSTRAINT \"primary\" PRIMARY KEY (a ASC, b ASC, c DESC)\n" + ")"));
DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(getDatabase().getDefaultSchema(), getDatabase(), new SnapshotControl(getDatabase()));
PrimaryKey pk = snapshot.get(new PrimaryKey().setTable(new Table().setName("pk")).setName("primary"));
List<Column> columns = pk.getColumns();
assertEquals("a", columns.get(0).getName());
assertNull(columns.get(0).getDescending());
assertEquals("b", columns.get(1).getName());
assertNull(columns.get(1).getDescending());
assertEquals("c", columns.get(2).getName());
assertTrue(columns.get(2).getDescending());
}
use of liquibase.structure.core.PrimaryKey in project liquibase by liquibase.
the class PrimaryKeyComparator method isSameObject.
@Override
public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, DatabaseObjectComparatorChain chain) {
if (!((databaseObject1 instanceof PrimaryKey) && (databaseObject2 instanceof PrimaryKey))) {
return false;
}
PrimaryKey thisPrimaryKey = (PrimaryKey) databaseObject1;
PrimaryKey otherPrimaryKey = (PrimaryKey) databaseObject2;
if ((thisPrimaryKey.getTable() != null) && (thisPrimaryKey.getTable().getName() != null) && (otherPrimaryKey.getTable() != null) && (otherPrimaryKey.getTable().getName() != null)) {
return DatabaseObjectComparatorFactory.getInstance().isSameObject(thisPrimaryKey.getTable(), otherPrimaryKey.getTable(), chain.getSchemaComparisons(), accordingTo);
} else {
return StringUtil.trimToEmpty(thisPrimaryKey.getName()).equalsIgnoreCase(otherPrimaryKey.getName());
}
}
Aggregations