Search in sources :

Example 6 with DatabaseConnection

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

the class HsqlDatabaseTest method testNotUsingOracleSyntax.

public void testNotUsingOracleSyntax() {
    HsqlDatabase database = new HsqlDatabase();
    DatabaseConnection conn = mock(DatabaseConnection.class);
    when(conn.getURL()).thenReturn("jdbc:hsqldb:mem:testdb");
    database.setConnection(conn);
    assertFalse("Using oracle syntax", database.isUsingOracleSyntax());
}
Also used : DatabaseConnection(liquibase.database.DatabaseConnection)

Example 7 with DatabaseConnection

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

the class Migrator method migrate.

/**
 * <p>migrate</p>
 *
 * @param migration a {@link org.opennms.core.schema.Migration} object.
 * @throws org.opennms.core.schema.MigrationException if any.
 */
public void migrate(final Migration migration) throws MigrationException {
    Connection connection = null;
    DatabaseConnection dbConnection = null;
    try {
        connection = m_dataSource.getConnection();
        dbConnection = new JdbcConnection(connection);
        ResourceAccessor accessor = migration.getAccessor();
        if (accessor == null)
            accessor = new SpringResourceAccessor();
        final Liquibase liquibase = new Liquibase(migration.getChangeLog(), accessor, dbConnection);
        liquibase.setChangeLogParameter("install.database.admin.user", migration.getAdminUser());
        liquibase.setChangeLogParameter("install.database.admin.password", migration.getAdminPassword());
        liquibase.setChangeLogParameter("install.database.user", migration.getDatabaseUser());
        liquibase.getDatabase().setDefaultSchemaName(migration.getSchemaName());
        final String contexts = System.getProperty("opennms.contexts", "production");
        liquibase.update(contexts);
    } catch (final Throwable e) {
        throw new MigrationException("unable to migrate the database", e);
    } finally {
        cleanUpDatabase(connection, dbConnection, null, null);
    }
}
Also used : Liquibase(liquibase.Liquibase) ResourceAccessor(liquibase.resource.ResourceAccessor) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 8 with DatabaseConnection

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

the class DbChecker method checkDb.

@PostConstruct
public void checkDb() {
    try {
        DatabaseConnection connection = new JdbcConnection(dataSource.getConnection());
        Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(connection);
        Liquibase liquibase = new Liquibase(script, new ClassLoaderResourceAccessor(), database);
        LOG.info("Checking database: {} URL:{}", database.getDatabaseProductName(), database.getConnection().getURL());
        List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(null, liquibase.getChangeLogParameters().getLabels());
        if (!changeSets.isEmpty()) {
            StringBuilder errors = new StringBuilder();
            for (ChangeSet changeSet : changeSets) {
                errors.append('>').append(changeSet.toString()).append('\n');
            }
            throw new Error("Database version mismatch. Check liquibase status. Errors:\n" + errors.toString() + database.getDatabaseProductName() + ", " + database);
        }
    } catch (liquibase.exception.LiquibaseException | SQLException e) {
        throw new Error("Database not ok, aborting startup.", e);
    }
    LOG.info("Liquibase ok");
}
Also used : SQLException(java.sql.SQLException) JdbcConnection(liquibase.database.jvm.JdbcConnection) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ChangeSet(liquibase.changelog.ChangeSet) PostConstruct(javax.annotation.PostConstruct)

Example 9 with DatabaseConnection

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

the class AbstractLiquibaseCommand method createDatabase.

private Database createDatabase(ManagedDataSource dataSource, Namespace namespace) throws SQLException, LiquibaseException {
    final DatabaseConnection conn = new JdbcConnection(dataSource.getConnection());
    final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(conn);
    final String catalogName = namespace.getString("catalog");
    final String schemaName = namespace.getString("schema");
    if (database.supportsCatalogs() && catalogName != null) {
        database.setDefaultCatalogName(catalogName);
        database.setOutputDefaultCatalog(true);
    }
    if (database.supportsSchemas() && schemaName != null) {
        database.setDefaultSchemaName(schemaName);
        database.setOutputDefaultSchema(true);
    }
    return database;
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 10 with DatabaseConnection

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

the class JdbcExecutor method execute.

public Object execute(StatementCallback action, List<SqlVisitor> sqlVisitors) throws DatabaseException {
    Scope.getCurrentScope().getLog(getClass()).fine("Executing with the '" + getName() + "' executor");
    DatabaseConnection con = database.getConnection();
    Statement stmt = null;
    try {
        if (con instanceof OfflineConnection) {
            throw new DatabaseException("Cannot execute commands against an offline database");
        }
        stmt = ((JdbcConnection) con).getUnderlyingConnection().createStatement();
        Statement stmtToUse = stmt;
        return action.doInStatement(stmtToUse);
    } catch (SQLException ex) {
        // Release Connection early, to avoid potential connection pool deadlock
        // in the case when the exception translator hasn't been initialized yet.
        JdbcUtil.closeStatement(stmt);
        stmt = null;
        String url;
        if (con.isClosed()) {
            url = "CLOSED CONNECTION";
        } else {
            url = con.getURL();
        }
        throw new DatabaseException("Error executing SQL " + StringUtil.join(applyVisitors(action.getStatement(), sqlVisitors), "; on " + url) + ": " + ex.getMessage(), ex);
    } finally {
        JdbcUtil.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) CompoundStatement(liquibase.statement.CompoundStatement) ExecutablePreparedStatement(liquibase.statement.ExecutablePreparedStatement) SqlStatement(liquibase.statement.SqlStatement) CallableSqlStatement(liquibase.statement.CallableSqlStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException)

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