Search in sources :

Example 1 with Index

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

the class IndexExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        Schema schema = new Schema(getCatalogName(), getSchemaName());
        Index example = new Index();
        String tableName = StringUtils.trimToNull(getTableName());
        if (tableName != null) {
            example.setTable((Table) new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
        }
        example.setName(database.correctObjectName(getIndexName(), Index.class));
        if (StringUtils.trimToNull(getColumnNames()) != null) {
            for (String column : getColumnNames().split("\\s*,\\s*")) {
                example.addColumn(new Column(database.correctObjectName(column, Column.class)));
            }
        }
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            String name = "";
            if (getIndexName() != null) {
                name += database.escapeObjectName(getIndexName(), Index.class);
            }
            if (tableName != null) {
                name += " on " + database.escapeObjectName(getTableName(), Table.class);
                if (StringUtils.trimToNull(getColumnNames()) != null) {
                    name += " columns " + getColumnNames();
                }
            }
            throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this);
        }
    } catch (Exception e) {
        if (e instanceof PreconditionFailedException) {
            throw (((PreconditionFailedException) e));
        }
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) Index(liquibase.structure.core.Index)

Example 2 with Index

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

the class AddNotNullConstraintChange 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>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), getDefaultNullValue()).setWhereClause(getColumnName() + " IS NULL"));
    }
    //		// ... test if column contains NULL values
    //		if (defaultNullValue == null) {
    //			List<Map> null_rows = null;
    //			try {
    //				null_rows = database.getExecutor().
    //					queryForList(new RawSqlStatement(
    //						"SELECT * FROM `"+
    //						database.escapeTableName(getSchemaName(), getTableName())+
    //						"` WHERE `"+getColumnName()+"` IS NULL;"));
    //			} catch (DatabaseException e) {
    //				e.printStackTrace();
    //			}
    //    		if (null_rows.size()>0) {
    //    			throw new UnsupportedChangeException(
    //    					"Failed to add a Not-Null-Constraint because " +
    //    					"some values are null. Use the " +
    //    					"defaultNullValue attribute to define default " +
    //    					"values for the existing null values.");
    //    		}
    //    	}
    // 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(getColumnName())) {
                column.getConstraints().setNullable(false);
            }
            return true;
        }

        @Override
        public boolean createThisIndex(Index index) {
            return true;
        }
    };
    try {
        // alter table
        statements.addAll(SQLiteDatabase.getAlterTableStatements(rename_alter_visitor, database, getCatalogName(), getSchemaName(), getTableName()));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) AlterTableVisitor(liquibase.database.core.SQLiteDatabase.AlterTableVisitor) ArrayList(java.util.ArrayList) Index(liquibase.structure.core.Index)

Example 3 with Index

use of liquibase.structure.core.Index 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 Index

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

the class UnexpectedTableChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table unexpectedTable = (Table) unexpectedObject;
    DropTableChange change = new DropTableChange();
    change.setTableName(unexpectedTable.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(unexpectedTable.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(unexpectedTable.getSchema().getName());
    }
    for (Column column : unexpectedTable.getColumns()) {
        control.setAlreadyHandledUnexpected(column);
    }
    ;
    control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey());
    for (Index index : unexpectedTable.getIndexes()) {
        control.setAlreadyHandledUnexpected(index);
    }
    control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey());
    if (unexpectedTable.getPrimaryKey() != null) {
        control.setAlreadyHandledUnexpected(unexpectedTable.getPrimaryKey().getBackingIndex());
    }
    return new Change[] { change };
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) DropTableChange(liquibase.change.core.DropTableChange) Index(liquibase.structure.core.Index) Change(liquibase.change.Change) DropTableChange(liquibase.change.core.DropTableChange)

Example 5 with Index

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

the class UnexpectedUniqueConstraintChangeGenerator method fixUnexpected.

@Override
public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    UniqueConstraint uc = (UniqueConstraint) unexpectedObject;
    if (uc.getRelation() == null) {
        return null;
    }
    DropUniqueConstraintChange change = new DropUniqueConstraintChange();
    change.setTableName(uc.getRelation().getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(uc.getRelation().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(uc.getRelation().getSchema().getName());
    }
    change.setConstraintName(uc.getName());
    Index backingIndex = uc.getBackingIndex();
    // if (backingIndex == null) {
    // Index exampleIndex = new Index().setTable(uc.getTable());
    // for (String col : uc.getColumns()) {
    // exampleIndex.getColumns().add(col);
    // }
    // control.setAlreadyHandledUnexpected(exampleIndex);
    // } else {
    control.setAlreadyHandledUnexpected(backingIndex);
    return new Change[] { change };
}
Also used : DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) UniqueConstraint(liquibase.structure.core.UniqueConstraint) Index(liquibase.structure.core.Index) DropUniqueConstraintChange(liquibase.change.core.DropUniqueConstraintChange) Change(liquibase.change.Change)

Aggregations

Index (liquibase.structure.core.Index)19 Column (liquibase.structure.core.Column)8 ArrayList (java.util.ArrayList)7 Change (liquibase.change.Change)6 SqlStatement (liquibase.statement.SqlStatement)5 Table (liquibase.structure.core.Table)5 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)4 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)4 Schema (liquibase.structure.core.Schema)4 UniqueConstraint (liquibase.structure.core.UniqueConstraint)4 DatabaseException (liquibase.exception.DatabaseException)3 ColumnConfig (liquibase.change.ColumnConfig)2 DropIndexChange (liquibase.change.core.DropIndexChange)2 DropUniqueConstraintChange (liquibase.change.core.DropUniqueConstraintChange)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 Sql (liquibase.sql.Sql)2 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1