Search in sources :

Example 61 with Liquibase

use of liquibase.Liquibase in project microservice_framework by CJSCommonPlatform.

the class CakeShopIT method initDatabase.

private static DataSource initDatabase(final String dbUrlPropertyName, final String dbUserNamePropertyName, final String dbPasswordPropertyName, final String... liquibaseChangeLogXmls) throws Exception {
    final BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName(H2_DRIVER);
    dataSource.setUrl(TEST_PROPERTIES.value(dbUrlPropertyName));
    dataSource.setUsername(TEST_PROPERTIES.value(dbUserNamePropertyName));
    dataSource.setPassword(TEST_PROPERTIES.value(dbPasswordPropertyName));
    boolean dropped = false;
    final JdbcConnection jdbcConnection = new JdbcConnection(dataSource.getConnection());
    for (String liquibaseChangeLogXml : liquibaseChangeLogXmls) {
        Liquibase liquibase = new Liquibase(liquibaseChangeLogXml, new ClassLoaderResourceAccessor(), jdbcConnection);
        if (!dropped) {
            liquibase.dropAll();
            dropped = true;
        }
        liquibase.update("");
    }
    return dataSource;
}
Also used : Liquibase(liquibase.Liquibase) JdbcConnection(liquibase.database.jvm.JdbcConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource)

Example 62 with Liquibase

use of liquibase.Liquibase 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 63 with Liquibase

use of liquibase.Liquibase in project iaf by ibissource.

the class LiquibaseMigrator method createMigrator.

private Liquibase createMigrator(Resource resource) throws SQLException, LiquibaseException {
    if (resource == null) {
        throw new LiquibaseException("no resource provided");
    }
    ResourceAccessor resourceAccessor = new LiquibaseResourceAccessor(resource);
    DatabaseConnection connection = getDatabaseConnection();
    return new Liquibase(resource.getSystemId(), resourceAccessor, connection);
}
Also used : Liquibase(liquibase.Liquibase) ResourceAccessor(liquibase.resource.ResourceAccessor) DatabaseConnection(liquibase.database.DatabaseConnection) LiquibaseException(liquibase.exception.LiquibaseException)

Example 64 with Liquibase

use of liquibase.Liquibase in project iaf by ibissource.

the class LiquibaseMigrator method doValidate.

private void doValidate() throws LiquibaseException, SQLException {
    try (Liquibase liquibase = createMigrator()) {
        Database database = liquibase.getDatabase();
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.waitForLock();
        List<RanChangeSet> alreadyExecutedChangeSets = database.getRanChangeSetList();
        for (RanChangeSet ranChangeSet : alreadyExecutedChangeSets) {
            CheckSum checkSum = ranChangeSet.getLastCheckSum();
            if (checkSum != null && checkSum.getVersion() < CheckSum.getCurrentVersion()) {
                migrationLog.warn("checksum [" + checkSum + "] for changeset [" + ranChangeSet + "] is outdated and will be updated");
            }
        }
        DatabaseChangeLog changeLog;
        try {
            changeLog = liquibase.getDatabaseChangeLog();
            ChangeLogHistoryService changeLogHistoryService = ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database);
            changeLogHistoryService.init();
            // Validate old checksums and update if required
            changeLogHistoryService.upgradeChecksums(changeLog, contexts, labelExpression);
            changeLogHistoryService.reset();
            // Validate the new (updated) checksums
            changeLog.validate(database, contexts, labelExpression);
        } finally {
            try {
                lockService.releaseLock();
            } catch (LockException e) {
                log.warn("unable to clean up Liquibase Lock", e);
            }
            LockServiceFactory.getInstance().resetAll();
            ChangeLogHistoryServiceFactory.getInstance().resetAll();
            Scope.getCurrentScope().getSingleton(ExecutorService.class).reset();
            liquibase.setChangeExecListener(null);
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) ChangeLogHistoryService(liquibase.changelog.ChangeLogHistoryService) LockService(liquibase.lockservice.LockService) LockException(liquibase.exception.LockException) CheckSum(liquibase.change.CheckSum) Database(liquibase.database.Database) ExecutorService(liquibase.executor.ExecutorService) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) RanChangeSet(liquibase.changelog.RanChangeSet)

Example 65 with Liquibase

use of liquibase.Liquibase in project jOOQ by jOOQ.

the class LiquibaseDatabase method export.

@Override
protected void export() throws Exception {
    String rootPath = getProperties().getProperty("rootPath");
    String scripts = getProperties().getProperty("scripts");
    includeLiquibaseTables = Boolean.parseBoolean(getProperties().getProperty("includeLiquibaseTables", "false"));
    if (isBlank(scripts)) {
        scripts = "";
        log.warn("No scripts defined", "It is recommended that you provide an explicit script directory to scan");
    }
    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection()));
    String contexts = "";
    // Database.setXyz() configuration setter calls
    for (Entry<Object, Object> entry : getProperties().entrySet()) {
        String key = "" + entry.getKey();
        if (key.startsWith("database.")) {
            String property = key.substring("database.".length());
            Method setter = SETTERS.get("set" + Character.toUpperCase(property.charAt(0)) + property.substring(1));
            try {
                if (setter != null)
                    setter.invoke(database, Convert.convert(entry.getValue(), setter.getParameterTypes()[0]));
            } catch (Exception e) {
                log.warn("Configuration error", e.getMessage(), e);
            }
        } else // [#9872] Some changeLogParameters can also be passed along
        if (key.startsWith("changeLogParameters.")) {
            String property = key.substring("changeLogParameters.".length());
            if ("contexts".equals(property))
                contexts = "" + entry.getValue();
        }
    }
    // Retrieve changeLog table names as they might be overridden by configuration setters
    databaseChangeLogTableName = database.getDatabaseChangeLogTableName();
    databaseChangeLogLockTableName = database.getDatabaseChangeLogLockTableName();
    // [#9866] Allow for loading included files from the classpath or using absolute paths.
    // [#12872] [#13021] The decision is made based on the presence of the rootPath property
    ResourceAccessor ra = StringUtils.isBlank(rootPath) ? new CompositeResourceAccessor(new ClassLoaderResourceAccessor(), new ClassLoaderResourceAccessor(Thread.currentThread().getContextClassLoader())) : new FileSystemResourceAccessor(new File(rootPath));
    Liquibase liquibase = new Liquibase(scripts, ra, database);
    liquibase.update(contexts);
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) JdbcConnection(liquibase.database.jvm.JdbcConnection) Method(java.lang.reflect.Method) SQLException(java.sql.SQLException) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) AbstractInterpretingDatabase(org.jooq.meta.extensions.AbstractInterpretingDatabase) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Aggregations

Liquibase (liquibase.Liquibase)124 LiquibaseException (liquibase.exception.LiquibaseException)37 JdbcConnection (liquibase.database.jvm.JdbcConnection)35 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)33 Database (liquibase.database.Database)32 Test (org.junit.Test)30 IOException (java.io.IOException)27 Contexts (liquibase.Contexts)24 SQLException (java.sql.SQLException)23 ResourceAccessor (liquibase.resource.ResourceAccessor)14 ChangeSet (liquibase.changelog.ChangeSet)13 DatabaseException (liquibase.exception.DatabaseException)12 ValidationFailedException (liquibase.exception.ValidationFailedException)12 LabelExpression (liquibase.LabelExpression)11 Connection (java.sql.Connection)10 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)10 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 BuildException (org.apache.tools.ant.BuildException)10 LockException (liquibase.exception.LockException)9