Search in sources :

Example 6 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class MarkChangeSetRanGenerator method generateSql.

@Override
public Sql[] generateSql(MarkChangeSetRanStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String dateValue = database.getCurrentDateTimeFunction();
    ChangeSet changeSet = statement.getChangeSet();
    SqlStatement runStatement;
    try {
        if (statement.getExecType().equals(ChangeSet.ExecType.FAILED) || statement.getExecType().equals(ChangeSet.ExecType.SKIPPED)) {
            //don't mark
            return new Sql[0];
        }
        String tag = null;
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }
        if (statement.getExecType().ranBefore) {
            runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addNewColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addNewColumnValue("EXECTYPE", statement.getExecType().value).addNewColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
            if (tag != null) {
                ((UpdateStatement) runStatement).addNewColumnValue("TAG", tag);
            }
        } else {
            runStatement = new InsertStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addColumnValue("ID", changeSet.getId()).addColumnValue("AUTHOR", changeSet.getAuthor()).addColumnValue("FILENAME", changeSet.getFilePath()).addColumnValue("DATEEXECUTED", new DatabaseFunction(dateValue)).addColumnValue("ORDEREXECUTED", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getNextSequenceValue()).addColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).addColumnValue("DESCRIPTION", limitSize(changeSet.getDescription())).addColumnValue("COMMENTS", limitSize(StringUtils.trimToEmpty(changeSet.getComments()))).addColumnValue("EXECTYPE", statement.getExecType().value).addColumnValue("CONTEXTS", changeSet.getContexts() == null || changeSet.getContexts().isEmpty() ? null : changeSet.getContexts().toString()).addColumnValue("LABELS", changeSet.getLabels() == null || changeSet.getLabels().isEmpty() ? null : changeSet.getLabels().toString()).addColumnValue("LIQUIBASE", LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP")).addColumnValue("DEPLOYMENT_ID", ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).getDeploymentId());
            if (tag != null) {
                ((InsertStatement) runStatement).addColumnValue("TAG", tag);
            }
        }
    } catch (LiquibaseException e) {
        throw new UnexpectedLiquibaseException(e);
    }
    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ChangeSet(liquibase.changelog.ChangeSet) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql)

Example 7 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class TagDatabaseGenerator method generateSql.

