Search in sources :

Example 31 with Liquibase

use of liquibase.Liquibase in project dropwizard by dropwizard.

the class DbLocksCommandTest method testRelease.

@Test
void testRelease() throws Exception {
    // We can't create locks in the database, so use mocks
    final Liquibase liquibase = Mockito.mock(Liquibase.class);
    locksCommand.run(new Namespace(Maps.of("list", false, "release", true)), liquibase);
    Mockito.verify(liquibase).forceReleaseLocks();
}
Also used : Liquibase(liquibase.Liquibase) Namespace(net.sourceforge.argparse4j.inf.Namespace) Test(org.junit.jupiter.api.Test)

Example 32 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class AbstractDatabaseDiffTask method getDiffResult.

protected DiffResult getDiffResult() {
    Liquibase liquibase = getLiquibase();
    Database targetDatabase = liquibase.getDatabase();
    Database referenceDatabase = createDatabaseFromType(referenceDatabaseType, getResourceAccessor());
    CatalogAndSchema targetCatalogAndSchema = buildCatalogAndSchema(targetDatabase);
    CatalogAndSchema referenceCatalogAndSchema = buildCatalogAndSchema(referenceDatabase);
    CompareControl.SchemaComparison[] schemaComparisons = { new CompareControl.SchemaComparison(referenceCatalogAndSchema, targetCatalogAndSchema) };
    SnapshotGeneratorFactory snapshotGeneratorFactory = SnapshotGeneratorFactory.getInstance();
    DatabaseSnapshot referenceSnapshot;
    try {
        referenceSnapshot = snapshotGeneratorFactory.createSnapshot(referenceDatabase.getDefaultSchema(), referenceDatabase, new SnapshotControl(referenceDatabase, diffTypes));
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to create a DatabaseSnapshot: " + e.getMessage(), e);
    }
    CompareControl compareControl = new CompareControl(schemaComparisons, referenceSnapshot.getSnapshotControl().getTypesToInclude());
    try {
        return liquibase.diff(referenceDatabase, targetDatabase, compareControl);
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to diff databases: " + e.getMessage(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl)

Example 33 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class BaseLiquibaseTask method execute.

@Override
public final void execute() throws BuildException {
    super.execute();
    log(coreBundle.getString("starting.liquibase"), Project.MSG_INFO);
    classLoader = getProject().createClassLoader(classpath);
    classLoader.setParent(this.getClass().getClassLoader());
    classLoader.setThreadContextLoader();
    validateParameters();
    final Database[] database = { null };
    try {
        resourceAccessor = createResourceAccessor(classLoader);
        scopeValues.put(Scope.Attr.resourceAccessor.name(), resourceAccessor);
        scopeValues.put(Scope.Attr.classLoader.name(), classLoader);
        Scope.child(scopeValues, () -> {
            database[0] = createDatabaseFromType(databaseType, resourceAccessor);
            liquibase = new Liquibase(getChangeLogFile(), resourceAccessor, database[0]);
            if (changeLogParameters != null) {
                changeLogParameters.applyParameters(liquibase);
            }
            if (shouldRun()) {
                executeWithLiquibaseClassloader();
            }
        });
    } catch (Exception e) {
        throw new BuildException("Unable to initialize Liquibase: " + e.getMessage(), e);
    } finally {
        closeDatabase(database[0]);
        classLoader.resetThreadContextLoader();
        classLoader.cleanup();
        classLoader = null;
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) BuildException(org.apache.tools.ant.BuildException) DatabaseException(liquibase.exception.DatabaseException) BuildException(org.apache.tools.ant.BuildException)

Example 34 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class ChangeLogSyncToTagTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    OutputStreamWriter writer = null;
    try {
        FileResource outputFile = getOutputFile();
        if (outputFile != null) {
            writer = new OutputStreamWriter(outputFile.getOutputStream(), getOutputEncoding());
            liquibase.changeLogSync(toTag, new Contexts(getContexts()), getLabels(), writer);
        } else {
            liquibase.changeLogSync(toTag, new Contexts(getContexts()), getLabels());
        }
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to generate sync SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to generate sync SQL. Error creating output writer.", e);
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to sync change log: " + e.getMessage(), e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) LiquibaseException(liquibase.exception.LiquibaseException) Contexts(liquibase.Contexts)

Example 35 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class LiquibaseChangeLogSyncSQLMojo method createLiquibase.

@Override
protected Liquibase createLiquibase(Database db) throws MojoExecutionException {
    Liquibase liquibase = super.createLiquibase(db);
    // Setup the output file writer
    try {
        if (!migrationSqlOutputFile.exists()) {
            // Ensure the parent directories exist
            migrationSqlOutputFile.getParentFile().mkdirs();
            // Create the actual file
            if (!migrationSqlOutputFile.createNewFile()) {
                throw new MojoExecutionException("Cannot create the migration SQL file; " + migrationSqlOutputFile.getAbsolutePath());
            }
        }
        outputWriter = getOutputWriter(migrationSqlOutputFile);
    } catch (IOException e) {
        getLog().error(e);
        throw new MojoExecutionException("Failed to create SQL output writer", e);
    }
    getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath());
    return liquibase;
}
Also used : Liquibase(liquibase.Liquibase) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException)

Aggregations

Liquibase (liquibase.Liquibase)97 Test (org.junit.Test)29 LiquibaseException (liquibase.exception.LiquibaseException)25 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)25 JdbcConnection (liquibase.database.jvm.JdbcConnection)24 IOException (java.io.IOException)22 Database (liquibase.database.Database)22 SQLException (java.sql.SQLException)17 Contexts (liquibase.Contexts)17 ValidationFailedException (liquibase.exception.ValidationFailedException)11 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)10 DatabaseException (liquibase.exception.DatabaseException)10 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)10 BuildException (org.apache.tools.ant.BuildException)10 ChangeSet (liquibase.changelog.ChangeSet)9 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)9 Connection (java.sql.Connection)8 ResourceAccessor (liquibase.resource.ResourceAccessor)8 FileNotFoundException (java.io.FileNotFoundException)6 Date (java.util.Date)6