Search in sources :

Example 11 with DatabaseSnapshot

use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.

the class MssqlIntegrationTest method defaultValuesTests.

@Test
public void defaultValuesTests() throws Exception {
    if (this.getDatabase() == null) {
        return;
    }
    Liquibase liquibase = createLiquibase("changelogs/mssql/issues/default.values.xml");
    liquibase.update((String) null);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
    for (Table table : snapshot.get(Table.class)) {
        for (Column column : table.getColumns()) {
            if (column.getName().toLowerCase().endsWith("_default")) {
                Object defaultValue = column.getDefaultValue();
                assertNotNull("Null default value for " + table.getName() + "." + column.getName(), defaultValue);
                if (column.getName().toLowerCase().contains("date") || column.getName().toLowerCase().contains("time")) {
                    if (defaultValue instanceof DatabaseFunction) {
                        ((DatabaseFunction) defaultValue).getValue().contains("type datetimeoffset");
                    } else {
                        assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof Date);
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(((Date) defaultValue));
                        assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
                        assertEquals(1, calendar.get(Calendar.MONTH));
                        assertEquals(2000, calendar.get(Calendar.YEAR));
                    }
                } else if (column.getName().toLowerCase().contains("char_")) {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof String);
                } else if (column.getName().toLowerCase().contains("binary_")) {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof DatabaseFunction);
                } else {
                    assertTrue("Unexpected default type " + defaultValue.getClass().getName() + " for " + table.getName() + "." + column.getName(), defaultValue instanceof Number);
                    assertEquals(1, ((Number) defaultValue).intValue());
                }
            }
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) DatabaseFunction(liquibase.statement.DatabaseFunction) Column(liquibase.structure.core.Column) Calendar(java.util.Calendar) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Date(java.util.Date) Test(org.junit.Test)

Example 12 with DatabaseSnapshot

use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.

the class MssqlIntegrationTest method dataTypeParamsTest.

@Test
public void dataTypeParamsTest() throws Exception {
    if (this.getDatabase() == null) {
        return;
    }
    Liquibase liquibase = createLiquibase("changelogs/mssql/issues/data.type.params.xml");
    liquibase.update((String) null);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
    for (Table table : snapshot.get(Table.class)) {
        if (getDatabase().isLiquibaseObject(table)) {
            continue;
        }
        for (Column column : table.getColumns()) {
            String expectedType = column.getName().split("_")[0];
            String foundTypeDefinition = DataTypeFactory.getInstance().from(column.getType(), new MSSQLDatabase()).toDatabaseDataType(getDatabase()).toString();
            assertFalse("Parameter found in " + table.getName() + "." + column.getName(), foundTypeDefinition.contains("("));
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 13 with DatabaseSnapshot

use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLogAltSchema.

@Test
public void testRerunDiffChangeLogAltSchema() throws Exception {
    if (database == null) {
        return;
    }
    if (!database.supportsSchemas()) {
        return;
    }
    Liquibase liquibase = createLiquibase(includedChangeLog);
    clearDatabase(liquibase);
    database.setDefaultSchemaName("lbcat2");
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    lockService.forceReleaseLock();
    liquibase.update(includedChangeLog);
    DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
    CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(CatalogAndSchema.DEFAULT, new CatalogAndSchema(null, "lbcat2")) }, originalSnapshot.getSnapshotControl().getTypesToInclude());
    DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
    File tempFile = File.createTempFile("liquibase-test", ".xml");
    FileOutputStream output = new FileOutputStream(tempFile);
    try {
        new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
        output.flush();
    } finally {
        output.close();
    }
    liquibase = createLiquibase(tempFile.getName());
    clearDatabase(liquibase);
    //run again to test changelog testing logic
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
        executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogTableName(), false));
    } catch (DatabaseException e) {
    //ok
    }
    try {
        executor.execute(new DropTableStatement("lbcat2", "lbcat2", database.getDatabaseChangeLogLockTableName(), false));
    } catch (DatabaseException e) {
    //ok
    }
    database.commit();
    DatabaseConnection connection = DatabaseTestContext.getInstance().getConnection(url);
    database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
    database.setDefaultSchemaName("lbcat2");
    liquibase = createLiquibase(tempFile.getName());
    try {
        liquibase.update(this.contexts);
    } catch (ValidationFailedException e) {
        e.printDescriptiveError(System.out);
        throw e;
    }
    tempFile.deleteOnExit();
    DatabaseSnapshot finalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
    CompareControl finalCompareControl = new CompareControl();
    finalCompareControl.addSuppressedField(Column.class, "autoIncrementInformation");
    DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, finalSnapshot, finalCompareControl);
    new DiffToReport(finalDiffResult, System.out).print();
    assertTrue(finalDiffResult.areEqual());
}
Also used : LockService(liquibase.lockservice.LockService) DiffOutputControl(liquibase.diff.output.DiffOutputControl) CatalogAndSchema(liquibase.CatalogAndSchema) Liquibase(liquibase.Liquibase) Executor(liquibase.executor.Executor) ValidationFailedException(liquibase.exception.ValidationFailedException) DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DatabaseConnection(liquibase.database.DatabaseConnection) DiffResult(liquibase.diff.DiffResult) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) DropTableStatement(liquibase.statement.core.DropTableStatement) DatabaseException(liquibase.exception.DatabaseException) Test(org.junit.Test)