@Override
public Sql[] generateSql(TagDatabaseStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String tableNameEscaped = database.escapeTableName(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName());
    String orderColumnNameEscaped = database.escapeObjectName("ORDEREXECUTED", Column.class);
    String dateColumnNameEscaped = database.escapeObjectName("DATEEXECUTED", Column.class);
    String tagColumnNameEscaped = database.escapeObjectName("TAG", Column.class);
    String tagEscaped = DataTypeFactory.getInstance().fromObject(statement.getTag(), database).objectToSql(statement.getTag(), database);
    if (database instanceof MySQLDatabase) {
        return new Sql[] { new UnparsedSql("UPDATE " + tableNameEscaped + " AS C " + "INNER JOIN (" + "SELECT " + orderColumnNameEscaped + ", " + dateColumnNameEscaped + " " + "FROM " + tableNameEscaped + " order by " + dateColumnNameEscaped + " desc, " + orderColumnNameEscaped + " desc limit 1) AS D " + "ON C." + orderColumnNameEscaped + " = D." + orderColumnNameEscaped + " " + "SET C." + tagColumnNameEscaped + " = " + tagEscaped) };
    } else if (database instanceof PostgresDatabase) {
        return new Sql[] { new UnparsedSql("UPDATE " + tableNameEscaped + " t SET TAG=" + tagEscaped + " FROM (SELECT " + dateColumnNameEscaped + ", " + orderColumnNameEscaped + " FROM " + tableNameEscaped + " ORDER BY " + dateColumnNameEscaped + " DESC, " + orderColumnNameEscaped + " DESC LIMIT 1) sub " + "WHERE t." + dateColumnNameEscaped + "=sub." + dateColumnNameEscaped + " AND t." + orderColumnNameEscaped + "=sub." + orderColumnNameEscaped) };
    } else if (database instanceof InformixDatabase) {
        String tempTableNameEscaped = database.escapeObjectName("max_order_temp", Table.class);
        return new Sql[] { new UnparsedSql("SELECT MAX(" + dateColumnNameEscaped + ") AS " + dateColumnNameEscaped + ", MAX(" + orderColumnNameEscaped + ") AS " + orderColumnNameEscaped + " " + "FROM " + tableNameEscaped + " " + "INTO TEMP " + tempTableNameEscaped + " WITH NO LOG"), new UnparsedSql("UPDATE " + tableNameEscaped + " " + "SET TAG = " + tagEscaped + " " + "WHERE " + dateColumnNameEscaped + " = (" + "SELECT " + dateColumnNameEscaped + " " + "FROM " + tempTableNameEscaped + ") AND " + orderColumnNameEscaped + " = (" + "SELECT " + orderColumnNameEscaped + " " + "FROM " + tempTableNameEscaped + ");"), new UnparsedSql("DROP TABLE " + tempTableNameEscaped + ";") };
    } else if (database instanceof MSSQLDatabase) {
        String changelogAliasEscaped = database.escapeObjectName("changelog", Table.class);
        String latestAliasEscaped = database.escapeObjectName("latest", Table.class);
        String idColumnEscaped = database.escapeObjectName("ID", Column.class);
        String authorColumnEscaped = database.escapeObjectName("AUTHOR", Column.class);
        String filenameColumnEscaped = database.escapeObjectName("FILENAME", Column.class);
        String topClause = "TOP (1)";
        try {
            if (database.getDatabaseMajorVersion() < 10) {
                // SQL Server 2005 or earlier
                topClause = "TOP 1";
            }
        } catch (DatabaseException ignored) {
        // assume SQL Server 2008 or later
        }
        return new Sql[] { new UnparsedSql("UPDATE " + changelogAliasEscaped + " " + "SET " + tagColumnNameEscaped + " = " + tagEscaped + " " + "FROM " + tableNameEscaped + " AS " + changelogAliasEscaped + " " + "INNER JOIN (" + "SELECT " + topClause + " " + idColumnEscaped + ", " + authorColumnEscaped + ", " + filenameColumnEscaped + " " + "FROM " + tableNameEscaped + " " + "ORDER BY " + dateColumnNameEscaped + " DESC, " + orderColumnNameEscaped + " DESC" + ") AS " + latestAliasEscaped + " " + "ON " + latestAliasEscaped + "." + idColumnEscaped + " = " + changelogAliasEscaped + "." + idColumnEscaped + " " + "AND " + latestAliasEscaped + "." + authorColumnEscaped + " = " + changelogAliasEscaped + "." + authorColumnEscaped + " " + "AND " + latestAliasEscaped + "." + filenameColumnEscaped + " = " + changelogAliasEscaped + "." + filenameColumnEscaped) };
    } else if (database instanceof OracleDatabase || database instanceof DB2Database) {
        String selectClause = "SELECT";
        String endClause = ")";
        String delimiter = "";
        if (database instanceof OracleDatabase) {
            selectClause = "SELECT * FROM (SELECT";
            endClause = ") where rownum=1)";
        } else if (database instanceof DB2Database) {
            endClause = " FETCH FIRST 1 ROWS ONLY)";
        }
        return new Sql[] { new UnparsedSql("MERGE INTO " + tableNameEscaped + " a " + "USING (" + selectClause + " " + orderColumnNameEscaped + ", " + dateColumnNameEscaped + " from " + tableNameEscaped + " order by " + dateColumnNameEscaped + " desc, " + orderColumnNameEscaped + " desc" + endClause + " b " + "ON ( a." + dateColumnNameEscaped + " = b." + dateColumnNameEscaped + " and a." + orderColumnNameEscaped + "=b." + orderColumnNameEscaped + " ) " + "WHEN MATCHED THEN " + "UPDATE SET  a.tag=" + tagEscaped + delimiter) };
    } else {
        //Only uses dateexecuted as a default. Depending on the timestamp resolution, multiple rows may be tagged which normally works fine but can cause confusion and some issues.
        //We cannot use orderexecuted alone because it is only guaranteed to be incrementing per update call.
        //TODO: Better handle other databases to use dateexecuted desc, orderexecuted desc.
        UpdateStatement updateStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("TAG", statement.getTag()).setWhereClause(dateColumnNameEscaped + " = (" + "SELECT MAX(" + dateColumnNameEscaped + ") " + "FROM " + tableNameEscaped + ")");
        return SqlGeneratorFactory.getInstance().generateSql(updateStatement, database);
    }
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql) Column(liquibase.structure.core.Column) DatabaseException(liquibase.exception.DatabaseException)

