Search in sources :

Example 1 with DatabaseFactory

use of liquibase.database.DatabaseFactory in project spring-boot by spring-projects.

the class LiquibaseEndpoint method invoke.

@Override
public List<LiquibaseReport> invoke() {
    List<LiquibaseReport> reports = new ArrayList<>();
    DatabaseFactory factory = DatabaseFactory.getInstance();
    StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
    for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
        try {
            DataSource dataSource = entry.getValue().getDataSource();
            JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
            try {
                Database database = factory.findCorrectDatabaseImplementation(connection);
                reports.add(new LiquibaseReport(entry.getKey(), service.queryDatabaseChangeLogTable(database)));
            } finally {
                connection.close();
            }
        } catch (Exception ex) {
            throw new IllegalStateException("Unable to get Liquibase changelog", ex);
        }
    }
    return reports;
}
Also used : LiquibaseReport(org.springframework.boot.actuate.endpoint.LiquibaseEndpoint.LiquibaseReport) ArrayList(java.util.ArrayList) JdbcConnection(liquibase.database.jvm.JdbcConnection) StandardChangeLogHistoryService(liquibase.changelog.StandardChangeLogHistoryService) DataSource(javax.sql.DataSource) DatabaseFactory(liquibase.database.DatabaseFactory) SpringLiquibase(liquibase.integration.spring.SpringLiquibase) Database(liquibase.database.Database) Map(java.util.Map)

Example 2 with DatabaseFactory

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

the class DatabaseType method createDatabase.

public Database createDatabase(ClassLoader classLoader) {
    logParameters();
    validateParameters();
    try {
        DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
        if (databaseClass != null) {
            Database databaseInstance = (Database) ClasspathUtils.newInstance(databaseClass, classLoader, Database.class);
            databaseFactory.register(databaseInstance);
        }
        DatabaseConnection jdbcConnection;
        if (getUrl().startsWith("offline:")) {
            jdbcConnection = new OfflineConnection(getUrl(), new ClassLoaderResourceAccessor(classLoader));
        } else {
            Driver driver = (Driver) ClasspathUtils.newInstance(getDriver(), classLoader, Driver.class);
            if (driver == null) {
                throw new BuildException("Unable to create Liquibase Database instance. Could not instantiate the JDBC driver.");
            }
            Properties connectionProps = new Properties();
            String user = getUser();
            if (user != null && !user.isEmpty()) {
                connectionProps.setProperty(USER, user);
            }
            String password = getPassword();
            if (password != null && !password.isEmpty()) {
                connectionProps.setProperty(PASSWORD, password);
            }
            ConnectionProperties connectionProperties = getConnectionProperties();
            if (connectionProperties != null) {
                connectionProps.putAll(connectionProperties.buildProperties());
            }
            Connection connection = driver.connect(getUrl(), connectionProps);
            if (connection == null) {
                throw new BuildException("Unable to create Liquibase Database instance. Could not connect to the database.");
            }
            jdbcConnection = new JdbcConnection(connection);
        }
        Database database = databaseFactory.findCorrectDatabaseImplementation(jdbcConnection);
        String schemaName = getDefaultSchemaName();
        if (schemaName != null) {
            database.setDefaultSchemaName(schemaName);
        }
        String catalogName = getDefaultCatalogName();
        if (catalogName != null) {
            database.setDefaultCatalogName(catalogName);
        }
        String currentDateTimeFunction = getCurrentDateTimeFunction();
        if (currentDateTimeFunction != null) {
            database.setCurrentDateTimeFunction(currentDateTimeFunction);
        }
        database.setOutputDefaultSchema(isOutputDefaultSchema());
        database.setOutputDefaultCatalog(isOutputDefaultCatalog());
        String liquibaseSchemaName = getLiquibaseSchemaName();
        if (liquibaseSchemaName != null) {
            database.setLiquibaseSchemaName(liquibaseSchemaName);
        }
        String liquibaseCatalogName = getLiquibaseCatalogName();
        if (liquibaseCatalogName != null) {
            database.setLiquibaseCatalogName(liquibaseCatalogName);
        }
        String databaseChangeLogTableName = getDatabaseChangeLogTableName();
        if (databaseChangeLogTableName != null) {
            database.setDatabaseChangeLogTableName(databaseChangeLogTableName);
        }
        String databaseChangeLogLockTableName = getDatabaseChangeLogLockTableName();
        if (databaseChangeLogLockTableName != null) {
            database.setDatabaseChangeLogLockTableName(databaseChangeLogLockTableName);
        }
        String liquibaseTablespaceName = getLiquibaseTablespaceName();
        if (liquibaseTablespaceName != null) {
            database.setLiquibaseTablespaceName(liquibaseTablespaceName);
        }
        return database;
    } catch (SQLException e) {
        throw new BuildException("Unable to create Liquibase database instance. A JDBC error occurred. " + e.toString(), e);
    } catch (DatabaseException e) {
        throw new BuildException("Unable to create Liquibase database instance. " + e.toString(), e);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) OfflineConnection(liquibase.database.OfflineConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) OfflineConnection(liquibase.database.OfflineConnection) Properties(java.util.Properties) DatabaseFactory(liquibase.database.DatabaseFactory) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) BuildException(org.apache.tools.ant.BuildException) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with DatabaseFactory

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

the class LockServiceFactoryTest method getLockService.

@Test
public void getLockService() {
    final Database oracle1 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database oracle2 = new OracleDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    final Database mysql = new MySQLDatabase() {

        @Override
        public boolean equals(Object o) {
            return o == this;
        }
    };
    DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
    databaseFactory.register(oracle1);
    databaseFactory.register(oracle2);
    databaseFactory.register(mysql);
    LockServiceFactory lockServiceFactory = LockServiceFactory.getInstance();
    assertNotNull(lockServiceFactory.getLockService(oracle1));
    assertNotNull(lockServiceFactory.getLockService(oracle2));
    assertNotNull(lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(oracle1) == lockServiceFactory.getLockService(oracle1));
    assertTrue(lockServiceFactory.getLockService(oracle2) == lockServiceFactory.getLockService(oracle2));
    assertTrue(lockServiceFactory.getLockService(mysql) == lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(oracle1) != lockServiceFactory.getLockService(oracle2));
    assertTrue(lockServiceFactory.getLockService(oracle1) != lockServiceFactory.getLockService(mysql));
    assertTrue(lockServiceFactory.getLockService(getMockDatabase()) instanceof MockLockService);
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) MockLockService(liquibase.lockservice.ext.MockLockService) DatabaseFactory(liquibase.database.DatabaseFactory) MockDatabase(liquibase.sdk.database.MockDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) Test(org.junit.Test)

Example 4 with DatabaseFactory

use of liquibase.database.DatabaseFactory 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

Database (liquibase.database.Database)4 DatabaseFactory (liquibase.database.DatabaseFactory)4 JdbcConnection (liquibase.database.jvm.JdbcConnection)3 DatabaseConnection (liquibase.database.DatabaseConnection)2 OfflineConnection (liquibase.database.OfflineConnection)2 Connection (java.sql.Connection)1 Driver (java.sql.Driver)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Properties (java.util.Properties)1 DataSource (javax.sql.DataSource)1 StandardChangeLogHistoryService (liquibase.changelog.StandardChangeLogHistoryService)1 MySQLDatabase (liquibase.database.core.MySQLDatabase)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 DatabaseException (liquibase.exception.DatabaseException)1 SpringLiquibase (liquibase.integration.spring.SpringLiquibase)1 MockLockService (liquibase.lockservice.ext.MockLockService)1 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)1 MockDatabase (liquibase.sdk.database.MockDatabase)1