@Override public int compareTo(Object other) { Index o = (Index) other; int returnValue = this.getTable().getName().compareTo(o.getTable().getName()); if (returnValue == 0) { String thisName = StringUtils.trimToEmpty(this.getName()); String oName = StringUtils.trimToEmpty(o.getName()); returnValue = thisName.compareTo(oName); } // We should not have two indexes that have the same name and tablename /*if (returnValue == 0) { returnValue = this.getColumnName().compareTo(o.getColumnName()); }*/ return returnValue; }
/** * Generates the {@link Index} example (taken from {@link IndexExistsPrecondition}). * * @param database the database instance. * @param schema the schema instance. * @param tableName the table name of the index. * @return the index example. */ protected Index getIndexExample( final Database database, final Schema schema, final String tableName) { final Index example = new Index(); 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 (final String columnName : getColumnNames().split("\\s*,\\s*")) { final Column column = new Column(database.correctObjectName(columnName, Column.class)); example.getColumns().add(column); } } return example; }
public static List<SqlStatement> getAlterTableStatements( AlterTableVisitor alterTableVisitor, Database database, String catalogName, String schemaName, String tableName) throws DatabaseException { DatabaseSnapshot snapshot = null; // todo List<SqlStatement> statements = new ArrayList<SqlStatement>(); Table table = null; try { table = SnapshotGeneratorFactory.getInstance() .createSnapshot( (Table) new Table().setName(tableName).setSchema(new Schema(new Catalog(null), null)), database); } catch (InvalidExampleException e) { throw new UnexpectedLiquibaseException(e); } List<ColumnConfig> createColumns = new Vector<ColumnConfig>(); List<ColumnConfig> copyColumns = new Vector<ColumnConfig>(); if (table != null) { for (Column column : table.getColumns()) { ColumnConfig new_column = new ColumnConfig(column); if (alterTableVisitor.createThisColumn(new_column)) { createColumns.add(new_column); } ColumnConfig copy_column = new ColumnConfig(column); if (alterTableVisitor.copyThisColumn(copy_column)) { copyColumns.add(copy_column); } } } for (ColumnConfig column : alterTableVisitor.getColumnsToAdd()) { if (alterTableVisitor.createThisColumn(column)) { createColumns.add(column); } if (alterTableVisitor.copyThisColumn(column)) { copyColumns.add(column); } } List<Index> newIndices = new Vector<Index>(); for (Index index : new ArrayList< Index>()) { // todo SnapshotGeneratorFactory.getInstance().getGenerator(Index.class, // database).get(new Schema(new Catalog(null), schemaName), database)) { if (index.getTable().getName().equalsIgnoreCase(tableName)) { if (alterTableVisitor.createThisIndex(index)) { newIndices.add(index); } } } // rename table String temp_table_name = tableName + "_temporary"; statements.add(new RenameTableStatement(catalogName, schemaName, tableName, temp_table_name)); // create temporary table CreateTableChange ct_change_tmp = new CreateTableChange(); ct_change_tmp.setSchemaName(schemaName); ct_change_tmp.setTableName(tableName); for (ColumnConfig column : createColumns) { ct_change_tmp.addColumn(column); } statements.addAll(Arrays.asList(ct_change_tmp.generateStatements(database))); // copy rows to temporary table statements.add(new CopyRowsStatement(temp_table_name, tableName, copyColumns)); // delete original table statements.add(new DropTableStatement(catalogName, schemaName, temp_table_name, false)); // validate indices statements.add(new ReindexStatement(catalogName, schemaName, tableName)); // add remaining indices for (Index index_config : newIndices) { statements.add( new CreateIndexStatement( index_config.getName(), catalogName, schemaName, tableName, index_config.isUnique(), index_config.getAssociatedWithAsString(), index_config.getColumns().toArray(new String[index_config.getColumns().size()]))); } return statements; }
@Override public Change[] fixMissing( DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { List<Change> returnList = new ArrayList<Change>(); UniqueConstraint uc = (UniqueConstraint) missingObject; if (uc.getTable() == null) { return null; } AddUniqueConstraintChange change = new AddUniqueConstraintChange(); change.setTableName(uc.getTable().getName()); if (uc.getBackingIndex() != null && control.getIncludeTablespace()) { change.setTablespace(uc.getBackingIndex().getTablespace()); } if (control.getIncludeCatalog()) { change.setCatalogName(uc.getTable().getSchema().getCatalogName()); } if (control.getIncludeSchema()) { change.setSchemaName(uc.getTable().getSchema().getName()); } change.setConstraintName(uc.getName()); change.setColumnNames(uc.getColumnNames()); change.setDeferrable(uc.isDeferrable() ? Boolean.TRUE : null); change.setInitiallyDeferred(uc.isInitiallyDeferred() ? Boolean.TRUE : null); change.setDisabled(uc.isDisabled() ? Boolean.TRUE : null); if (comparisonDatabase instanceof OracleDatabase) { Index backingIndex = uc.getBackingIndex(); if (backingIndex != null && backingIndex.getName() != null) { Change[] changes = ChangeGeneratorFactory.getInstance() .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase); if (changes != null) { returnList.addAll(Arrays.asList(changes)); change.setForIndexName(backingIndex.getName()); Schema schema = backingIndex.getSchema(); if (schema != null) { if (control.getIncludeCatalog()) { change.setForIndexCatalogName(schema.getCatalogName()); } if (control.getIncludeSchema()) { change.setForIndexSchemaName(schema.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.setAlreadyHandledMissing(exampleIndex); // } else { control.setAlreadyHandledMissing(backingIndex); // } returnList.add(change); return returnList.toArray(new Change[returnList.size()]); }
@Override public Change[] fixMissing( DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) { List<Change> returnList = new ArrayList<Change>(); PrimaryKey pk = (PrimaryKey) missingObject; AddPrimaryKeyChange change = new AddPrimaryKeyChange(); 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()); change.setColumnNames(pk.getColumnNames()); if (control.getIncludeTablespace()) { change.setTablespace(pk.getTablespace()); } if (referenceDatabase instanceof MSSQLDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && !pk.getBackingIndex().getClustered()) { change.setClustered(false); } if (comparisonDatabase instanceof OracleDatabase || (comparisonDatabase instanceof DB2Database && pk.getBackingIndex() != null && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) { Index backingIndex = pk.getBackingIndex(); if (backingIndex != null && backingIndex.getName() != null) { try { if (!control.getIncludeCatalog() && !control.getIncludeSchema()) { CatalogAndSchema schema = comparisonDatabase.getDefaultSchema().customize(comparisonDatabase); backingIndex .getTable() .setSchema( schema.getCatalogName(), schema .getSchemaName()); // set table schema so it is found in the correct schema } if (referenceDatabase.equals(comparisonDatabase) || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) { Change[] fixes = ChangeGeneratorFactory.getInstance() .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase); if (fixes != null) { for (Change fix : fixes) { if (fix != null) { returnList.add(fix); } } } } } catch (Exception e) { throw new UnexpectedLiquibaseException(e); } change.setForIndexName(backingIndex.getName()); Schema schema = backingIndex.getSchema(); if (schema != null) { if (control.getIncludeCatalog()) { change.setForIndexCatalogName(schema.getCatalogName()); } if (control.getIncludeSchema()) { change.setForIndexSchemaName(schema.getName()); } } } } control.setAlreadyHandledMissing(pk.getBackingIndex()); returnList.add(change); return returnList.toArray(new Change[returnList.size()]); }