Search in sources :

Example 6 with MSSQLDatabase

use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.

the class StandardLockService method acquireLock.

@Override
public boolean acquireLock() throws LockException {
    if (hasChangeLogLock) {
        return true;
    }
    quotingStrategy = database.getObjectQuotingStrategy();
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
        database.rollback();
        this.init();
        Boolean locked = (Boolean) ExecutorService.getInstance().getExecutor(database).queryForObject(new SelectFromDatabaseChangeLogLockStatement("LOCKED"), Boolean.class);
        if (locked) {
            return false;
        } else {
            executor.comment("Lock Database");
            int rowsUpdated = executor.update(new LockDatabaseChangeLogStatement());
            if (rowsUpdated == -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 LockDatabaseChangeLogStatement(), database);
                if (sql.length != 1) {
                    throw new UnexpectedLiquibaseException("Did not expect " + sql.length + " statements");
                }
                rowsUpdated = executor.update(new RawSqlStatement("EXEC sp_executesql N'SET NOCOUNT OFF " + sql[0].toSql().replace("'", "''") + "'"));
            }
            if (rowsUpdated > 1) {
                throw new LockException("Did not update change log lock correctly");
            }
            if (rowsUpdated == 0) {
                // another node was faster
                return false;
            }
            database.commit();
            LogFactory.getLogger().info("Successfully acquired change log lock");
            hasChangeLogLock = true;
            database.setCanCacheLiquibaseTableInfo(true);
            return true;
        }
    } catch (Exception e) {
        throw new LockException(e);
    } finally {
        try {
            database.rollback();
        } catch (DatabaseException e) {
            ;
        }
    }
}
Also used : LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) Sql(liquibase.sql.Sql) Executor(liquibase.executor.Executor) LockException(liquibase.exception.LockException) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Example 7 with MSSQLDatabase

use of liquibase.database.core.MSSQLDatabase 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 8 with MSSQLDatabase

use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.

the class MissingUniqueConstraintChangeGenerator method fixMissing.

@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 (referenceDatabase instanceof MSSQLDatabase) {
        change.setClustered(uc.isClustered() ? 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()]);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) ArrayList(java.util.ArrayList) AddUniqueConstraintChange(liquibase.change.core.AddUniqueConstraintChange) Change(liquibase.change.Change) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 9 with MSSQLDatabase

use of liquibase.database.core.MSSQLDatabase 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 10 with MSSQLDatabase

use of liquibase.database.core.MSSQLDatabase in project liquibase by liquibase.

the class Liquibase method outputHeader.

private void outputHeader(String message) throws DatabaseException {
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    executor.comment("*********************************************************************");
    executor.comment(message);
    executor.comment("*********************************************************************");
    executor.comment("Change Log: " + changeLogFile);
    executor.comment("Ran at: " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()));
    DatabaseConnection connection = getDatabase().getConnection();
    if (connection != null) {
        executor.comment("Against: " + connection.getConnectionUserName() + "@" + connection.getURL());
    }
    executor.comment("Liquibase version: " + LiquibaseUtil.getBuildVersion());
    executor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    if (database instanceof OracleDatabase) {
        executor.execute(new RawSqlStatement("SET DEFINE OFF;"));
    }
    if (database instanceof MSSQLDatabase && database.getDefaultCatalogName() != null) {
        executor.execute(new RawSqlStatement("USE " + database.escapeObjectName(database.getDefaultCatalogName(), Catalog.class) + ";"));
    }
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) DatabaseConnection(liquibase.database.DatabaseConnection) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Date(java.util.Date)

Aggregations

MSSQLDatabase (liquibase.database.core.MSSQLDatabase)38 Test (org.junit.Test)17 Sql (liquibase.sql.Sql)12 OracleDatabase (liquibase.database.core.OracleDatabase)11 Database (liquibase.database.Database)7 DB2Database (liquibase.database.core.DB2Database)7 MySQLDatabase (liquibase.database.core.MySQLDatabase)7 PostgresDatabase (liquibase.database.core.PostgresDatabase)7 RawSqlStatement (liquibase.statement.core.RawSqlStatement)6 Column (liquibase.structure.core.Column)6 ArrayList (java.util.ArrayList)5 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)5 DatabaseException (liquibase.exception.DatabaseException)5 Change (liquibase.change.Change)4 HsqlDatabase (liquibase.database.core.HsqlDatabase)4 SybaseDatabase (liquibase.database.core.SybaseDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 Executor (liquibase.executor.Executor)4 InsertOrUpdateStatement (liquibase.statement.core.InsertOrUpdateStatement)4 BigInteger (java.math.BigInteger)3