Search in sources :

Example 26 with DatabaseConnection

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

the class DatabaseSnapshot method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    try {
        Map<String, DatabaseObject> referencedObjects = new HashMap<>();
        Map<String, DatabaseObject> objects = new HashMap<>();
        Map<String, DatabaseObject> allObjects = new HashMap<>();
        ParsedNode databaseNode = parsedNode.getChild(null, "database");
        DatabaseConnection connection = getDatabase().getConnection();
        if ((databaseNode != null) && (connection instanceof OfflineConnection)) {
            ((OfflineConnection) connection).setDatabaseMajorVersion(databaseNode.getChildValue(null, "majorVersion", Integer.class));
            ((OfflineConnection) connection).setDatabaseMinorVersion(databaseNode.getChildValue(null, "minorVersion", Integer.class));
            ((OfflineConnection) connection).setProductVersion(databaseNode.getChildValue(null, "productVersion", String.class));
            ((OfflineConnection) connection).setConnectionUserName(databaseNode.getChildValue(null, "user", String.class));
        }
        loadObjects(referencedObjects, allObjects, parsedNode.getChild(null, "referencedObjects"), resourceAccessor);
        loadObjects(objects, allObjects, parsedNode.getChild(null, "objects"), resourceAccessor);
        for (DatabaseObject object : allObjects.values()) {
            for (String attr : new ArrayList<>(object.getAttributes())) {
                Object value = object.getAttribute(attr, Object.class);
                if ((value instanceof String) && allObjects.containsKey(value)) {
                    if (ObjectUtil.hasProperty(object, attr)) {
                        ObjectUtil.setProperty(object, attr, allObjects.get(value));
                    } else {
                        object.setAttribute(attr, allObjects.get(value));
                    }
                } else if ((value instanceof Collection) && !((Collection) value).isEmpty() && allObjects.containsKey(((Collection) value).iterator().next())) {
                    List<DatabaseObject> newList = new ArrayList<DatabaseObject>();
                    for (String element : (Collection<String>) value) {
                        newList.add(allObjects.get(element));
                    }
                    if (ObjectUtil.hasProperty(object, attr)) {
                        ObjectUtil.setProperty(object, attr, newList);
                    } else {
                        object.setAttribute(attr, newList);
                    }
                } else {
                    if ((value != null) && ObjectUtil.hasProperty(object, attr)) {
                        if ((value instanceof byte[]) && ObjectUtil.getPropertyType(object, attr).equals(String.class)) {
                            value = new String((byte[]) value, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
                        }
                        object.setAttribute(attr, null);
                        ObjectUtil.setProperty(object, attr, value);
                    }
                }
            }
        }
        for (DatabaseObject object : objects.values()) {
            this.allFound.add(object);
        }
        for (DatabaseObject object : referencedObjects.values()) {
            this.referencedObjects.add(object);
        }
    } catch (Exception e) {
        throw new ParsedNodeException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) OfflineConnection(liquibase.database.OfflineConnection) ParsedNodeException(liquibase.parser.core.ParsedNodeException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseObjectCollection(liquibase.structure.DatabaseObjectCollection) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseObject(liquibase.structure.DatabaseObject) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 27 with DatabaseConnection

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

the class SqlPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet, ChangeExecListener changeExecListener) throws PreconditionFailedException, PreconditionErrorException {
    DatabaseConnection connection = database.getConnection();
    try {
        Object oResult = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database).queryForObject(new RawSqlStatement(getSql().replaceFirst(";$", "")), String.class);
        if (oResult == null) {
            throw new PreconditionFailedException("No rows returned from SQL Precondition", changeLog, this);
        }
        String result = oResult.toString();
        String expectedResult = getExpectedResult();
        if (!expectedResult.equals(result)) {
            throw new PreconditionFailedException("SQL Precondition failed.  Expected '" + expectedResult + "' got '" + result + "'", changeLog, this);
        }
    } catch (DatabaseException e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) DatabaseConnection(liquibase.database.DatabaseConnection)

Example 28 with DatabaseConnection

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

the class CreateSequenceGeneratorTest method postgresDatabaseSupportIfNotExistsByVersion.

@Test
public void postgresDatabaseSupportIfNotExistsByVersion() throws Exception {
    DatabaseConnection dbConnection = mock(DatabaseConnection.class);
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(4);
    PostgresDatabase database = spy(new PostgresDatabase());
    database.setConnection(dbConnection);
    doReturn(SEQUENCE_NAME).when(database).escapeSequenceName(CATALOG_NAME, SCHEMA_NAME, SEQUENCE_NAME);
    CreateSequenceStatement createSequenceStatement = createSampleSqlStatement();
    createSequenceStatement.setStartValue(new BigInteger("1"));
    // verify that for version <= 9.4 no IF NOT EXISTS is not in the statement
    Sql[] sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).doesNotContain("IF NOT EXISTS");
    // verify that if no version is available the optional no IF NOT EXISTS is not in the statement
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenThrow(DatabaseException.class);
    sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).doesNotContain("IF NOT EXISTS");
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(5);
    sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).contains("IF NOT EXISTS");
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) BigInteger(java.math.BigInteger) DatabaseConnection(liquibase.database.DatabaseConnection) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

Example 29 with DatabaseConnection

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

the class InternalDropAllCommandStep method getHubConnection.

private Connection getHubConnection(CommandScope commandScope) {
    String apiKey = StringUtil.trimToNull(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue());
    if (apiKey == null) {
        return null;
    }
    Database database = commandScope.getArgumentValue(DATABASE_ARG);
    DatabaseConnection dbConnection = database.getConnection();
    Connection connection = new Connection();
    connection.setId(commandScope.getArgumentValue(HUB_CONNECTION_ID_ARG));
    connection.setJdbcUrl(dbConnection.getURL());
    return connection;
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseConnection(liquibase.database.DatabaseConnection)

Example 30 with DatabaseConnection

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

the class OracleDatabase method validate.

@Override
public ValidationErrors validate() {
    ValidationErrors errors = super.validate();
    DatabaseConnection connection = getConnection();
    if ((connection == null) || (connection instanceof OfflineConnection)) {
        // noinspection HardCodedStringLiteral
        Scope.getCurrentScope().getLog(getClass()).info("Cannot validate offline database");
        return errors;
    }
    if (!canAccessDbaRecycleBin()) {
        errors.addWarning(getDbaRecycleBinWarning());
    }
    return errors;
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) DatabaseConnection(liquibase.database.DatabaseConnection) 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