Search in sources :

Example 11 with PrimaryKey

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

the class AddPrimaryKeyChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        PrimaryKey example = new PrimaryKey(getConstraintName(), getCatalogName(), getSchemaName(), getTableName(), Column.arrayFromNames(getColumnNames()));
        PrimaryKey snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
        result.assertComplete(snapshot != null, "Primary key does not exist");
        return result;
    } catch (Exception e) {
        return result.unknown(e);
    }
}
Also used : PrimaryKey(liquibase.structure.core.PrimaryKey)

Example 12 with PrimaryKey

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

the class MissingTableChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table missingTable = (Table) missingObject;
    PrimaryKey primaryKey = missingTable.getPrimaryKey();
    List<String> pkColumnList = ((primaryKey != null) ? primaryKey.getColumnNamesAsList() : null);
    Map<Column, UniqueConstraint> singleUniqueConstraints = getSingleColumnUniqueConstraints(missingTable);
    CreateTableChange change = createCreateTableChange();
    change.setTableName(missingTable.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(missingTable.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(missingTable.getSchema().getName());
    }
    if (missingTable.getRemarks() != null) {
        change.setRemarks(missingTable.getRemarks());
    }
    if (control.getIncludeTablespace() && (missingTable.getTablespace() != null) && comparisonDatabase.supportsTablespaces()) {
        change.setTablespace(missingTable.getTablespace());
    }
    for (Column column : missingTable.getColumns()) {
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setName(column.getName());
        LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
        DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
        String typeString = ddt.toString();
        if (comparisonDatabase instanceof MSSQLDatabase) {
            typeString = comparisonDatabase.unescapeDataTypeString(typeString);
        }
        columnConfig.setType(typeString);
        if (column.isAutoIncrement()) {
            columnConfig.setAutoIncrement(true);
        }
        boolean primaryKeyOrderMatchesTableOrder = checkPrimaryKeyOrderMatchesTableOrder(missingTable, pkColumnList);
        ConstraintsConfig constraintsConfig = null;
        // In MySQL, the primary key must be specified at creation for an autoincrement column
        if ((pkColumnList != null) && primaryKeyOrderMatchesTableOrder && pkColumnList.contains(column.getName())) {
            if ((referenceDatabase instanceof MSSQLDatabase) && (primaryKey.getBackingIndex() != null) && (primaryKey.getBackingIndex().getClustered() != null) && !primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else if ((referenceDatabase instanceof PostgresDatabase) && (primaryKey.getBackingIndex() != null) && (primaryKey.getBackingIndex().getClustered() != null) && primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else {
                constraintsConfig = new ConstraintsConfig();
                if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
                    constraintsConfig.setPrimaryKey(true);
                    constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
                    // MySQL sets some primary key names as PRIMARY which is invalid
                    if ((comparisonDatabase instanceof MySQLDatabase) && "PRIMARY".equals(primaryKey.getName())) {
                        constraintsConfig.setPrimaryKeyName(null);
                    } else {
                        constraintsConfig.setPrimaryKeyName(primaryKey.getName());
                    }
                    control.setAlreadyHandledMissing(primaryKey);
                    control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
                } else {
                    constraintsConfig.setNullable(false);
                }
            }
        }
        if ((column.isNullable() != null) && !column.isNullable()) {
            if (constraintsConfig == null) {
                constraintsConfig = new ConstraintsConfig();
            }
            constraintsConfig.setNullable(false);
            if (!column.getValidateNullable()) {
                constraintsConfig.setValidateNullable(false);
            }
            constraintsConfig.setNotNullConstraintName(column.getAttribute("notNullConstraintName", String.class));
        }
        if (referenceDatabase instanceof MySQLDatabase) {
            UniqueConstraint uniqueConstraint = singleUniqueConstraints.get(column);
            if (uniqueConstraint != null) {
                if (!control.alreadyHandledMissing(uniqueConstraint, referenceDatabase)) {
                    if (constraintsConfig == null) {
                        constraintsConfig = new ConstraintsConfig();
                    }
                    constraintsConfig.setUnique(true);
                    control.setAlreadyHandledMissing(uniqueConstraint);
                    control.setAlreadyHandledMissing(uniqueConstraint.getBackingIndex());
                }
            }
        }
        if (constraintsConfig != null) {
            columnConfig.setConstraints(constraintsConfig);
        }
        setDefaultValue(columnConfig, column, referenceDatabase);
        if (column.getRemarks() != null) {
            columnConfig.setRemarks(column.getRemarks());
        }
        Column.AutoIncrementInformation autoIncrementInfo = column.getAutoIncrementInformation();
        if (autoIncrementInfo != null) {
            BigInteger startWith = autoIncrementInfo.getStartWith();
            BigInteger incrementBy = autoIncrementInfo.getIncrementBy();
            String generationType = autoIncrementInfo.getGenerationType();
            Boolean defaultOnNull = autoIncrementInfo.getDefaultOnNull();
            if ((startWith != null) && !startWith.equals(BigInteger.ONE)) {
                columnConfig.setStartWith(startWith);
            }
            if ((incrementBy != null) && !incrementBy.equals(BigInteger.ONE)) {
                columnConfig.setIncrementBy(incrementBy);
            }
            if (StringUtil.isNotEmpty(generationType)) {
                columnConfig.setGenerationType(generationType);
                if (defaultOnNull != null) {
                    columnConfig.setDefaultOnNull(defaultOnNull);
                }
            }
        }
        // 
        if (column.getComputed() != null) {
            columnConfig.setComputed(column.getComputed());
        }
        change.addColumn(columnConfig);
        control.setAlreadyHandledMissing(column);
    }
    return new Change[] { change };
}
Also used : Table(liquibase.structure.core.Table) ColumnConfig(liquibase.change.ColumnConfig) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) PrimaryKey(liquibase.structure.core.PrimaryKey) UniqueConstraint(liquibase.structure.core.UniqueConstraint) MySQLDatabase(liquibase.database.core.MySQLDatabase) Change(liquibase.change.Change) CreateTableChange(liquibase.change.core.CreateTableChange) PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) Column(liquibase.structure.core.Column) CreateTableChange(liquibase.change.core.CreateTableChange) ConstraintsConfig(liquibase.change.ConstraintsConfig) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

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