Search in sources :

Example 11 with Index

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

the class RenameColumnChange method generateStatementsForSQLiteDatabase.

private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
    // SQLite does not support this ALTER TABLE operation until now.
    // For more information see: http://www.sqlite.org/omitted.html.
    // This is a small work around...
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    // define alter table logic
    AlterTableVisitor rename_alter_visitor = new AlterTableVisitor() {

        @Override
        public ColumnConfig[] getColumnsToAdd() {
            return new ColumnConfig[0];
        }

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

        @Override
        public boolean createThisColumn(ColumnConfig column) {
            if (column.getName().equals(getOldColumnName())) {
                column.setName(getNewColumnName());
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            if (index.getColumnNames().contains(getOldColumnName())) {
                Iterator<Column> columnIterator = index.getColumns().iterator();
                while (columnIterator.hasNext()) {
                    Column column = columnIterator.next();
                    if (column.getName().equals(getOldColumnName())) {
                        columnIterator.remove();
                        break;
                    }
                }
                index.addColumn(new Column(getNewColumnName()).setRelation(index.getTable()));
            }
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        System.err.println(e);
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Column(liquibase.structure.core.Column) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 12 with Index

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

the class CreateIndexChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    ChangeStatus result = new ChangeStatus();
    try {
        Index example = new Index(getIndexName(), getCatalogName(), getSchemaName(), getTableName());
        if (getColumns() != null) {
            for (ColumnConfig column : getColumns()) {
                example.addColumn(new Column(column));
            }
        }
        Index snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
        result.assertComplete(snapshot != null, "Index does not exist");
        if (snapshot != null) {
            if (isUnique() != null) {
                result.assertCorrect(isUnique().equals(snapshot.isUnique()), "Unique does not match");
            }
        }
        return result;
    } catch (Exception e) {
        return result.unknown(e);
    }
}
Also used : Column(liquibase.structure.core.Column) Index(liquibase.structure.core.Index)

Aggregations

Index (liquibase.structure.core.Index)12 ArrayList (java.util.ArrayList)7 Column (liquibase.structure.core.Column)6 SqlStatement (liquibase.statement.SqlStatement)5 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)4 Change (liquibase.change.Change)3 AddColumnConfig (liquibase.change.AddColumnConfig)2 CreateIndexChange (liquibase.change.core.CreateIndexChange)2 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)2 Schema (liquibase.structure.core.Schema)2 Table (liquibase.structure.core.Table)2 UniqueConstraint (liquibase.structure.core.UniqueConstraint)2 List (java.util.List)1 ColumnConfig (liquibase.change.ColumnConfig)1 AddUniqueConstraintChange (liquibase.change.core.AddUniqueConstraintChange)1 DropIndexChange (liquibase.change.core.DropIndexChange)1 DropUniqueConstraintChange (liquibase.change.core.DropUniqueConstraintChange)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 Difference (liquibase.diff.Difference)1