Search in sources :

Example 16 with DatabaseConnection

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

the class H2Database method setConnection.

@Override
public void setConnection(DatabaseConnection conn) {
    Connection sqlConn = null;
    if (!(conn instanceof OfflineConnection)) {
        try {
            if (conn instanceof JdbcConnection) {
                Method wrappedConn = conn.getClass().getMethod("getWrappedConnection");
                wrappedConn.setAccessible(true);
                sqlConn = (Connection) wrappedConn.invoke(conn);
            }
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
        if (sqlConn != null) {
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                statement = sqlConn.createStatement();
                resultSet = statement.executeQuery("SELECT SCHEMA()");
                String schemaName = null;
                if (resultSet.next()) {
                    schemaName = resultSet.getString(1);
                }
                if (schemaName != null) {
                    this.connectionSchemaName = schemaName;
                }
            } catch (SQLException e) {
                Scope.getCurrentScope().getLog(getClass()).info("Could not read current schema name: " + e.getMessage());
            } finally {
                JdbcUtil.close(resultSet, statement);
            }
        }
    }
    super.setConnection(conn);
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) ResultSet(java.sql.ResultSet) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Method(java.lang.reflect.Method) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) DateParseException(liquibase.exception.DateParseException) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ParseException(java.text.ParseException)

Example 17 with DatabaseConnection

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

the class CreateSequenceGeneratorTest method postgresDatabaseSupportAsStructureByVersion.

@Test
public void postgresDatabaseSupportAsStructureByVersion() throws Exception {
    DatabaseConnection dbConnection = mock(DatabaseConnection.class);
    ValidationErrors errors;
    PostgresDatabase postgresDatabase = spy(new PostgresDatabase());
    postgresDatabase.setConnection(dbConnection);
    doReturn(SEQUENCE_NAME).when(postgresDatabase).escapeSequenceName(CATALOG_NAME, SCHEMA_NAME, SEQUENCE_NAME);
    // verify that for version < 10 validate() method returns error
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(6);
    CreateSequenceStatement createSequenceStatement = createSampleSqlStatement();
    createSequenceStatement.setStartValue(new BigInteger("1"));
    createSequenceStatement.setDataType("int");
    errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
    assertThat(errors.getErrorMessages()).contains("AS is not allowed on postgresql");
    // verify that if no version is available the validate() method passes
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenThrow(DatabaseException.class);
    errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
    assertThat(errors.getErrorMessages()).isEmpty();
    // verify that for version >= 10 the validate() method passes
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(10);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(0);
    errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
    assertThat(errors.getErrorMessages()).isEmpty();
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) ValidationErrors(liquibase.exception.ValidationErrors) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) BigInteger(java.math.BigInteger) DatabaseConnection(liquibase.database.DatabaseConnection) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

Example 18 with DatabaseConnection

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

the class Liquibase method outputHeader.

public void outputHeader(String message) throws DatabaseException {
    Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("logging", database);
    executor.comment("*********************************************************************");
    executor.comment(message);
    executor.comment("*********************************************************************");
    executor.comment("Change Log: " + changeLogFile);
    executor.comment("Ran at: " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()));
    DatabaseConnection connection = getDatabase().getConnection();
    if (connection != null) {
        executor.comment("Against: " + connection.getConnectionUserName() + "@" + connection.getURL());
    }
    executor.comment("Liquibase version: " + LiquibaseUtil.getBuildVersionInfo());
    executor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
    if ((database instanceof MSSQLDatabase) && (database.getDefaultCatalogName() != null)) {
        executor.execute(new RawSqlStatement("USE " + database.escapeObjectName(database.getDefaultCatalogName(), Catalog.class) + ";"));
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) ExecutorService(liquibase.executor.ExecutorService) DatabaseConnection(liquibase.database.DatabaseConnection) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 19 with DatabaseConnection

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

the class LiquibaseTest method constructor_createDatabaseInstanceFromConnection.

@Test
public void constructor_createDatabaseInstanceFromConnection() throws LiquibaseException {
    DatabaseConnection databaseConnection = mock(DatabaseConnection.class);
    Database database = mockDatabase;
    try {
        DatabaseFactory.setInstance(mock(DatabaseFactory.class));
        when(DatabaseFactory.getInstance().findCorrectDatabaseImplementation(databaseConnection)).thenReturn(database);
        Liquibase liquibase = new Liquibase("com/example/test.xml", mockResourceAccessor, databaseConnection);
        assertSame("Liquibase constructor passing connection did not find the correct database implementation", database, liquibase.getDatabase());
    } finally {
        DatabaseFactory.reset();
    }
}
Also used : DatabaseFactory(liquibase.database.DatabaseFactory) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) Test(org.junit.Test)

Example 20 with DatabaseConnection

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

the class SpringLiquibase method createDatabase.

@Override
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);
    }
    DatabaseFactory factory = DatabaseFactory.getInstance();
    overrideHsqlDbImplementation(factory);
    Database database = factory.findCorrectDatabaseImplementation(liquibaseConnection);
    if (StringUtils.trimToNull(this.defaultSchema) != null) {
        database.setDefaultSchemaName(this.defaultSchema);
    }
    return database;
}
Also used : DatabaseFactory(liquibase.database.DatabaseFactory) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection)

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