Search in sources :

Example 41 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class DiffToChangeLogTest method getOrderedOutputTypes_hasDependencies.

@Test
public void getOrderedOutputTypes_hasDependencies() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    Class<? extends DatabaseObject>[] typesArray = new Class[5];
    typesArray[0] = Schema.class;
    typesArray[1] = View.class;
    typesArray[2] = Catalog.class;
    typesArray[3] = Table.class;
    typesArray[4] = Column.class;
    SnapshotControl control = new SnapshotControl(database, typesArray);
    EmptyDatabaseSnapshot emptyDatabaseSnapshot = new EmptyDatabaseSnapshot(database, control);
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(emptyDatabaseSnapshot, emptyDatabaseSnapshot, new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        assertThat("There should be some types", orderedOutputTypes, hasSize(greaterThan(5)));
    }
    List<Class<? extends DatabaseObject>> unexpectedOrderedOutputTypes = obj.getOrderedOutputTypes(UnexpectedObjectChangeGenerator.class);
    assertThat("There should be some types", unexpectedOrderedOutputTypes, hasSize(7));
    List<Class<? extends DatabaseObject>> missingOrderedOutputTypes = obj.getOrderedOutputTypes(MissingObjectChangeGenerator.class);
    assertThat("There should be some types", missingOrderedOutputTypes, hasSize(6));
    List<Class<? extends DatabaseObject>> changedOrderedOutputTypes = obj.getOrderedOutputTypes(ChangedObjectChangeGenerator.class);
    assertThat("There should be some types", changedOrderedOutputTypes, hasSize(6));
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 42 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class DiffToChangeLogTest method getOrderedOutputTypes_isConsistent.

@Test
public void getOrderedOutputTypes_isConsistent() throws Exception {
    MySQLDatabase database = new MySQLDatabase();
    DiffToChangeLog obj = new DiffToChangeLog(new DiffResult(new EmptyDatabaseSnapshot(database), new EmptyDatabaseSnapshot(database), new CompareControl()), null);
    for (Class<? extends ChangeGenerator> type : new Class[] { UnexpectedObjectChangeGenerator.class, MissingObjectChangeGenerator.class, ChangedObjectChangeGenerator.class }) {
        List<Class<? extends DatabaseObject>> orderedOutputTypes = obj.getOrderedOutputTypes(type);
        for (int i = 0; i < 50; i++) {
            assertThat("Error checking " + type.getName(), orderedOutputTypes, contains(obj.getOrderedOutputTypes(type).toArray()));
        }
    }
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) DiffResult(liquibase.diff.DiffResult) Test(org.junit.Test)

Example 43 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class StandardChangeLogHistoryService method destroy.

@Override
public void destroy() throws DatabaseException {
    Database database = getDatabase();
    try {
        // 
        // This code now uses the ChangeGeneratorFactory to
        // allow extension code to be called in order to
        // delete the changelog table.
        // 
        // To implement the extension, you will need to override:
        // DropTableStatement
        // DropTableChange
        // DropTableGenerator
        // 
        // 
        DatabaseObject example = new Table().setName(database.getDatabaseChangeLogTableName()).setSchema(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName());
        if (SnapshotGeneratorFactory.getInstance().has(example, database)) {
            DatabaseObject table = SnapshotGeneratorFactory.getInstance().createSnapshot(example, database);
            DiffOutputControl diffOutputControl = new DiffOutputControl(true, true, false, null);
            Change[] change = ChangeGeneratorFactory.getInstance().fixUnexpected(table, diffOutputControl, database, database);
            SqlStatement[] sqlStatement = change[0].generateStatements(database);
            Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).execute(sqlStatement[0]);
        }
        reset();
    } catch (InvalidExampleException e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) InvalidExampleException(liquibase.snapshot.InvalidExampleException) Table(liquibase.structure.core.Table) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DB2Database(liquibase.database.core.DB2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) DatabaseObject(liquibase.structure.DatabaseObject) DiffOutputControl(liquibase.diff.output.DiffOutputControl) Change(liquibase.change.Change) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 44 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class StandardObjectChangeFilter method parseFilter.

protected void parseFilter(String filter) {
    filter = StringUtil.trimToNull(filter);
    if (filter == null) {
        return;
    }
    // first, split the string on commas to get the subfilters
    for (String subfilter : filter.split("\\s*,\\s*")) {
        // each subfilter can be either "objecttype:regex" or just "regex", so split on colon to decide
        String[] split = subfilter.split(":");
        if (split.length == 1) {
            filters.add(new Filter(null, Pattern.compile(split[0])));
        } else {
            String className = StringUtil.upperCaseFirst(split[0]);
            className = "liquibase.structure.core." + className;
            try {
                Class<DatabaseObject> clazz = (Class<DatabaseObject>) Class.forName(className);
                filters.add(new Filter(clazz, Pattern.compile(split[1])));
                catalogOrSchemaFilter |= "Catalog".equals(className) || "Schema".equals(className);
            } catch (ClassNotFoundException e) {
                throw new UnexpectedLiquibaseException(e);
            }
        }
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 45 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class UniqueConstraintComparator method findDifferences.

@Override
public ObjectDifferences findDifferences(DatabaseObject databaseObject1, DatabaseObject databaseObject2, Database accordingTo, CompareControl compareControl, DatabaseObjectComparatorChain chain, Set<String> exclude) {
    exclude.add("name");
    exclude.add("columns");
    exclude.add("backingIndex");
    ObjectDifferences differences = chain.findDifferences(databaseObject1, databaseObject2, accordingTo, compareControl, exclude);
    differences.compare("columns", databaseObject1, databaseObject2, new ObjectDifferences.CompareFunction() {

        @Override
        public boolean areEqual(Object referenceValue, Object compareToValue) {
            List<Column> referenceList = (List) referenceValue;
            List<Column> compareList = (List) compareToValue;
            if (referenceList.size() != compareList.size()) {
                return false;
            }
            for (int i = 0; i < referenceList.size(); i++) {
                if (!StringUtil.trimToEmpty((referenceList.get(i)).getName()).equalsIgnoreCase(StringUtil.trimToEmpty(compareList.get(i).getName()))) {
                    return false;
                }
            }
            return true;
        }
    });
    differences.compare("backingIndex", databaseObject1, databaseObject2, new ObjectDifferences.StandardCompareFunction(chain.getSchemaComparisons(), accordingTo));
    return differences;
}
Also used : ObjectDifferences(liquibase.diff.ObjectDifferences) DatabaseObject(liquibase.structure.DatabaseObject) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

DatabaseObject (liquibase.structure.DatabaseObject)47 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)14 CompareControl (liquibase.diff.compare.CompareControl)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 CatalogAndSchema (liquibase.CatalogAndSchema)7 DiffResult (liquibase.diff.DiffResult)6 ObjectDifferences (liquibase.diff.ObjectDifferences)6 ArrayList (java.util.ArrayList)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 Column (liquibase.structure.core.Column)5 Table (liquibase.structure.core.Table)5 HashSet (java.util.HashSet)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 DatabaseException (liquibase.exception.DatabaseException)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 SnapshotControl (liquibase.snapshot.SnapshotControl)4 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)4 List (java.util.List)3