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);
}
}
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);
}
}
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");
}
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;
}
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;
}
Aggregations