Search in sources :

Example 36 with DatabaseObject

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

the class YamlSnapshotSerializer method toMap.

// @Override
// public String serialize(LiquibaseSerializable object, boolean pretty) {
// if (object instanceof DatabaseObject) {
// if (alreadySerializingObject) {
// return ((DatabaseObject) object).getObjectTypeName()+"#"+((DatabaseObject) object).getSnapshotId();
// } else {
// alreadySerializingObject = true;
// String string = super.serialize(object, pretty);
// alreadySerializingObject = false;
// return string;
// }
// }
// return super.serialize(object, pretty);
// }
@Override
protected Object toMap(final LiquibaseSerializable object) {
    if (object instanceof DatabaseObject) {
        if (object instanceof Column && (BooleanUtil.isTrue(((Column) object).getDescending()) || BooleanUtil.isTrue(((Column) object).getComputed()))) {
            // not really a "real" column that has a snapshot to reference, just serialize it
            return super.toMap(object);
        } else if (alreadySerializingObject) {
            String snapshotId = ((DatabaseObject) object).getSnapshotId();
            if (snapshotId == null) {
                String name = ((DatabaseObject) object).getName();
                Object table = ((DatabaseObject) object).getAttribute("table", Object.class);
                if (table == null) {
                    table = ((DatabaseObject) object).getAttribute("relation", Object.class);
                }
                if (table != null) {
                    name = table.toString() + "." + name;
                }
                if (((DatabaseObject) object).getSchema() != null) {
                    name = ((DatabaseObject) object).getSchema().toString() + "." + name;
                }
                throw new UnexpectedLiquibaseException("Found a null snapshotId for " + StringUtil.lowerCaseFirst(object.getClass().getSimpleName()) + " " + name);
            }
            return ((DatabaseObject) object).getClass().getName() + "#" + snapshotId;
        } else {
            alreadySerializingObject = true;
            Object map = super.toMap(object);
            alreadySerializingObject = false;
            return map;
        }
    }
    if (object instanceof DatabaseObjectCollection) {
        SortedMap<String, Object> returnMap = new TreeMap<>();
        for (Map.Entry<Class<? extends DatabaseObject>, Set<? extends DatabaseObject>> entry : ((DatabaseObjectCollection) object).toMap().entrySet()) {
            ArrayList value = new ArrayList(entry.getValue());
            Collections.sort(value, new DatabaseObjectComparator());
            returnMap.put(entry.getKey().getName(), value);
        }
        return returnMap;
    }
    return super.toMap(object);
}
Also used : DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectComparator(liquibase.structure.DatabaseObjectComparator) DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 37 with DatabaseObject

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

the class DatabaseObjectFactory method parseTypes.

public Set<Class<? extends DatabaseObject>> parseTypes(String typesString) {
    if (StringUtil.trimToNull(typesString) == null) {
        return getStandardTypes();
    } else {
        Set<Class<? extends DatabaseObject>> returnSet = new HashSet<>();
        Set<String> typesToInclude = new HashSet<>(Arrays.asList(typesString.toLowerCase().split("\\s*,\\s*")));
        Set<String> typesNotFound = new HashSet<>(typesToInclude);
        for (DatabaseObject object : Scope.getCurrentScope().getServiceLocator().findInstances(DatabaseObject.class)) {
            Class<? extends DatabaseObject> clazz = object.getClass();
            if (typesToInclude.contains(clazz.getSimpleName().toLowerCase()) || typesToInclude.contains(clazz.getSimpleName().toLowerCase() + "s") || // like indexes
            typesToInclude.contains(clazz.getSimpleName().toLowerCase() + "es")) {
                returnSet.add(clazz);
                typesNotFound.remove(clazz.getSimpleName().toLowerCase());
                typesNotFound.remove(clazz.getSimpleName().toLowerCase() + "s");
                typesNotFound.remove(clazz.getSimpleName().toLowerCase() + "es");
            }
        }
        if (!typesNotFound.isEmpty()) {
            throw new UnexpectedLiquibaseException("Unknown snapshot type(s) " + StringUtil.join(typesNotFound, ", "));
        }
        return returnSet;
    }
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) HashSet(java.util.HashSet)

Example 38 with DatabaseObject

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

the class InternalDiffCommandStep method createReferenceSnapshot.

