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