Search in sources :

Example 1 with ForeignKeyConstraint

use of liquibase.statement.ForeignKeyConstraint in project liquibase by liquibase.

the class CreateTableGeneratorInformix method generateSql.

/**
 * Informix SQL-specific implementation of the CREATE TABLE SQL generator.
 * @param statement The properties of the statement that we will translate into SQL
 * @param database For this implementation always an object of the InformixDatabase type
 * @param sqlGeneratorChain Other generators in the pipeline for this command
 * @return An array of Sql[] statements containing the requested SQL statements for Informix SQL
 */
@Override
public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    StringBuilder buffer = new StringBuilder();
    // CREATE TABLE table_name ...
    buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())).append(" ");
    buffer.append("(");
    Iterator<String> columnIterator = statement.getColumns().iterator();
    List<String> primaryKeyColumns = new LinkedList<>();
    /*
         * Build the list of columns and constraints in the form
         * (
         *   column1,
         *   ...,
         *   columnN,
         *   constraint1,
         *   ...,
         *   constraintN
         * )
         */
    while (columnIterator.hasNext()) {
        String column = columnIterator.next();
        buffer.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), column));
        buffer.append(" ").append(statement.getColumnTypes().get(column).toDatabaseDataType(database).toSql());
        AutoIncrementConstraint autoIncrementConstraint = null;
        for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) {
            if (column.equals(currentAutoIncrementConstraint.getColumnName())) {
                autoIncrementConstraint = currentAutoIncrementConstraint;
                break;
            }
        }
        boolean isAutoIncrementColumn = autoIncrementConstraint != null;
        boolean isPrimaryKeyColumn = (statement.getPrimaryKeyConstraint() != null) && statement.getPrimaryKeyConstraint().getColumns().contains(column);
        if (isPrimaryKeyColumn) {
            primaryKeyColumns.add(column);
        }
        if (statement.getDefaultValue(column) != null) {
            Object defaultValue = statement.getDefaultValue(column);
            buffer.append(" DEFAULT ");
            buffer.append(statement.getColumnTypes().get(column).objectToSql(defaultValue, database));
        }
        if (isAutoIncrementColumn) {
            // TODO: check if database supports auto increment on non primary key column
            if (database.supportsAutoIncrement()) {
                String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy(), autoIncrementConstraint.getGenerationType(), autoIncrementConstraint.getDefaultOnNull());
                if (!autoIncrementClause.isEmpty()) {
                    buffer.append(" ").append(autoIncrementClause);
                }
            } else {
                Scope.getCurrentScope().getLog(getClass()).warning(database.getShortName() + " does not support autoincrement columns as requested for " + (database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())));
            }
        }
        if (statement.getNotNullColumns().containsKey(column)) {
            buffer.append(" NOT NULL");
        }
        if (columnIterator.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(",");
    /*
         * We only create a PRIMARY KEY constraint if one is defined and has at least 1 column.
         * General Informix SQL syntax, according to the docs for 11.5
         * https://www.ibm.com/support/knowledgecenter/SSGU8G_11.50.0/com.ibm.sqls.doc/ids_sqs_0100.htm
         * is:
         * ( columns ... --> PRIMARY KEY (column1, ..., columnN) [CONSTRAINT pk_name]
          */
    // 
    PrimaryKeyConstraint pkConstraint = statement.getPrimaryKeyConstraint();
    if ((statement.getPrimaryKeyConstraint() != null) && !statement.getPrimaryKeyConstraint().getColumns().isEmpty()) {
        buffer.append(" PRIMARY KEY (");
        buffer.append(StringUtil.join(primaryKeyColumns, ", "));
        buffer.append(")");
        if (!StringUtil.isEmpty(pkConstraint.getConstraintName())) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(pkConstraint.getConstraintName()));
        }
        // Setting up table space for PK's index if it exist
        buffer.append(",");
    }
    for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) {
        String referencesString = fkConstraint.getReferences();
        if (!referencesString.contains(".") && (database.getDefaultSchemaName() != null)) {
            referencesString = database.getDefaultSchemaName() + "." + referencesString;
        }
        buffer.append(" FOREIGN KEY (").append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())).append(") REFERENCES ").append(referencesString);
        if (fkConstraint.isDeleteCascade()) {
            buffer.append(" ON DELETE CASCADE");
        }
        buffer.append(" CONSTRAINT ");
        buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName()));
        if (fkConstraint.isInitiallyDeferred()) {
            buffer.append(" INITIALLY DEFERRED");
        }
        if (fkConstraint.isDeferrable()) {
            buffer.append(" DEFERRABLE");
        }
        buffer.append(",");
    }
    // TODO: code duplication. Maybe we can merge this whole class into CreateTableGenerator again.
    for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) {
        if ((uniqueConstraint.getConstraintName() != null) && !constraintNameAfterUnique(database)) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName()));
        }
        buffer.append(" UNIQUE (");
        buffer.append(database.escapeColumnNameList(StringUtil.join(uniqueConstraint.getColumns(), ", ")));
        buffer.append(")");
        if ((uniqueConstraint.getConstraintName() != null) && constraintNameAfterUnique(database)) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName()));
        }
        buffer.append(",");
    }
    /*
         * Here, the list of columns and constraints in the form
         * ( column1, ..., columnN, constraint1, ..., constraintN,
         * ends. We cannot leave an expression like ", )", so we remove the last comma.
         */
    String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")";
    if ((statement.getTablespace() != null) && database.supportsTablespaces()) {
        sql += " IN " + statement.getTablespace();
    }
    return new Sql[] { new UnparsedSql(sql, new Table().setName(statement.getTableName()).setSchema(new Schema(statement.getCatalogName(), statement.getSchemaName()))) };
}
Also used : AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) Table(liquibase.structure.core.Table) UnparsedSql(liquibase.sql.UnparsedSql) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) Schema(liquibase.structure.core.Schema) UniqueConstraint(liquibase.statement.UniqueConstraint) LinkedList(java.util.LinkedList) PrimaryKeyConstraint(liquibase.statement.PrimaryKeyConstraint) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 2 with ForeignKeyConstraint