protected DatabaseSnapshot createReferenceSnapshot(CommandScope commandScope) throws DatabaseException, InvalidExampleException {
    CatalogAndSchema[] schemas;
    CompareControl compareControl = commandScope.getArgumentValue(COMPARE_CONTROL_ARG);
    Database targetDatabase = commandScope.getArgumentValue(TARGET_DATABASE_ARG);
    Database referenceDatabase = commandScope.getArgumentValue(REFERENCE_DATABASE_ARG);
    SnapshotControl snapshotControl = commandScope.getArgumentValue(REFERENCE_SNAPSHOT_CONTROL_ARG);
    Class<? extends DatabaseObject>[] snapshotTypes = commandScope.getArgumentValue(SNAPSHOT_TYPES_ARG);
    ObjectChangeFilter objectChangeFilter = commandScope.getArgumentValue(OBJECT_CHANGE_FILTER_ARG);
    SnapshotListener snapshotListener = commandScope.getArgumentValue(SNAPSHOT_LISTENER_ARG);
    if ((compareControl == null) || (compareControl.getSchemaComparisons() == null)) {
        schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
    } else {
        schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
        int i = 0;
        for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
            CatalogAndSchema schema;
            if (referenceDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(referenceDatabase.getDefaultCatalogName(), comparison.getReferenceSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getReferenceSchema().getSchemaName(), comparison.getReferenceSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(referenceDatabase, objectChangeFilter, snapshotTypes);
    }
    if (snapshotListener != null) {
        snapshotControl.setSnapshotListener(snapshotListener);
    }
    ObjectQuotingStrategy originalStrategy = referenceDatabase.getObjectQuotingStrategy();
    try {
        referenceDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, referenceDatabase, snapshotControl);
    } finally {
        referenceDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : ObjectChangeFilter(liquibase.diff.output.ObjectChangeFilter) CatalogAndSchema(liquibase.CatalogAndSchema) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 39 with DatabaseObject

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

the class InternalDiffCommandStep method createTargetSnapshot.

protected DatabaseSnapshot createTargetSnapshot(CommandScope commandScope) throws DatabaseException, InvalidExampleException {
    CatalogAndSchema[] schemas;
    CompareControl compareControl = commandScope.getArgumentValue(COMPARE_CONTROL_ARG);
    Database targetDatabase = commandScope.getArgumentValue(TARGET_DATABASE_ARG);
    SnapshotControl snapshotControl = commandScope.getArgumentValue(TARGET_SNAPSHOT_CONTROL_ARG);
    Class<? extends DatabaseObject>[] snapshotTypes = commandScope.getArgumentValue(SNAPSHOT_TYPES_ARG);
    SnapshotListener snapshotListener = commandScope.getArgumentValue(SNAPSHOT_LISTENER_ARG);
    if ((compareControl == null) || (compareControl.getSchemaComparisons() == null)) {
        schemas = new CatalogAndSchema[] { targetDatabase.getDefaultSchema() };
    } else {
        schemas = new CatalogAndSchema[compareControl.getSchemaComparisons().length];
        int i = 0;
        for (CompareControl.SchemaComparison comparison : compareControl.getSchemaComparisons()) {
            CatalogAndSchema schema;
            if (targetDatabase.supportsSchemas()) {
                schema = new CatalogAndSchema(targetDatabase.getDefaultCatalogName(), comparison.getComparisonSchema().getSchemaName());
            } else {
                schema = new CatalogAndSchema(comparison.getComparisonSchema().getSchemaName(), comparison.getComparisonSchema().getSchemaName());
            }
            schemas[i++] = schema;
        }
    }
    if (snapshotControl == null) {
        snapshotControl = new SnapshotControl(targetDatabase, snapshotTypes);
    }
    if (snapshotListener != null) {
        snapshotControl.setSnapshotListener(snapshotListener);
    }
    ObjectQuotingStrategy originalStrategy = targetDatabase.getObjectQuotingStrategy();
    try {
        targetDatabase.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
        return SnapshotGeneratorFactory.getInstance().createSnapshot(schemas, targetDatabase, snapshotControl);
    } finally {
        targetDatabase.setObjectQuotingStrategy(originalStrategy);
    }
}
Also used : Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) CatalogAndSchema(liquibase.CatalogAndSchema) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy)

Example 40 with DatabaseObject

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

the class StandardLockService method destroy.

@Override
public void destroy() throws DatabaseException {
    try {
        // 
        // This code now uses the ChangeGeneratorFactory to
        // allow extension code to be called in order to
        // delete the changelog lock table.
        // 
        // To implement the extension, you will need to override:
        // DropTableStatement
        // DropTableChange
        // DropTableGenerator
        // 
        // 
        DatabaseObject example = new Table().setName(database.getDatabaseChangeLogLockTableName()).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) DatabaseObject(liquibase.structure.DatabaseObject) DiffOutputControl(liquibase.diff.output.DiffOutputControl) Change(liquibase.change.Change) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

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