Search in sources :

Example 26 with Database

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);
        }
    }
}
Also used : CachedRow(liquibase.snapshot.CachedRow) SQLException(java.sql.SQLException) CatalogAndSchema(liquibase.CatalogAndSchema) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) JdbcDatabaseSnapshot(liquibase.snapshot.JdbcDatabaseSnapshot) AbstractJdbcDatabase(liquibase.database.AbstractJdbcDatabase) DatabaseException(liquibase.exception.DatabaseException)

Example 27 with Database

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);
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) Database(liquibase.database.Database) DatabaseException(liquibase.exception.DatabaseException)

Example 28 with Database

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());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseException(liquibase.exception.DatabaseException) Test(org.junit.Test)

Example 29 with Database

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);
}
Also used : Database(liquibase.database.Database) Test(org.junit.Test)

Example 30 with Database

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());
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) CreateTableChange(liquibase.change.core.CreateTableChange) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) ArrayList(java.util.ArrayList) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) RanChangeSet(liquibase.changelog.RanChangeSet) Test(org.junit.Test)

Aggregations

Database (liquibase.database.Database)146 Test (org.junit.Test)74 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)50 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)43 Sql (liquibase.sql.Sql)42 DB2Database (liquibase.database.core.DB2Database)41 OracleDatabase (liquibase.database.core.OracleDatabase)39 PostgresDatabase (liquibase.database.core.PostgresDatabase)37 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DerbyDatabase (liquibase.database.core.DerbyDatabase)33 H2Database (liquibase.database.core.H2Database)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)33 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 CreateTableStatement (liquibase.statement.core.CreateTableStatement)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)30 DatabaseException (liquibase.exception.DatabaseException)25 AbstractJdbcDatabaseTest (liquibase.database.AbstractJdbcDatabaseTest)21 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)17