Search in sources :

Example 21 with OfflineConnection

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

the class AbstractDb2Database method getDefaultCatalogName.

@Override
public String getDefaultCatalogName() {
    if (defaultCatalogName != null) {
        return defaultCatalogName;
    }
    if (defaultSchemaName != null) {
        return defaultSchemaName;
    }
    if (getConnection() == null) {
        return null;
    }
    if (getConnection() instanceof OfflineConnection) {
        return ((OfflineConnection) getConnection()).getSchema();
    }
    Statement stmt = null;
    ResultSet rs = null;
    try {
        stmt = ((JdbcConnection) getConnection()).createStatement();
        rs = stmt.executeQuery("select current schema from sysibm.sysdummy1");
        if (rs.next()) {
            String result = rs.getString(1);
            if (result != null) {
                this.defaultSchemaName = StringUtil.trimToNull(result);
            } else {
                this.defaultSchemaName = StringUtil.trimToNull(super.getDefaultSchemaName());
            }
        }
    } catch (Exception e) {
        throw new RuntimeException("Could not determine current schema", e);
    } finally {
        JdbcUtil.close(rs, stmt);
    }
    return defaultSchemaName;
}
Also used : GetViewDefinitionStatement(liquibase.statement.core.GetViewDefinitionStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) OfflineConnection(liquibase.database.OfflineConnection) DatabaseException(liquibase.exception.DatabaseException) DateParseException(liquibase.exception.DateParseException) ParseException(java.text.ParseException)

Example 22 with OfflineConnection

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

the class YamlSnapshotParser method parse.

@Override
public DatabaseSnapshot parse(String path, ResourceAccessor resourceAccessor) throws LiquibaseParseException {
    Yaml yaml = new Yaml(new SafeConstructor());
    try (InputStream stream = resourceAccessor.openStream(null, path)) {
        if (stream == null) {
            throw new LiquibaseParseException(path + " does not exist");
        }
        Map parsedYaml = getParsedYamlFromInputStream(yaml, stream);
        Map rootList = (Map) parsedYaml.get("snapshot");
        if (rootList == null) {
            throw new LiquibaseParseException("Could not find root snapshot node");
        }
        String shortName = (String) ((Map) rootList.get("database")).get("shortName");
        Database database = DatabaseFactory.getInstance().getDatabase(shortName).getClass().getConstructor().newInstance();
        database.setConnection(new OfflineConnection("offline:" + shortName, null));
        DatabaseSnapshot snapshot = new RestoredDatabaseSnapshot(database);
        ParsedNode snapshotNode = new ParsedNode(null, "snapshot");
        snapshotNode.setValue(rootList);
        Map metadata = (Map) rootList.get("metadata");
        if (metadata != null) {
            snapshot.getMetadata().putAll(metadata);
        }
        snapshot.load(snapshotNode, resourceAccessor);
        return snapshot;
    } catch (LiquibaseParseException e) {
        throw (LiquibaseParseException) e;
    } catch (Exception e) {
        throw new LiquibaseParseException(e);
    }
}
Also used : LiquibaseParseException(liquibase.exception.LiquibaseParseException) ParsedNode(liquibase.parser.core.ParsedNode) InputStream(java.io.InputStream) Database(liquibase.database.Database) SafeConstructor(org.yaml.snakeyaml.constructor.SafeConstructor) OfflineConnection(liquibase.database.OfflineConnection) Map(java.util.Map) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) Yaml(org.yaml.snakeyaml.Yaml) LiquibaseParseException(liquibase.exception.LiquibaseParseException) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot)

Example 23 with OfflineConnection

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

Aggregations

OfflineConnection (liquibase.database.OfflineConnection)23 DatabaseException (liquibase.exception.DatabaseException)14 DatabaseConnection (liquibase.database.DatabaseConnection)12 JdbcConnection (liquibase.database.jvm.JdbcConnection)12 ResultSet (java.sql.ResultSet)6 SQLException (java.sql.SQLException)6 Statement (java.sql.Statement)6 Database (liquibase.database.Database)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CallableStatement (java.sql.CallableStatement)3 ParseException (java.text.ParseException)3 DateParseException (liquibase.exception.DateParseException)3 ExecutorService (liquibase.executor.ExecutorService)3 Method (java.lang.reflect.Method)2 Connection (java.sql.Connection)2 Map (java.util.Map)2 DatabaseFactory (liquibase.database.DatabaseFactory)2 Executor (liquibase.executor.Executor)2 ParsedNode (liquibase.parser.core.ParsedNode)2