use of liquibase.statement.ForeignKeyConstraint in project liquibase by liquibase.

the class CreateTableGenerator method generateSql.

@Override
public Sql[] generateSql(CreateTableStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<Sql> additionalSql = new ArrayList<>();
    StringBuilder buffer = new StringBuilder();
    buffer.append("CREATE TABLE ").append(database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())).append(" ");
    buffer.append("(");
    boolean isSinglePrimaryKeyColumn = (statement.getPrimaryKeyConstraint() != null) && (statement.getPrimaryKeyConstraint().getColumns().size() == 1);
    boolean isPrimaryKeyAutoIncrement = false;
    Iterator<String> columnIterator = statement.getColumns().iterator();
    BigInteger mysqlTableOptionStartWith = null;
    /* We have reached the point after "CREATE TABLE ... (" and will now iterate through the column list. */
    while (columnIterator.hasNext()) {
        String column = columnIterator.next();
        DatabaseDataType columnType = null;
        if (statement.getColumnTypes().get(column) != null) {
            columnType = statement.getColumnTypes().get(column).toDatabaseDataType(database);
        }
        if (columnType == null) {
            buffer.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), column, false));
        } else {
            buffer.append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), column, !statement.isComputed(column)));
            buffer.append(" ").append(columnType);
        }
        AutoIncrementConstraint autoIncrementConstraint = null;
        for (AutoIncrementConstraint currentAutoIncrementConstraint : statement.getAutoIncrementConstraints()) {
            if (column.equals(currentAutoIncrementConstraint.getColumnName())) {
                autoIncrementConstraint = currentAutoIncrementConstraint;
                break;
            }
        }
        boolean isAutoIncrementColumn = autoIncrementConstraint != null;
        boolean isPrimaryKeyColumn = (statement.getPrimaryKeyConstraint() != null) && statement.getPrimaryKeyConstraint().getColumns().contains(column);
        isPrimaryKeyAutoIncrement = isPrimaryKeyAutoIncrement || (isPrimaryKeyColumn && isAutoIncrementColumn);
        if ((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyColumn && isAutoIncrementColumn) {
            String pkName = StringUtil.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName());
            if (pkName == null) {
                pkName = database.generatePrimaryKeyName(statement.getTableName());
            }
            if (pkName != null) {
                buffer.append(" CONSTRAINT ");
                buffer.append(database.escapeConstraintName(pkName));
            }
            buffer.append(" PRIMARY KEY");
        }
        // for the serial data type in postgres, there should be no default value
        if (columnType != null && !columnType.isAutoIncrement() && (statement.getDefaultValue(column) != null)) {
            Object defaultValue = statement.getDefaultValue(column);
            if (database instanceof MSSQLDatabase) {
                String constraintName = statement.getDefaultValueConstraintName(column);
                if (constraintName == null) {
                    constraintName = ((MSSQLDatabase) database).generateDefaultConstraintName(statement.getTableName(), column);
                }
                buffer.append(" CONSTRAINT ").append(database.escapeObjectName(constraintName, ForeignKey.class));
            }
            if (((database instanceof OracleDatabase) || (database instanceof PostgresDatabase)) && statement.getDefaultValue(column).toString().startsWith("GENERATED ALWAYS ")) {
                buffer.append(" ");
            } else if (database instanceof Db2zDatabase && statement.getDefaultValue(column).toString().contains("CURRENT TIMESTAMP") || statement.getDefaultValue(column).toString().contains("IDENTITY GENERATED BY DEFAULT")) {
                buffer.append(" ");
            } else {
                buffer.append(" DEFAULT ");
            }
            if (defaultValue instanceof DatabaseFunction) {
                buffer.append(database.generateDatabaseFunctionValue((DatabaseFunction) defaultValue));
            } else if (database instanceof Db2zDatabase) {
                if (statement.getDefaultValue(column).toString().contains("CURRENT TIMESTAMP")) {
                    buffer.append("");
                }
                if (statement.getDefaultValue(column).toString().contains("IDENTITY GENERATED BY DEFAULT")) {
                    buffer.append("GENERATED BY DEFAULT AS IDENTITY");
                }
                if (statement.getDefaultValue(column).toString().contains("CURRENT USER")) {
                    buffer.append("SESSION_USER ");
                }
                if (statement.getDefaultValue(column).toString().contains("CURRENT SQLID")) {
                    buffer.append("CURRENT SQLID ");
                }
            } else {
                buffer.append(statement.getColumnTypes().get(column).objectToSql(defaultValue, database));
            }
        }
        if (isAutoIncrementColumn) {
            if (database instanceof PostgresDatabase && buffer.toString().toLowerCase().endsWith("serial")) {
            // don't add more info
            } else if (database.supportsAutoIncrement()) {
                // TODO: check if database supports auto increment on non primary key column
                String autoIncrementClause = database.getAutoIncrementClause(autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy(), autoIncrementConstraint.getGenerationType(), autoIncrementConstraint.getDefaultOnNull());
                if (!"".equals(autoIncrementClause)) {
                    buffer.append(" ").append(autoIncrementClause);
                }
                if (autoIncrementConstraint.getStartWith() != null) {
                    if (database instanceof PostgresDatabase) {
                        int majorVersion = 9;
                        try {
                            majorVersion = database.getDatabaseMajorVersion();
                        } catch (DatabaseException e) {
                        // ignore
                        }
                        if (majorVersion < 10) {
                            String sequenceName = statement.getTableName() + "_" + column + "_seq";
                            additionalSql.add(new UnparsedSql("alter sequence " + database.escapeSequenceName(statement.getCatalogName(), statement.getSchemaName(), sequenceName) + " start with " + autoIncrementConstraint.getStartWith(), new Sequence().setName(sequenceName).setSchema(statement.getCatalogName(), statement.getSchemaName())));
                        }
                    } else if (database instanceof MySQLDatabase) {
                        mysqlTableOptionStartWith = autoIncrementConstraint.getStartWith();
                    }
                }
            } else {
                Scope.getCurrentScope().getLog(getClass()).warning(database.getShortName() + " does not support autoincrement columns as requested for " + (database.escapeTableName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName())));
            }
        }
        // Do we have a NOT NULL constraint for this column?
        if (statement.getNotNullColumns().get(column) != null) {
            if (!database.supportsNotNullConstraintNames()) {
                buffer.append(" NOT NULL");
            } else {
                /* Determine if the NOT NULL constraint has a name. */
                NotNullConstraint nnConstraintForThisColumn = statement.getNotNullColumns().get(column);
                String nncName = StringUtil.trimToNull(nnConstraintForThisColumn.getConstraintName());
                if (nncName == null) {
                    buffer.append(" NOT NULL");
                } else {
                    buffer.append(" CONSTRAINT ");
                    buffer.append(database.escapeConstraintName(nncName));
                    buffer.append(" NOT NULL");
                }
                if (!nnConstraintForThisColumn.shouldValidateNullable()) {
                    if (database instanceof OracleDatabase) {
                        buffer.append(" ENABLE NOVALIDATE ");
                    }
                }
            }
        // does the DB support constraint names?
        } else {
            if (columnType != null && ((database instanceof SybaseDatabase) || (database instanceof SybaseASADatabase) || (database instanceof MySQLDatabase) || ((database instanceof MSSQLDatabase) && columnType.toString().toLowerCase().contains("timestamp")))) {
                buffer.append(" NULL");
            }
        // Do we need to specify NULL explicitly?
        }
        if ((database instanceof MySQLDatabase) && (statement.getColumnRemarks(column) != null)) {
            buffer.append(" COMMENT '" + database.escapeStringForDatabase(statement.getColumnRemarks(column)) + "'");
        }
        if (columnIterator.hasNext()) {
            buffer.append(", ");
        }
    }
    buffer.append(",");
    if (!((database instanceof SQLiteDatabase) && isSinglePrimaryKeyColumn && isPrimaryKeyAutoIncrement)) {
        if ((statement.getPrimaryKeyConstraint() != null) && !statement.getPrimaryKeyConstraint().getColumns().isEmpty()) {
            if (database.supportsPrimaryKeyNames()) {
                String pkName = StringUtil.trimToNull(statement.getPrimaryKeyConstraint().getConstraintName());
                if (pkName == null) {
                    // TODO ORA-00972: identifier is too long
                    // If tableName lenght is more then 28 symbols
                    // then generated pkName will be incorrect
                    pkName = database.generatePrimaryKeyName(statement.getTableName());
                }
                if (pkName != null) {
                    buffer.append(" CONSTRAINT ");
                    buffer.append(database.escapeConstraintName(pkName));
                }
            }
            buffer.append(" PRIMARY KEY (");
            buffer.append(database.escapeColumnNameList(StringUtil.join(statement.getPrimaryKeyConstraint().getColumns(), ", ")));
            buffer.append(")");
            // Setting up table space for PK's index if it exist
            if (((database instanceof OracleDatabase) || (database instanceof PostgresDatabase)) && (statement.getPrimaryKeyConstraint().getTablespace() != null)) {
                buffer.append(" USING INDEX TABLESPACE ");
                buffer.append(statement.getPrimaryKeyConstraint().getTablespace());
            }
            buffer.append(!statement.getPrimaryKeyConstraint().shouldValidatePrimaryKey() ? " ENABLE NOVALIDATE " : "");
            if (database.supportsInitiallyDeferrableColumns()) {
                if (statement.getPrimaryKeyConstraint().isInitiallyDeferred()) {
                    buffer.append(" INITIALLY DEFERRED");
                }
                if (statement.getPrimaryKeyConstraint().isDeferrable()) {
                    buffer.append(" DEFERRABLE");
                }
            }
            buffer.append(",");
        }
    }
    for (ForeignKeyConstraint fkConstraint : statement.getForeignKeyConstraints()) {
        if (!(database instanceof InformixDatabase)) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName()));
        }
        String referencesString = fkConstraint.getReferences();
        buffer.append(" FOREIGN KEY (").append(database.escapeColumnName(statement.getCatalogName(), statement.getSchemaName(), statement.getTableName(), fkConstraint.getColumn())).append(") REFERENCES ");
        if (referencesString != null) {
            if (!referencesString.contains(".") && (database.getDefaultSchemaName() != null) && database.getOutputDefaultSchema()) {
                referencesString = database.escapeObjectName(database.getDefaultSchemaName(), Schema.class) + "." + referencesString;
            }
            buffer.append(referencesString);
        } else {
            buffer.append(database.escapeObjectName(fkConstraint.getReferencedTableCatalogName(), fkConstraint.getReferencedTableSchemaName(), fkConstraint.getReferencedTableName(), Table.class)).append("(").append(database.escapeColumnNameList(fkConstraint.getReferencedColumnNames())).append(")");
        }
        if (fkConstraint.isDeleteCascade()) {
            buffer.append(" ON DELETE CASCADE");
        }
        if ((database instanceof InformixDatabase)) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(fkConstraint.getForeignKeyName()));
        }
        if (fkConstraint.isInitiallyDeferred()) {
            buffer.append(" INITIALLY DEFERRED");
        }
        if (fkConstraint.isDeferrable()) {
            buffer.append(" DEFERRABLE");
        }
        if (database instanceof OracleDatabase) {
            buffer.append(!fkConstraint.shouldValidateForeignKey() ? " ENABLE NOVALIDATE " : "");
        }
        buffer.append(",");
    }
    /*
         * In the current syntax, UNIQUE constraints can only be set per column on table creation.
         * To alleviate this problem we combine the columns of unique constraints that have the same name.
         */
    LinkedHashMap<String, UniqueConstraint> namedUniqueConstraints = new LinkedHashMap<>();
    List<UniqueConstraint> unnamedUniqueConstraints = new LinkedList<>();
    for (UniqueConstraint uniqueConstraint : statement.getUniqueConstraints()) {
        if (uniqueConstraint.getConstraintName() == null) {
            // Only combine uniqueConstraints that have a name.
            unnamedUniqueConstraints.add(uniqueConstraint);
        } else {
            String constraintName = uniqueConstraint.getConstraintName();
            UniqueConstraint existingConstraint = namedUniqueConstraints.get(constraintName);
            if (existingConstraint != null) {
                if (uniqueConstraint.shouldValidateUnique()) {
                    // if validateUnique = true on only one column, make sure it is true
                    existingConstraint.setValidateUnique(true);
                }
                existingConstraint.getColumns().addAll(uniqueConstraint.getColumns());
            } else {
                // if we haven't seen the constraint before put it in the map.
                namedUniqueConstraints.put(constraintName, uniqueConstraint);
            }
        }
    }
    unnamedUniqueConstraints.addAll(namedUniqueConstraints.values());
    for (UniqueConstraint uniqueConstraint : unnamedUniqueConstraints) {
        if (uniqueConstraint.getConstraintName() != null) {
            buffer.append(" CONSTRAINT ");
            buffer.append(database.escapeConstraintName(uniqueConstraint.getConstraintName()));
        }
        buffer.append(" UNIQUE (");
        buffer.append(database.escapeColumnNameList(StringUtil.join(uniqueConstraint.getColumns(), ", ")));
        buffer.append(")");
        if (database instanceof OracleDatabase) {
            buffer.append(!uniqueConstraint.shouldValidateUnique() ? " ENABLE NOVALIDATE " : "");
        }
        buffer.append(",");
    }
    /*
         * Here, the list of columns and constraints in the form
         * ( column1, ..., columnN, constraint1, ..., constraintN,
         * ends. We cannot leave an expression like ", )", so we remove the last comma.
         */
    String sql = buffer.toString().replaceFirst(",\\s*$", "") + ")";
    if ((database instanceof MySQLDatabase) && (mysqlTableOptionStartWith != null)) {
        Scope.getCurrentScope().getLog(getClass()).info("[MySQL] Using last startWith statement (" + mysqlTableOptionStartWith.toString() + ") as table option.");
        sql += " " + ((MySQLDatabase) database).getTableOptionAutoIncrementStartWithClause(mysqlTableOptionStartWith);
    }
    if ((statement.getTablespace() != null) && database.supportsTablespaces()) {
        if ((database instanceof MSSQLDatabase) || (database instanceof SybaseASADatabase)) {
            sql += " ON " + statement.getTablespace();
        } else if ((database instanceof AbstractDb2Database) || (database instanceof InformixDatabase)) {
            sql += " IN " + statement.getTablespace();
        } else {
            sql += " TABLESPACE " + statement.getTablespace();
        }
    }
    if ((database instanceof MySQLDatabase) && (statement.getRemarks() != null)) {
        sql += " COMMENT='" + database.escapeStringForDatabase(statement.getRemarks()) + "' ";
    }
    additionalSql.add(0, new UnparsedSql(sql, getAffectedTable(statement)));
    return additionalSql.toArray(new Sql[additionalSql.size()]);
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) AbstractDb2Database(liquibase.database.core.AbstractDb2Database) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) Schema(liquibase.structure.core.Schema) UniqueConstraint(liquibase.statement.UniqueConstraint) InformixDatabase(liquibase.database.core.InformixDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) NotNullConstraint(liquibase.statement.NotNullConstraint) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) UnparsedSql(liquibase.sql.UnparsedSql) MySQLDatabase(liquibase.database.core.MySQLDatabase) Sequence(liquibase.structure.core.Sequence) ForeignKey(liquibase.structure.core.ForeignKey) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) Db2zDatabase(liquibase.database.core.Db2zDatabase) BigInteger(java.math.BigInteger) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with ForeignKeyConstraint

