Search in sources :

Example 1 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) 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);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PrimaryKey(liquibase.structure.core.PrimaryKey)

Example 2 with PrimaryKey

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));
}
Also used : PrimaryKey(liquibase.structure.core.PrimaryKey) Test(org.junit.Test)

Example 3 with PrimaryKey

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 };
// }
}
Also used : DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) PrimaryKey(liquibase.structure.core.PrimaryKey) Index(liquibase.structure.core.Index) DropPrimaryKeyChange(liquibase.change.core.DropPrimaryKeyChange) Change(liquibase.change.Change)

Example 4 with PrimaryKey

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());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) ExecutorService(liquibase.executor.ExecutorService) PrimaryKey(liquibase.structure.core.PrimaryKey) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Example 5 with PrimaryKey

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());
    }
}
Also used : PrimaryKey(liquibase.structure.core.PrimaryKey)

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