Search in sources :

Example 56 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class DatabaseSnapshot method include.

/**
     * Include the object described by the passed example object in this snapshot. Returns the object snapshot or null if the object does not exist in the database.
     * If the same object was returned by an earlier include() call, the same object instance will be returned.
     */
protected <T extends DatabaseObject> T include(T example) throws DatabaseException, InvalidExampleException {
    if (example == null) {
        return null;
    }
    if (database.isSystemObject(example)) {
        return null;
    }
    if (example instanceof Schema && example.getName() == null && (((Schema) example).getCatalog() == null || ((Schema) example).getCatalogName() == null)) {
        CatalogAndSchema catalogAndSchema = ((Schema) example).toCatalogAndSchema().customize(database);
        example = (T) new Schema(catalogAndSchema.getCatalogName(), catalogAndSchema.getSchemaName());
    }
    if (!snapshotControl.shouldInclude(example.getClass())) {
        return example;
    }
    T existing = get(example);
    if (existing != null) {
        return existing;
    }
    if (isKnownNull(example)) {
        return null;
    }
    SnapshotListener snapshotListener = snapshotControl.getSnapshotListener();
    SnapshotGeneratorChain chain = createGeneratorChain(example.getClass(), database);
    if (snapshotListener != null) {
        snapshotListener.willSnapshot(example, database);
    }
    T object = chain.snapshot(example, this);
    if (object == null) {
        Set<DatabaseObject> collection = knownNull.get(example.getClass());
        if (collection == null) {
            collection = new HashSet<DatabaseObject>();
            knownNull.put(example.getClass(), collection);
        }
        collection.add(example);
        if (example instanceof Schema) {
            LogFactory.getInstance().getLog().warning("Did not find schema '" + example + "' to snapshot");
        }
        if (example instanceof Catalog) {
            LogFactory.getInstance().getLog().warning("Did not find catalog '" + example + "' to snapshot");
        }
    } else {
        allFound.add(object);
        try {
            includeNestedObjects(object);
        } catch (InstantiationException e) {
            throw new UnexpectedLiquibaseException(e);
        } catch (IllegalAccessException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
    if (snapshotListener != null) {
        snapshotListener.finishedSnapshot(example, object, database);
    }
    return object;
}
Also used : CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseObject(liquibase.structure.DatabaseObject) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 57 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class ReflectionSerializer method getValue.

public Object getValue(Object object, String field) {
    if (!reflectionCache.containsKey(object.getClass())) {
        //fills cache
        getFields(object);
    }
    Map<String, Field> fieldsByName = reflectionCache.get(object.getClass());
    Field foundField = fieldsByName.get(field);
    try {
        if (foundField == null) {
            foundField = findField(object, field);
            foundField.setAccessible(true);
            fieldsByName.put(field, foundField);
        }
        return foundField.get(object);
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 58 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class ReflectionSerializer method getDataTypeClass.

public Class getDataTypeClass(Object object, String field) {
    try {
        Field foundField = findField(object, field);
        Type dataType = foundField.getGenericType();
        if (dataType instanceof Class) {
            return (Class) dataType;
        }
        if (dataType instanceof ParameterizedType) {
            return (Class) ((ParameterizedType) dataType).getRawType();
        }
        return Object.class;
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 59 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class FormattedSqlChangeLogSerializer method getTargetDatabase.

protected Database getTargetDatabase(ChangeSet changeSet) {
    String filePath = changeSet.getFilePath();
    if (filePath == null) {
        throw new UnexpectedLiquibaseException("You must specify the changelog file name as filename.DB_TYPE.sql. Example: changelog.mysql.sql");
    }
    Matcher matcher = fileNamePatter.matcher(filePath);
    if (!matcher.matches()) {
        throw new UnexpectedLiquibaseException("Serializing changelog as sql requires a file name in the format *.databaseType.sql. Example: changelog.h2.sql. Passed: " + filePath);
    }
    String shortName = matcher.replaceFirst("$1");
    Database database = DatabaseFactory.getInstance().getDatabase(shortName);
    if (database == null) {
        throw new UnexpectedLiquibaseException("Serializing changelog as sql requires a file name in the format *.databaseType.sql. Example: changelog.h2.sql. Unknown databaes type: " + shortName);
    }
    return database;
}
Also used : Database(liquibase.database.Database) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 60 with UnexpectedLiquibaseException

use of liquibase.exception.UnexpectedLiquibaseException in project liquibase by liquibase.

the class FormattedSqlChangeLogSerializer method serialize.

@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
    if (object instanceof ChangeSet) {
        StringBuilder builder = new StringBuilder();
        ChangeSet changeSet = (ChangeSet) object;
        Database database = getTargetDatabase(changeSet);
        String author = (changeSet.getAuthor()).replaceAll("\\s+", "_");
        author = author.replace("_(generated)", "");
        builder.append("--changeset ").append(author).append(":").append(changeSet.getId()).append("\n");
        for (Change change : changeSet.getChanges()) {
            Sql[] sqls = SqlGeneratorFactory.getInstance().generateSql(change.generateStatements(database), database);
            if (sqls != null) {
                for (Sql sql : sqls) {
                    builder.append(sql.toSql()).append(sql.getEndDelimiter()).append("\n");
                }
            }
        }
        return builder.toString();
    } else {
        throw new UnexpectedLiquibaseException("Cannot serialize object type: " + object.getClass().getName());
    }
}
Also used : Database(liquibase.database.Database) Change(liquibase.change.Change) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) Sql(liquibase.sql.Sql)

Aggregations

UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)75 DatabaseException (liquibase.exception.DatabaseException)12 IOException (java.io.IOException)10 Database (liquibase.database.Database)10 DatabaseObject (liquibase.structure.DatabaseObject)10 LiquibaseException (liquibase.exception.LiquibaseException)9 InvalidExampleException (liquibase.snapshot.InvalidExampleException)9 CatalogAndSchema (liquibase.CatalogAndSchema)8 InputStream (java.io.InputStream)7 ParsedNodeException (liquibase.parser.core.ParsedNodeException)7 SQLException (java.sql.SQLException)6 DatabaseFunction (liquibase.statement.DatabaseFunction)6 SqlStatement (liquibase.statement.SqlStatement)6 ArrayList (java.util.ArrayList)5 Matcher (java.util.regex.Matcher)5 Change (liquibase.change.Change)5 ColumnConfig (liquibase.change.ColumnConfig)5 Executor (liquibase.executor.Executor)5 Sql (liquibase.sql.Sql)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4