use of liquibase.statement.ForeignKeyConstraint in project liquibase by liquibase.

the class AddColumnGeneratorSQLite method generateSql.

@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    // Workaround implemented by replacing a table with a new one (duplicate)
    // with a new column added
    final List<AddColumnStatement> columns = new ArrayList<>(statement.getColumns());
    if (columns.size() == 0) {
        columns.add(statement);
    }
    Set<String> newColumnNames = columns.stream().map(AddColumnStatement::getColumnName).collect(Collectors.toSet());
    Sql[] generatedSqls;
    SQLiteDatabase.AlterTableVisitor alterTableVisitor = new SQLiteDatabase.AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            ColumnConfig[] columnConfigs = new ColumnConfig[columns.size()];
            int i = 0;
            for (AddColumnStatement column : columns) {
                ColumnConfig newColumn = new ColumnConfig();
                newColumn.setName(column.getColumnName());
                newColumn.setType(column.getColumnType());
                newColumn.setAutoIncrement(column.isAutoIncrement());
                ConstraintsConfig constraintsConfig = new ConstraintsConfig();
                if (column.isPrimaryKey()) {
                    constraintsConfig.setPrimaryKey(true);
                }
                if (column.isNullable()) {
                    constraintsConfig.setNullable(true);
                }
                if (column.isUnique()) {
                    constraintsConfig.setUnique(true);
                }
                newColumn.setConstraints(constraintsConfig);
                for (ColumnConstraint constraint : column.getConstraints()) {
                    if (constraint instanceof ForeignKeyConstraint) {
                        final ForeignKeyConstraint fkConstraint = (ForeignKeyConstraint) constraint;
                        constraintsConfig.setReferencedTableCatalogName(fkConstraint.getReferencedTableCatalogName());
                        constraintsConfig.setReferencedTableSchemaName(fkConstraint.getReferencedTableSchemaName());
                        constraintsConfig.setReferencedTableName(fkConstraint.getReferencedTableName());
                        constraintsConfig.setReferencedColumnNames(fkConstraint.getReferencedColumnNames());
                        constraintsConfig.setReferences(fkConstraint.getReferences());
                        constraintsConfig.setForeignKeyName(fkConstraint.getForeignKeyName());
                        if (fkConstraint.isDeleteCascade()) {
                            constraintsConfig.setDeleteCascade(true);
                        }
                    }
                }
                columnConfigs[i++] = newColumn;
            }
            return columnConfigs;
        }

        @Override
        public boolean copyThisColumn(ColumnConfig column) {
            return !newColumnNames.contains(column.getName());
        }

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    final String catalogName = columns.get(0).getCatalogName();
    final String schemaName = columns.get(0).getSchemaName();
    final String tableName = columns.get(0).getTableName();
    generatedSqls = SQLiteDatabase.getAlterTableSqls(database, alterTableVisitor, catalogName, schemaName, tableName);
    return generatedSqls;
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) ColumnConstraint(liquibase.statement.ColumnConstraint) Sql(liquibase.sql.Sql) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ColumnConstraint(liquibase.statement.ColumnConstraint) ConstraintsConfig(liquibase.change.ConstraintsConfig) AddColumnStatement(liquibase.statement.core.AddColumnStatement)

Aggregations

Sql (liquibase.sql.Sql)3 ForeignKeyConstraint (liquibase.statement.ForeignKeyConstraint)3 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)2 UnparsedSql (liquibase.sql.UnparsedSql)2 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)2 UniqueConstraint (liquibase.statement.UniqueConstraint)2 Schema (liquibase.structure.core.Schema)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 ColumnConfig (liquibase.change.ColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)1 Db2zDatabase (liquibase.database.core.Db2zDatabase)1 InformixDatabase (liquibase.database.core.InformixDatabase)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)1