Search in sources :

Example 36 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class LiquibaseChangeLogSyncToTagSQLMojo 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)

Example 37 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class DatabaseUpdateTask method executeWithLiquibaseClassloader.

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

Example 38 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class DBDocTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    File outputDirFile = outputDirectory.getFile();
    if (!outputDirFile.exists()) {
        boolean success = outputDirFile.mkdirs();
        if (!success) {
            throw new BuildException("Unable to create output directory.");
        }
    }
    if (!outputDirFile.isDirectory()) {
        throw new BuildException("Output path is not a directory.");
    }
    Liquibase liquibase = getLiquibase();
    try {
        if (contexts != null) {
            liquibase.generateDocumentation(outputDirectory.toString(), contexts);
        } else {
            liquibase.generateDocumentation(outputDirectory.toString());
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Liquibase encountered an error while creating database documentation. " + e.toString(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) BuildException(org.apache.tools.ant.BuildException) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File)

Example 39 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class DatabaseRollbackTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Writer writer = null;
    Liquibase liquibase = getLiquibase();
    try {
        FileResource outputFile = getOutputFile();
        if (rollbackCount != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackCount, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else if (rollbackTag != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackTag, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else if (rollbackDate != null) {
            if (outputFile != null) {
                writer = getOutputFileWriter();
                liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels(), writer);
            } else {
                liquibase.rollback(rollbackDate, rollbackScript, new Contexts(getContexts()), getLabels());
            }
        } else {
            throw new BuildException("Unable to rollback database. No count, tag, or date set.");
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to rollback database: " + e.getMessage(), e);
    } catch (UnsupportedEncodingException e) {
        throw new BuildException("Unable to generate rollback SQL. Encoding [" + getOutputEncoding() + "] is not supported.", e);
    } catch (IOException e) {
        throw new BuildException("Unable to generate rollback SQL. Error creating output writer.", e);
    } finally {
        FileUtils.close(writer);
    }
}
Also used : Liquibase(liquibase.Liquibase) FileResource(org.apache.tools.ant.types.resources.FileResource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BuildException(org.apache.tools.ant.BuildException) LiquibaseException(liquibase.exception.LiquibaseException) IOException(java.io.IOException) Contexts(liquibase.Contexts) Writer(java.io.Writer)

Example 40 with Liquibase

use of liquibase.Liquibase in project liquibase by liquibase.

the class PostgreSQLIntegrationTest method testDependenciesInGenerateChangeLog.

@Test
public void testDependenciesInGenerateChangeLog() throws Exception {
    assumeNotNull(this.getDatabase());
    Liquibase liquibase = createLiquibase(this.dependenciesChangeLog);
    clearDatabase();
    try {
        liquibase.update(new Contexts());
        Database database = liquibase.getDatabase();
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, new CompareControl());
        DiffToChangeLog changeLogWriter = new DiffToChangeLog(diffResult, new DiffOutputControl(false, false, false, null));
        List<ChangeSet> changeSets = changeLogWriter.generateChangeSets();
        Assert.assertTrue(changeSets.size() > 0);
        ChangeSet addPrimaryKeyChangeSet = changeSets.stream().filter(changeSet -> changeSet.getChanges().get(0) instanceof AddPrimaryKeyChange).findFirst().orElse(null);
        Assert.assertNull(addPrimaryKeyChangeSet);
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.out);
        throw e;
    }
}
Also used : Liquibase(liquibase.Liquibase) ValidationFailedException(liquibase.exception.ValidationFailedException) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) DiffOutputControl(liquibase.diff.output.DiffOutputControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) AddPrimaryKeyChange(liquibase.change.core.AddPrimaryKeyChange) DiffResult(liquibase.diff.DiffResult) Contexts(liquibase.Contexts) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

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