Search in sources :

Example 71 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class YamlChangeLogSerializerTest method serialize__change.

@Test
public void serialize__change() {
    ChangeSet changeSet = new ChangeSet("test1", "nvoxland", false, true, "/test/me.txt", null, null, null);
    CreateTableChange change = new CreateTableChange();
    change.setTableName("testTable");
    change.addColumn(new ColumnConfig().setName("id").setType("int"));
    change.addColumn(new ColumnConfig().setName("name").setType("varchar(255)"));
    changeSet.addChange(change);
    String out = new YamlChangeLogSerializer().serialize(changeSet, false);
    System.out.println(out);
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) CreateTableChange(liquibase.change.core.CreateTableChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 72 with ChangeSet

use of liquibase.changelog.ChangeSet in project liquibase by liquibase.

the class XMLChangeLogSerializerTest method serialize_pretty_ChangeSetParameters.

@Test
public void serialize_pretty_ChangeSetParameters() throws Exception {
    ChangeSet changeSet = new ChangeSet("1", "tms", true, true, "path/to/file.json", "context", "mssql", "runWith", false, ObjectQuotingStrategy.LEGACY, null);
    changeSet.setCreated("created");
    changeSet.setFailOnError(true);
    changeSet.setLabels(new Labels("label"));
    changeSet.setLogicalFilePath("path/to/file.json");
    String out = new XMLChangeLogSerializer().serialize(changeSet, true);
    assertEquals("<changeSet author=\"tms\"\n" + "        context=\"context\"\n" + "        created=\"created\"\n" + "        dbms=\"mssql\"\n" + "        failOnError=\"true\"\n" + "        id=\"1\"\n" + "        labels=\"label\"\n" + "        logicalFilePath=\"path/to/file.json\"\n" + "        objectQuotingStrategy=\"LEGACY\"\n" + "        runAlways=\"true\"\n" + "        runOnChange=\"true\"/>", out);
}
Also used : Labels(liquibase.Labels) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 73 with ChangeSet

use of liquibase.changelog.ChangeSet in project iaf by ibissource.

the class LiquibaseMigrator method update.

@Override
public void update() {
    List<String> changes = new ArrayList<>();
    try (Liquibase liquibase = createMigrator()) {
        List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(contexts, labelExpression);
        for (ChangeSet changeSet : changeSets) {
            changes.add("LiquiBase applying change [" + changeSet.getId() + ":" + changeSet.getAuthor() + "] description [" + changeSet.getDescription() + "]");
        }
        if (!changeSets.isEmpty()) {
            liquibase.update(contexts);
            ChangeSet lastChange = changeSets.get(changeSets.size() - 1);
            String tag = lastChange.getId() + ":" + lastChange.getAuthor();
            liquibase.tag(tag);
            if (changes.size() > 1) {
                logConfigurationMessage("LiquiBase applied [" + changes.size() + "] change(s) and added tag [" + tag + "]");
            } else {
                for (String change : changes) {
                    logConfigurationMessage(change + " tag [" + tag + "]");
                }
            }
        }
    } catch (Exception e) {
        String errorMsg = "Error running LiquiBase update. Failed to execute [" + changes.size() + "] change(s): ";
        errorMsg += e.getMessage();
        ConfigurationWarnings.add(this, log, errorMsg, e);
    }
}
Also used : Liquibase(liquibase.Liquibase) ArrayList(java.util.ArrayList) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) LockException(liquibase.exception.LockException) JdbcException(nl.nn.adapterframework.jdbc.JdbcException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ValidationFailedException(liquibase.exception.ValidationFailedException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 74 with ChangeSet

use of liquibase.changelog.ChangeSet in project keycloak by keycloak.

the class QuarkusJpaUpdaterProvider method updateChangeSet.

protected void updateChangeSet(Liquibase liquibase, Writer exportWriter) throws LiquibaseException {
    String changelog = liquibase.getChangeLogFile();
    Database database = liquibase.getDatabase();
    Table changelogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
    if (changelogTable != null) {
        boolean hasDeploymentIdColumn = changelogTable.getColumn(DEPLOYMENT_ID_COLUMN) != null;
        // create DEPLOYMENT_ID column if it doesn't exist
        if (!hasDeploymentIdColumn) {
            ChangeLogHistoryService changelogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changelogHistoryService.generateDeploymentId();
            String deploymentId = changelogHistoryService.getDeploymentId();
            logger.debugv("Adding missing column {0}={1} to {2} table", DEPLOYMENT_ID_COLUMN, deploymentId, changelogTable.getName());
            List<SqlStatement> statementsToExecute = new ArrayList<>();
            statementsToExecute.add(new AddColumnStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", null));
            statementsToExecute.add(new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName()).addNewColumnValue(DEPLOYMENT_ID_COLUMN, deploymentId));
            statementsToExecute.add(new SetNullableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", false));
            ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
            Executor executor = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, liquibase.getDatabase());
            for (SqlStatement sql : statementsToExecute) {
                executor.execute(sql);
                database.commit();
            }
        }
    }
    List<ChangeSet> changeSets = getLiquibaseUnrunChangeSets(liquibase);
    if (!changeSets.isEmpty()) {
        List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
        if (ranChangeSets.isEmpty()) {
            logger.infov("Initializing database schema. Using changelog {0}", changelog);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog);
            } else {
                logger.infov("Updating database. Using changelog {0}", changelog);
            }
        }
        if (exportWriter != null) {
            if (ranChangeSets.isEmpty()) {
                outputChangeLogTableCreationScript(liquibase, exportWriter);
            }
            liquibase.update(null, new LabelExpression(), exportWriter, false);
        } else {
            liquibase.update((Contexts) null);
        }
        logger.debugv("Completed database update for changelog {0}", changelog);
    } else {
        logger.debugv("Database is up to date for changelog {0}", changelog);
    }
    // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance().
    // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here
    resetLiquibaseServices(liquibase);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) SqlStatement(liquibase.statement.SqlStatement) CustomChangeLogHistoryService(org.keycloak.connections.jpa.updater.liquibase.conn.CustomChangeLogHistoryService) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) Column(liquibase.structure.core.Column) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) LabelExpression(liquibase.LabelExpression) SnapshotControl(liquibase.snapshot.SnapshotControl) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) AddColumnStatement(liquibase.statement.core.AddColumnStatement) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 75 with ChangeSet

