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);
}
}
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()]);
}
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 };
// }
}
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 };
}
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 };
}
Aggregations