Search in sources :

Example 1 with ChangeSet

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

the class ChangeSetExecutedPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    ObjectQuotingStrategy objectQuotingStrategy = null;
    if (changeSet == null) {
        objectQuotingStrategy = ObjectQuotingStrategy.LEGACY;
    } else {
        objectQuotingStrategy = changeSet.getObjectQuotingStrategy();
    }
    String changeLogFile = getChangeLogFile();
    if (changeLogFile == null) {
        changeLogFile = changeLog.getLogicalFilePath();
    }
    ChangeSet interestedChangeSet = new ChangeSet(getId(), getAuthor(), false, false, changeLogFile, null, null, false, objectQuotingStrategy, changeLog);
    RanChangeSet ranChangeSet;
    try {
        ranChangeSet = database.getRanChangeSet(interestedChangeSet);
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
    if (ranChangeSet == null || ranChangeSet.getExecType() == null || !ranChangeSet.getExecType().ran) {
        throw new PreconditionFailedException("Change Set '" + interestedChangeSet.toString(false) + "' has not been run", changeLog, this);
    }
}
Also used : PreconditionFailedException(liquibase.exception.PreconditionFailedException) RanChangeSet(liquibase.changelog.RanChangeSet) ChangeSet(liquibase.changelog.ChangeSet) ObjectQuotingStrategy(liquibase.database.ObjectQuotingStrategy) PreconditionFailedException(liquibase.exception.PreconditionFailedException) PreconditionErrorException(liquibase.exception.PreconditionErrorException) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException)

Example 2 with ChangeSet

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

the class LiquibaseHelperTest method testFilterChangeSets.

@Test
public void testFilterChangeSets() {
    final DropTableChange c1 = new DropTableChange();
    c1.setTableName("foo");
    final ChangeSet cs1 = new ChangeSet(null);
    cs1.addChange(c1);
    final DropTableChange c2 = new DropTableChange();
    c2.setTableName("JGROUPSPING");
    final ChangeSet cs2 = new ChangeSet(null);
    cs2.addChange(c2);
    final List<ChangeSet> original = Arrays.asList(cs1, cs2);
    final List<ChangeSet> filtered = LiquibaseHelper.filterChangeSets(original);
    assertEquals(1, filtered.size());
    assertEquals(c1, filtered.get(0).getChanges().get(0));
}
Also used : DropTableChange(liquibase.change.core.DropTableChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 3 with ChangeSet

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

the class LiquibaseHelperTest method testIgnoreChangeSet.

@Test
public void testIgnoreChangeSet() {
    final DropTableChange change = new DropTableChange();
    change.setTableName("JGROUPSPING");
    final ChangeSet changeSet = new ChangeSet(null);
    changeSet.addChange(change);
    assertTrue(LiquibaseHelper.isIgnoredChangeSet(changeSet));
}
Also used : DropTableChange(liquibase.change.core.DropTableChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 4 with ChangeSet

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

the class LiquibaseHelper method generateMigrations.

private File generateMigrations(final Database referenceDatabase, final Database targetDatabase) throws LiquibaseException, IOException {
    if (!resourcesDir.exists()) {
        resourcesDir.mkdirs();
    }
    if (!migrationsDir.exists()) {
        migrationsDir.mkdirs();
    }
    if (masterChangeLogFile.exists()) {
        LOG.info("Checking current database state");
        validateDatabaseState(targetDatabase);
    } else {
        LOG.info("Creating new master changelog");
        writeChangeSets(masterChangeLogFile, emptyList());
    }
    @SuppressWarnings("unchecked") final SnapshotControl snapshotControl = new SnapshotControl(referenceDatabase, liquibase.structure.core.Schema.class, liquibase.structure.core.Table.class, liquibase.structure.core.Column.class, liquibase.structure.core.PrimaryKey.class, liquibase.structure.core.Index.class);
    LOG.info("Executing diff");
    final CompareControl compareControl = new CompareControl(snapshotControl.getTypesToInclude());
    final DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(referenceDatabase, targetDatabase, compareControl);
    LOG.info("Converting diff to changelog");
    final DiffOutputControl diffOutputControl = new DiffOutputControl(false, false, true, null);
    final DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffResult, diffOutputControl);
    diffToChangeLog.setChangeSetAuthor(System.getProperty("user.name"));
    final List<ChangeSet> changeSets = filterChangeSets(diffToChangeLog.generateChangeSets());
    LOG.info("Found {} changes", changeSets.size());
    if (changeSets.isEmpty()) {
        return null;
    }
    final File generatedChangeLogFile = new File(migrationsDir, generateFileName(masterChangeLogFile));
    LOG.info("Writing new changelog: {}", generatedChangeLogFile);
    writeChangeSets(generatedChangeLogFile, changeSets);
    LOG.info("Add migration to master changelog: {}", masterChangeLogFile);
    addIncludeFile(generatedChangeLogFile);
    LOG.info("Cleaning changelog");
    cleanXmlFile(masterChangeLogFile);
    cleanXmlFile(generatedChangeLogFile);
    LOG.info("Diff complete");
    return generatedChangeLogFile;
}
Also used : CompareControl(liquibase.diff.compare.CompareControl) DiffOutputControl(liquibase.diff.output.DiffOutputControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) ChangeSet(liquibase.changelog.ChangeSet) File(java.io.File)

Example 5 with ChangeSet

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

the class DbChecker method checkDb.

@PostConstruct
public void checkDb() {
    try {
        DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        Liquibase liquibase = new Liquibase(script, new ClassLoaderResourceAccessor(), database);
        LOG.info("Checking database: {} URL:{}", database.getDatabaseProductName(), database.getConnection().getURL());
        List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(null, liquibase.getChangeLogParameters().getLabels());
        if (!changeSets.isEmpty()) {
            StringBuilder errors = new StringBuilder();
            for (ChangeSet changeSet : changeSets) {
                errors.append('>').append(changeSet.toString()).append('\n');
            }
            throw new Error("Database version mismatch. Check liquibase status. Errors:\n" + errors.toString() + database.getDatabaseProductName() + ", " + database);
        }
    } catch (liquibase.exception.LiquibaseException | SQLException e) {
        throw new Error("Database not ok, aborting startup.", e);
    }
    LOG.info("Liquibase ok");
}
Also used : SQLException(java.sql.SQLException) JdbcConnection(liquibase.database.jvm.JdbcConnection) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ChangeSet(liquibase.changelog.ChangeSet) PostConstruct(javax.annotation.PostConstruct)

Aggregations

ChangeSet (liquibase.changelog.ChangeSet)73 Test (org.junit.Test)41 Contexts (liquibase.Contexts)12 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)11 RanChangeSet (liquibase.changelog.RanChangeSet)11 Liquibase (liquibase.Liquibase)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 LiquibaseException (liquibase.exception.LiquibaseException)9 ArrayList (java.util.ArrayList)8 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 Sql (liquibase.sql.Sql)5 MarkChangeSetRanStatement (liquibase.statement.core.MarkChangeSetRanStatement)5 SQLException (java.sql.SQLException)4 Date (java.util.Date)4