Search in sources :

Example 11 with UnexpectedLiquibaseException

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

the class StandardLockService method releaseLock.

@Override
public void releaseLock() throws LockException {
    ObjectQuotingStrategy incomingQuotingStrategy = null;
    if (this.quotingStrategy != null) {
        incomingQuotingStrategy = database.getObjectQuotingStrategy();
        database.setObjectQuotingStrategy(this.quotingStrategy);
    }
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
        if (this.hasDatabaseChangeLogLockTable()) {
            executor.comment("Release Database Lock");
            database.rollback();
            int updatedRows = executor.update(new UnlockDatabaseChangeLogStatement());
            if (updatedRows == -1 && database instanceof MSSQLDatabase) {
                LogFactory.getLogger().debug("Database did not return a proper row count (Might have NOCOUNT enabled.)");
                database.rollback();
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(new UnlockDatabaseChangeLogStatement(), database);
                if (sql.length != 1) {
                    throw new UnexpectedLiquibaseException("Did not expect " + sql.length + " statements");
                }
                updatedRows = executor.update(new RawSqlStatement("EXEC sp_executesql N'SET NOCOUNT OFF " + sql[0].toSql().replace("'", "''") + "'"));
            }
            if (updatedRows != 1) {
                throw new LockException("Did not update change log lock correctly.\n\n" + updatedRows + " rows were updated instead of the expected 1 row using executor " + executor.getClass().getName() + " there are " + executor.queryForInt(new RawSqlStatement("select count(*) from " + database.getDatabaseChangeLogLockTableName())) + " rows in the table");
            }
            database.commit();
        }
    } catch (Exception e) {
        throw new LockException(e);
    } finally {
        try {
            hasChangeLogLock = false;
            database.setCanCacheLiquibaseTableInfo(false);
            LogFactory.getLogger().info("Successfully released change log lock");
            database.rollback();
        } catch (DatabaseException e) {
            ;
        }
        if (incomingQuotingStrategy != null) {
            database.setObjectQuotingStrategy(incomingQuotingStrategy);
        }
    }
}
Also used : Executor(liquibase.executor.Executor) LockException(liquibase.exception.LockException) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) Sql(liquibase.sql.Sql)

Example 12 with UnexpectedLiquibaseException

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

the class MissingPrimaryKeyChangeGenerator method fixMissing.

@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 = createAddPrimaryKeyChange();
    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 (referenceDatabase instanceof PostgresDatabase && pk.getBackingIndex() != null && pk.getBackingIndex().getClustered() != null && pk.getBackingIndex().getClustered()) {
        change.setClustered(true);
    }
    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);
                    //set table schema so it is found in the correct schema
                    backingIndex.getTable().setSchema(schema.getCatalogName(), schema.getSchemaName());
                }
                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()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) CatalogAndSchema(liquibase.CatalogAndSchema) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) CreateIndexChange(liquibase.change.core.CreateIndexChange) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) CatalogAndSchema(liquibase.CatalogAndSchema) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) OracleDatabase(liquibase.database.core.OracleDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 13 with UnexpectedLiquibaseException

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

the class CreateProcedureChange method generateCheckSum.

/**
     * Calculates the checksum based on the contained SQL.
     *
     * @see liquibase.change.AbstractChange#generateCheckSum()
     */
@Override
public CheckSum generateCheckSum() {
    if (this.path == null) {
        return super.generateCheckSum();
    }
    InputStream stream = null;
    try {
        stream = openSqlStream();
    } catch (IOException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    try {
        String procedureText = this.procedureText;
        if (stream == null && procedureText == null) {
            procedureText = "";
        }
        String encoding = LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding();
        if (procedureText != null) {
            try {
                stream = new ByteArrayInputStream(procedureText.getBytes(encoding));
            } catch (UnsupportedEncodingException e) {
                throw new AssertionError(encoding + " is not supported by the JVM, this should not happen according to the JavaDoc of the Charset class");
            }
        }
        CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(";", false, false, stream), false);
        return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum.toString());
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 14 with UnexpectedLiquibaseException

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

the class CreateProcedureChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    String endDelimiter = ";";
    if (database instanceof OracleDatabase) {
        endDelimiter = "\n/";
    } else if (database instanceof DB2Database) {
        endDelimiter = "";
    }
    String procedureText;
    String path = getPath();
    if (path == null) {
        procedureText = StringUtils.trimToNull(getProcedureText());
    } else {
        try {
            InputStream stream = openSqlStream();
            if (stream == null) {
                throw new IOException("File does not exist: " + path);
            }
            procedureText = StreamUtil.getStreamContents(stream, encoding);
        } catch (IOException e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }
    return generateStatements(procedureText, endDelimiter, database);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) DB2Database(liquibase.database.core.DB2Database) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 15 with UnexpectedLiquibaseException

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

the class ChangeFactory method register.

/**
     * Register a new Change class.
     * Normally called automatically by ChangeFactory on all Change implementations found by the ServiceLocator, but it can be called manually if needed.
     */
public void register(Class<? extends Change> changeClass) {
    try {
        Change instance = changeClass.newInstance();
        ChangeMetaData metaData = getChangeMetaData(instance);
        String name = metaData.getName();
        if (registry.get(name) == null) {
            registry.put(name, new TreeSet<Class<? extends Change>>(new Comparator<Class<? extends Change>>() {

                @Override
                public int compare(Class<? extends Change> o1, Class<? extends Change> o2) {
                    try {
                        return -1 * Integer.valueOf(getChangeMetaData(o1.newInstance()).getPriority()).compareTo(getChangeMetaData(o2.newInstance()).getPriority());
                    } catch (Exception e) {
                        throw new UnexpectedLiquibaseException(e);
                    }
                }
            }));
        }
        registry.get(name).add(changeClass);
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

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