Example 14 with DatabaseSnapshot

use of liquibase.snapshot.DatabaseSnapshot in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLog.

@Test
public void testRerunDiffChangeLog() throws Exception {
    if (database == null) {
        return;
    }
    for (int run = 0; run < 2; run++) {
        //run once outputting data as insert, once as csv
        boolean outputCsv = run == 1;
        runCompleteChangeLog();
        SnapshotControl snapshotControl = new SnapshotControl(database);
        //todo            compareControl.setDiffData(true);
        DatabaseSnapshot originalSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, snapshotControl);
        CompareControl compareControl = new CompareControl();
        //database returns different data even if the same
        compareControl.addSuppressedField(Column.class, "defaultValue");
        //database returns different data even if the same
        compareControl.addSuppressedField(Column.class, "autoIncrementInformation");
        if (database instanceof OracleDatabase) {
            //database returns different nvarchar2 info even though they are the same
            compareControl.addSuppressedField(Column.class, "type");
        }
        DiffOutputControl diffOutputControl = new DiffOutputControl();
        File tempFile = File.createTempFile("liquibase-test", ".xml");
        deleteOnExit(tempFile);
        if (outputCsv) {
            diffOutputControl.setDataDir(new File(tempFile.getParentFile(), "liquibase-data").getCanonicalPath().replaceFirst("\\w:", ""));
        }
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(database, null, compareControl);
        FileOutputStream output = new FileOutputStream(tempFile);
        try {
            new DiffToChangeLog(diffResult, new DiffOutputControl()).print(new PrintStream(output));
            output.flush();
        } finally {
            output.close();
        }
        Liquibase liquibase = createLiquibase(tempFile.getName());
        clearDatabase(liquibase);
        DatabaseSnapshot emptySnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        //run again to test changelog testing logic
        liquibase = createLiquibase(tempFile.getName());
        try {
            liquibase.update(this.contexts);
        } catch (ValidationFailedException e) {
            e.printDescriptiveError(System.out);
            throw e;
        }
        //            tempFile.deleteOnExit();
        DatabaseSnapshot migratedSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        DiffResult finalDiffResult = DiffGeneratorFactory.getInstance().compare(originalSnapshot, migratedSnapshot, compareControl);
        try {
            assertTrue(finalDiffResult.areEqual());
        } catch (AssertionError e) {
            new DiffToReport(finalDiffResult, System.err).print();
            throw e;
        }
        //diff to empty and drop all
        DiffResult emptyDiffResult = DiffGeneratorFactory.getInstance().compare(emptySnapshot, migratedSnapshot, compareControl);
        output = new FileOutputStream(tempFile);
        try {
            new DiffToChangeLog(emptyDiffResult, new DiffOutputControl(true, true, true, null)).print(new PrintStream(output));
            output.flush();
        } finally {
            output.close();
        }
        liquibase = createLiquibase(tempFile.getName());
        System.out.println("updating from " + tempFile.getCanonicalPath());
        try {
            liquibase.update(this.contexts);
        } catch (LiquibaseException e) {
            throw e;
        }
        DatabaseSnapshot emptyAgainSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(database.getDefaultSchema(), database, new SnapshotControl(database));
        assertEquals(2, emptyAgainSnapshot.get(Table.class).size());
        assertEquals(0, emptyAgainSnapshot.get(View.class).size());
    }
}
Also used : DiffOutputControl(liquibase.diff.output.DiffOutputControl) OracleDatabase(liquibase.database.core.OracleDatabase) Liquibase(liquibase.Liquibase) ValidationFailedException(liquibase.exception.ValidationFailedException) DiffToReport(liquibase.diff.output.report.DiffToReport) CompareControl(liquibase.diff.compare.CompareControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) DiffResult(liquibase.diff.DiffResult) LiquibaseException(liquibase.exception.LiquibaseException) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) Test(org.junit.Test)

Aggregations

DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)14 SnapshotControl (liquibase.snapshot.SnapshotControl)12 Liquibase (liquibase.Liquibase)7 CompareControl (liquibase.diff.compare.CompareControl)6 Test (org.junit.Test)6 DiffResult (liquibase.diff.DiffResult)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 CatalogAndSchema (liquibase.CatalogAndSchema)4 DatabaseObject (liquibase.structure.DatabaseObject)4 Column (liquibase.structure.core.Column)4 Table (liquibase.structure.core.Table)4 Database (liquibase.database.Database)3 DiffOutputControl (liquibase.diff.output.DiffOutputControl)3 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)3 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)3 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 DiffToReport (liquibase.diff.output.report.DiffToReport)2 LiquibaseException (liquibase.exception.LiquibaseException)2 ValidationFailedException (liquibase.exception.ValidationFailedException)2 InvalidExampleException (liquibase.snapshot.InvalidExampleException)2