use of liquibase.database.Database in project liquibase by liquibase.
the class TableSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
return;
}
if (foundObject instanceof Schema) {
Database database = snapshot.getDatabase();
Schema schema = (Schema) foundObject;
List<CachedRow> tableMetaDataRs = null;
try {
tableMetaDataRs = ((JdbcDatabaseSnapshot) snapshot).getMetaData().getTables(((AbstractJdbcDatabase) database).getJdbcCatalogName(schema), ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema), null);
for (CachedRow row : tableMetaDataRs) {
String tableName = row.getString("TABLE_NAME");
Table tableExample = (Table) new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);
schema.addDatabaseObject(tableExample);
}
} catch (SQLException e) {
throw new DatabaseException(e);
}
}
}
use of liquibase.database.Database in project liquibase by liquibase.
the class UniqueConstraintSnapshotGenerator method addTo.
@Override
protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException {
if (!snapshot.getSnapshotControl().shouldInclude(UniqueConstraint.class)) {
return;
}
if (foundObject instanceof Table) {
Table table = (Table) foundObject;
Database database = snapshot.getDatabase();
Schema schema;
schema = table.getSchema();
List<CachedRow> metadata = null;
try {
metadata = listConstraints(table, snapshot, schema);
} catch (SQLException e) {
throw new DatabaseException(e);
}
Set<String> seenConstraints = new HashSet<String>();
for (CachedRow constraint : metadata) {
UniqueConstraint uq = new UniqueConstraint().setName(cleanNameFromDatabase((String) constraint.get("CONSTRAINT_NAME"), database)).setTable(table);
if (constraint.containsColumn("INDEX_NAME")) {
uq.setBackingIndex(new Index((String) constraint.get("INDEX_NAME"), (String) constraint.get("INDEX_CATALOG"), null, table.getName()));
}
if ("CLUSTERED".equals(constraint.get("TYPE_DESC"))) {
uq.setClustered(true);
}
if (seenConstraints.add(uq.getName())) {
table.getUniqueConstraints().add(uq);
}
}
}
}
use of liquibase.database.Database in project liquibase by liquibase.
the class AbstractSQLChangeTest method generateStatements_willCallNativeSqlIfPossible.
@Test
public void generateStatements_willCallNativeSqlIfPossible() throws DatabaseException {
ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("SOME SQL");
Database database = mock(Database.class);
DatabaseConnection connection = mock(DatabaseConnection.class);
when(database.getConnection()).thenReturn(connection);
when(connection.nativeSQL("SOME SQL")).thenReturn("SOME NATIVE SQL");
SqlStatement[] statements = change.generateStatements(database);
assertEquals(1, statements.length);
assertEquals("SOME NATIVE SQL", ((RawSqlStatement) statements[0]).getSql());
//If there is an error, it falls back to passed SQL
when(connection.nativeSQL("SOME SQL")).thenThrow(new DatabaseException("Testing exception"));
statements = change.generateStatements(database);
assertEquals(1, statements.length);
assertEquals("SOME SQL", ((RawSqlStatement) statements[0]).getSql());
}
use of liquibase.database.Database in project liquibase by liquibase.
the class ChangeLogSyncVisitorTest method testVisitListenerConstructor.
@Test
public void testVisitListenerConstructor() throws LiquibaseException {
Database mockDatabase = mock(Database.class);
ChangeLogSyncListener mockListener = mock(ChangeLogSyncListener.class);
ChangeLogSyncVisitor visitor = new ChangeLogSyncVisitor(mockDatabase, mockListener);
visitor.visit(changeSet, databaseChangeLog, mockDatabase, Collections.<ChangeSetFilterResult>emptySet());
verify(mockDatabase).markChangeSetExecStatus(changeSet, ChangeSet.ExecType.EXECUTED);
verify(mockListener).markedRan(changeSet, databaseChangeLog, mockDatabase);
}
use of liquibase.database.Database in project liquibase by liquibase.
the class ValidatingVisitorTest method visit_torunOnly.
@Test
public void visit_torunOnly() throws Exception {
changeSet1.addChange(new CreateTableChange() {
@Override
public ValidationErrors validate(Database database) {
ValidationErrors changeValidationErrors = new ValidationErrors();
changeValidationErrors.addError("Test message");
return changeValidationErrors;
}
});
List<RanChangeSet> ran = new ArrayList<RanChangeSet>();
ran.add(new RanChangeSet(changeSet1));
ValidatingVisitor handler = new ValidatingVisitor(ran);
handler.visit(changeSet1, new DatabaseChangeLog(), null, null);
assertEquals(0, handler.getSetupExceptions().size());
assertTrue(handler.validationPassed());
}
Aggregations