use of liquibase.changelog.ChangeSet in project keycloak by keycloak.

the class LiquibaseJpaUpdaterProvider method updateChangeSet.

protected void updateChangeSet(Liquibase liquibase, Writer exportWriter) throws LiquibaseException, SQLException {
    String changelog = liquibase.getChangeLogFile();
    Database database = liquibase.getDatabase();
    Table changelogTable = SnapshotGeneratorFactory.getInstance().getDatabaseChangeLogTable(new SnapshotControl(database, false, Table.class, Column.class), database);
    if (changelogTable != null) {
        boolean hasDeploymentIdColumn = changelogTable.getColumn(DEPLOYMENT_ID_COLUMN) != null;
        // create DEPLOYMENT_ID column if it doesn't exist
        if (!hasDeploymentIdColumn) {
            ChangeLogHistoryService changelogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changelogHistoryService.generateDeploymentId();
            String deploymentId = changelogHistoryService.getDeploymentId();
            logger.debugv("Adding missing column {0}={1} to {2} table", DEPLOYMENT_ID_COLUMN, deploymentId, changelogTable.getName());
            List<SqlStatement> statementsToExecute = new ArrayList<>();
            statementsToExecute.add(new AddColumnStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", null));
            statementsToExecute.add(new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName()).addNewColumnValue(DEPLOYMENT_ID_COLUMN, deploymentId));
            statementsToExecute.add(new SetNullableStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), changelogTable.getName(), DEPLOYMENT_ID_COLUMN, "VARCHAR(10)", false));
            ExecutorService executorService = Scope.getCurrentScope().getSingleton(ExecutorService.class);
            Executor executor = executorService.getExecutor(LiquibaseConstants.JDBC_EXECUTOR, liquibase.getDatabase());
            for (SqlStatement sql : statementsToExecute) {
                executor.execute(sql);
                database.commit();
            }
        }
    }
    List<ChangeSet> changeSets = getLiquibaseUnrunChangeSets(liquibase);
    if (!changeSets.isEmpty()) {
        List<RanChangeSet> ranChangeSets = liquibase.getDatabase().getRanChangeSetList();
        if (ranChangeSets.isEmpty()) {
            logger.infov("Initializing database schema. Using changelog {0}", changelog);
        } else {
            if (logger.isDebugEnabled()) {
                logger.debugv("Updating database from {0} to {1}. Using changelog {2}", ranChangeSets.get(ranChangeSets.size() - 1).getId(), changeSets.get(changeSets.size() - 1).getId(), changelog);
            } else {
                logger.infov("Updating database. Using changelog {0}", changelog);
            }
        }
        if (exportWriter != null) {
            if (ranChangeSets.isEmpty()) {
                outputChangeLogTableCreationScript(liquibase, exportWriter);
            }
            liquibase.update(null, new LabelExpression(), exportWriter, false);
        } else {
            liquibase.update((Contexts) null);
        }
        logger.debugv("Completed database update for changelog {0}", changelog);
    } else {
        logger.debugv("Database is up to date for changelog {0}", changelog);
    }
    // Needs to restart liquibase services to clear ChangeLogHistoryServiceFactory.getInstance().
    // See https://issues.jboss.org/browse/KEYCLOAK-3769 for discussion relevant to why reset needs to be here
    resetLiquibaseServices(liquibase);
}
Also used : UpdateStatement(liquibase.statement.core.UpdateStatement) Table(liquibase.structure.core.Table) ArrayList(java.util.ArrayList) SqlStatement(liquibase.statement.SqlStatement) CustomChangeLogHistoryService(org.keycloak.connections.jpa.updater.liquibase.conn.CustomChangeLogHistoryService) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) Column(liquibase.structure.core.Column) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) LabelExpression(liquibase.LabelExpression) SnapshotControl(liquibase.snapshot.SnapshotControl) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) AddColumnStatement(liquibase.statement.core.AddColumnStatement) RanChangeSet(liquibase.changelog.RanChangeSet)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)75 Test (org.junit.Test)41 RanChangeSet (liquibase.changelog.RanChangeSet)13 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 Database (liquibase.database.Database)11 ArrayList (java.util.ArrayList)10 Liquibase (liquibase.Liquibase)10 Change (liquibase.change.Change)9 LiquibaseException (liquibase.exception.LiquibaseException)9 DiffOutputControl (liquibase.diff.output.DiffOutputControl)7 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)7 IOException (java.io.IOException)6 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)6 DiffResult (liquibase.diff.DiffResult)6 CompareControl (liquibase.diff.compare.CompareControl)6 LabelExpression (liquibase.LabelExpression)5 Sql (liquibase.sql.Sql)5 SqlStatement (liquibase.statement.SqlStatement)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5