Example 8 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class UpdateChangeSetChecksumGenerator method generateSql.

@Override
public Sql[] generateSql(UpdateChangeSetChecksumStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    ChangeSet changeSet = statement.getChangeSet();
    SqlStatement runStatement = null;
    runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
    return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) Column(liquibase.structure.core.Column) ChangeSet(liquibase.changelog.ChangeSet)

Example 9 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class UpdateGeneratorTest method testGenerateSqlWithParamPlaceholders.

@Test
public void testGenerateSqlWithParamPlaceholders() {
    // given
    Database database = new MSSQLDatabase();
    UpdateStatement statement = new UpdateStatement(null, null, "DATABASECHANGELOG").addNewColumnValue("MD5SUM", "7:e27bf9c0c2313160ef960a15d44ced47").setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters("SYPA: AUTO_START tüüp INT -> TEXT, vaartus 0 00 17 * * ?", "martin", "db/changelog.xml");
    UpdateGenerator generator = new UpdateGenerator();
    // when
    Sql[] sqls = generator.generateSql(statement, database, null);
    // then
    assertEquals("UPDATE [DATABASECHANGELOG] " + "SET [MD5SUM] = '7:e27bf9c0c2313160ef960a15d44ced47' " + "WHERE [ID] = N'SYPA: AUTO_START tüüp INT -> TEXT, vaartus 0 00 17 * * ?' " + "AND [AUTHOR] = 'martin' " + "AND [FILENAME] = 'db/changelog.xml'", sqls[0].toSql());
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Column(liquibase.structure.core.Column) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 10 with UpdateStatement

use of liquibase.statement.core.UpdateStatement in project liquibase by liquibase.

the class UnlockDatabaseChangeLogGenerator method generateSql.

@Override
public Sql[] generateSql(UnlockDatabaseChangeLogStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String liquibaseSchema = database.getLiquibaseSchemaName();
    UpdateStatement releaseStatement = new UpdateStatement(database.getLiquibaseCatalogName(), liquibaseSchema, database.getDatabaseChangeLogLockTableName());
    releaseStatement.addNewColumnValue("LOCKED", false);
    releaseStatement.addNewColumnValue("LOCKGRANTED", null);
    releaseStatement.addNewColumnValue("LOCKEDBY", null);
    releaseStatement.setWhereClause(database.escapeColumnName(database.getLiquibaseCatalogName(), liquibaseSchema, database.getDatabaseChangeLogTableName(), "ID") + " = 1");
    return SqlGeneratorFactory.getInstance().generateSql(releaseStatement, database);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement)

Aggregations

UpdateStatement (liquibase.statement.core.UpdateStatement)13 Sql (liquibase.sql.Sql)5 SqlStatement (liquibase.statement.SqlStatement)5 Column (liquibase.structure.core.Column)5 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)2 ChangeSet (liquibase.changelog.ChangeSet)2 Database (liquibase.database.Database)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 OracleDatabase (liquibase.database.core.OracleDatabase)2 LiquibaseException (liquibase.exception.LiquibaseException)2 UnparsedSql (liquibase.sql.UnparsedSql)2 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)2 Test (org.junit.Test)2 Timestamp (java.sql.Timestamp)1 Change (liquibase.change.Change)1 TagDatabaseChange (liquibase.change.core.TagDatabaseChange)1 DB2Database (liquibase.database.core.DB2Database)1 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)1 DatabaseException (liquibase.exception.DatabaseException)1