Search in sources :

Example 31 with DatabaseConnection

use of liquibase.database.DatabaseConnection in project liquibase by liquibase.

the class SybaseASADatabase method setConnection.

@Override
public void setConnection(DatabaseConnection conn) {
    DatabaseConnection dbConn;
    if (conn instanceof JdbcConnection) {
        // If conn is a real connection (JDBC), wrap it to prevent a driver bug
        // (see SysbaseASAConnection for details)
        dbConn = new SybaseASAConnection(((JdbcConnection) conn).getWrappedConnection());
    } else {
        dbConn = conn;
    }
    super.setConnection(dbConn);
}
Also used : SybaseASAConnection(liquibase.database.jvm.SybaseASAConnection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 32 with DatabaseConnection

use of liquibase.database.DatabaseConnection in project liquibase by liquibase.

the class SpringLiquibase method createDatabase.

/**
 * Subclasses may override this method add change some database settings such as
 * default schema before returning the database object.
 *
 * @param c
 * @return a Database implementation retrieved from the {@link DatabaseFactory}.
 * @throws DatabaseException
 */
protected Database createDatabase(Connection c, ResourceAccessor resourceAccessor) throws DatabaseException {
    DatabaseConnection liquibaseConnection;
    if (c == null) {
        log.warning("Null connection returned by liquibase datasource. Using offline unknown database");
        liquibaseConnection = new OfflineConnection("offline:unknown", resourceAccessor);
    } else {
        liquibaseConnection = new JdbcConnection(c);
    }
    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(liquibaseConnection);
    if (StringUtil.trimToNull(this.defaultSchema) != null) {
        if (database.supportsSchemas()) {
            database.setDefaultSchemaName(this.defaultSchema);
        } else if (database.supportsCatalogs()) {
            database.setDefaultCatalogName(this.defaultSchema);
        }
    }
    if (StringUtil.trimToNull(this.liquibaseSchema) != null) {
        if (database.supportsSchemas()) {
            database.setLiquibaseSchemaName(this.liquibaseSchema);
        } else if (database.supportsCatalogs()) {
            database.setLiquibaseCatalogName(this.liquibaseSchema);
        }
    }
    if (StringUtil.trimToNull(this.liquibaseTablespace) != null && database.supportsTablespaces()) {
        database.setLiquibaseTablespaceName(this.liquibaseTablespace);
    }
    if (StringUtil.trimToNull(this.databaseChangeLogTable) != null) {
        database.setDatabaseChangeLogTableName(this.databaseChangeLogTable);
    }
    if (StringUtil.trimToNull(this.databaseChangeLogLockTable) != null) {
        database.setDatabaseChangeLogLockTableName(this.databaseChangeLogLockTable);
    }
    return database;
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection)

Example 33 with DatabaseConnection

use of liquibase.database.DatabaseConnection in project liquibase by liquibase.

the class AbstractIntegrationTest method testRerunDiffChangeLogAltSchema.

@Test
public void testRerunDiffChangeLogAltSchema() throws Exception {
    assumeNotNull(this.getDatabase());
    if (database.getShortName().equalsIgnoreCase("mssql")) {
        // not possible on MSSQL.
        return;
    }
    if (!database.supportsSchemas()) {
        return;
    }
    Liquibase liquibase = createLiquibase(includedChangeLog);
    database.setDefaultSchemaName("lbschem2");
    clearDatabase();
    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("lbcat2", null)) }, 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();
    // run again to test changelog testing logic
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
    try {
        executor.execute(new DropTableStatement("lbcat2", null, database.getDatabaseChangeLogTableName(), false));
    } catch (DatabaseException e) {
    // ok
    }
    try {
        executor.execute(new DropTableStatement("lbcat2", null, database.getDatabaseChangeLogLockTableName(), false));
    } catch (DatabaseException e) {
    // ok
    }
    database.commit();
    DatabaseConnection connection = new JdbcConnection(testSystem.getConnection());
    database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
    database.setDefaultSchemaName("lbschem2");
    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("running the same change log two times against an alternative schema should produce " + "equal snapshots.", finalDiffResult.areEqual());
}
Also used : LockService(liquibase.lockservice.LockService) DiffOutputControl(liquibase.diff.output.DiffOutputControl) JdbcConnection(liquibase.database.jvm.JdbcConnection) Executor(liquibase.executor.Executor) ValidationFailedException(liquibase.exception.ValidationFailedException) ExecutorService(liquibase.executor.ExecutorService) 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 34 with DatabaseConnection

use of liquibase.database.DatabaseConnection in project collect by openforis.

the class LiquibaseRelationalSchemaCreator method getDatabaseImplementation.

private Database getDatabaseImplementation(Connection targetConn) throws DatabaseException {
    DatabaseConnection dbconn = new JdbcConnection(targetConn);
    DatabaseFactory dbFactory = DatabaseFactory.getInstance();
    Database rdb = dbFactory.findCorrectDatabaseImplementation(dbconn);
    if (rdb instanceof PostgresDatabase) {
        rdb = new CollectPostgresDatabase(dbconn);
    }
    return rdb;
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseFactory(liquibase.database.DatabaseFactory) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 35 with DatabaseConnection

use of liquibase.database.DatabaseConnection in project iaf by ibissource.

the class LiquibaseMigrator method createMigrator.

private Liquibase createMigrator(Resource resource) throws SQLException, LiquibaseException {
    if (resource == null) {
        throw new LiquibaseException("no resource provided");
    }
    ResourceAccessor resourceAccessor = new LiquibaseResourceAccessor(resource);
    DatabaseConnection connection = getDatabaseConnection();
    return new Liquibase(resource.getSystemId(), resourceAccessor, connection);
}
Also used : Liquibase(liquibase.Liquibase) ResourceAccessor(liquibase.resource.ResourceAccessor) DatabaseConnection(liquibase.database.DatabaseConnection) LiquibaseException(liquibase.exception.LiquibaseException)

Aggregations

DatabaseConnection (liquibase.database.DatabaseConnection)36 JdbcConnection (liquibase.database.jvm.JdbcConnection)20 DatabaseException (liquibase.exception.DatabaseException)15 OfflineConnection (liquibase.database.OfflineConnection)12 Database (liquibase.database.Database)11 SQLException (java.sql.SQLException)10 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 Test (org.junit.Test)5 Statement (java.sql.Statement)4 DatabaseFactory (liquibase.database.DatabaseFactory)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 CallableStatement (java.sql.CallableStatement)3 Connection (java.sql.Connection)3 Liquibase (liquibase.Liquibase)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 SqlStatement (liquibase.statement.SqlStatement)3 Method (java.lang.reflect.Method)2 BigInteger (java.math.BigInteger)2 DatabaseMetaData (java.sql.DatabaseMetaData)2