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;
}
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);
}
}
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);
}
}
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);
}
}
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;
}
}
Aggregations