use of liquibase.executor.Executor in project liquibase by liquibase.
the class DiffToChangeLog method addDependencies.
/**
* Adds dependencies to the graph as schema.object_name.
*/
protected void addDependencies(DependencyUtil.DependencyGraph<String> graph, List<String> schemas, Collection<DatabaseObject> missingObjects, Database database) throws DatabaseException {
if (database instanceof DB2Database) {
Executor executor = ExecutorService.getInstance().getExecutor(database);
List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement("select TABSCHEMA, TABNAME, BSCHEMA, BNAME from syscat.tabdep where (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "TABSCHEMA='" + obj + "'";
}
}) + ")"));
for (Map<String, ?> row : rs) {
String tabName = StringUtils.trimToNull((String) row.get("TABSCHEMA")) + "." + StringUtils.trimToNull((String) row.get("TABNAME"));
String bName = StringUtils.trimToNull((String) row.get("BSCHEMA")) + "." + StringUtils.trimToNull((String) row.get("BNAME"));
graph.add(bName, tabName);
}
} else if (database instanceof OracleDatabase) {
Executor executor = ExecutorService.getInstance().getExecutor(database);
List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement("select OWNER, NAME, REFERENCED_OWNER, REFERENCED_NAME from DBA_DEPENDENCIES where REFERENCED_OWNER != 'SYS' AND NOT(NAME LIKE 'BIN$%') AND NOT(OWNER = REFERENCED_OWNER AND NAME = REFERENCED_NAME) AND (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "OWNER='" + obj + "'";
}
}) + ")"));
for (Map<String, ?> row : rs) {
String tabName = StringUtils.trimToNull((String) row.get("OWNER")) + "." + StringUtils.trimToNull((String) row.get("NAME"));
String bName = StringUtils.trimToNull((String) row.get("REFERENCED_OWNER")) + "." + StringUtils.trimToNull((String) row.get("REFERENCED_NAME"));
graph.add(bName, tabName);
}
} else if (database instanceof MSSQLDatabase && database.getDatabaseMajorVersion() >= 9) {
Executor executor = ExecutorService.getInstance().getExecutor(database);
String sql = "select object_schema_name(referencing_id) as referencing_schema_name, object_name(referencing_id) as referencing_name, object_name(referenced_id) as referenced_name, object_schema_name(referenced_id) as referenced_schema_name from sys.sql_expression_dependencies depz where (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "object_schema_name(referenced_id)='" + obj + "'";
}
}) + ")";
sql += " UNION select object_schema_name(object_id) as referencing_schema_name, object_name(object_id) as referencing_name, object_name(parent_object_id) as referenced_name, object_schema_name(parent_object_id) as referenced_schema_name " + "from sys.objects " + "where parent_object_id > 0 " + "and is_ms_shipped=0 " + "and (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "object_schema_name(object_id)='" + obj + "'";
}
}) + ")";
sql += " UNION select object_schema_name(fk.object_id) as referencing_schema_name, fk.name as referencing_name, i.name as referenced_name, object_schema_name(i.object_id) as referenced_schema_name " + "from sys.foreign_keys fk " + "join sys.indexes i on fk.referenced_object_id=i.object_id and fk.key_index_id=i.index_id " + "where fk.is_ms_shipped=0 " + "and (" + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "object_schema_name(fk.object_id)='" + obj + "'";
}
}) + ")";
sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, object_name(i.object_id) as referencing_name, s.name as referenced_name, null as referenced_schema_name " + "from sys.indexes i " + "join sys.partition_schemes s on i.data_space_id = s.data_space_id";
sql += " UNION select null as referencing_schema_name, s.name as referencing_name, f.name as referenced_name, null as referenced_schema_name from sys.partition_functions f " + "join sys.partition_schemes s on s.function_id=f.function_id";
sql += " UNION select null as referencing_schema_name, s.name as referencing_name, fg.name as referenced_name, null as referenced_schema_name from sys.partition_schemes s " + "join sys.destination_data_spaces ds on s.data_space_id=ds.partition_scheme_id " + "join sys.filegroups fg on ds.data_space_id=fg.data_space_id";
//get data file -> filegroup dependencies
sql += " UNION select distinct null as referencing_schema_name, f.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.database_files f " + "join sys.data_spaces ds on f.data_space_id=ds.data_space_id " + "where f.data_space_id > 1";
//get table -> filestream dependencies
sql += " UNION select object_schema_name(t.object_id) as referencing_schema_name, t.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.tables t " + "join sys.data_spaces ds on t.filestream_data_space_id=ds.data_space_id " + "where t.filestream_data_space_id > 1";
//get table -> filestream dependencies
sql += " UNION select object_schema_name(t.object_id) as referencing_schema_name, t.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.tables t " + "join sys.data_spaces ds on t.lob_data_space_id=ds.data_space_id " + "where t.lob_data_space_id > 1";
//get index -> filegroup dependencies
sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, i.name as referencing_name, ds.name as referenced_name, null as referenced_schema_name from sys.indexes i " + "join sys.data_spaces ds on i.data_space_id=ds.data_space_id " + "where i.data_space_id > 1";
//get index -> table dependencies
sql += " UNION select object_schema_name(i.object_id) as referencing_schema_name, i.name as referencing_name, object_name(i.object_id) as referenced_name, object_schema_name(i.object_id) as referenced_schema_name from sys.indexes i " + "where " + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "object_schema_name(i.object_id)='" + obj + "'";
}
});
//get schema -> base object dependencies
sql += " UNION SELECT SCHEMA_NAME(SCHEMA_ID) as referencing_schema_name, name as referencing_name, PARSENAME(BASE_OBJECT_NAME,1) AS referenced_name, (CASE WHEN PARSENAME(BASE_OBJECT_NAME,2) IS NULL THEN schema_name(schema_id) else PARSENAME(BASE_OBJECT_NAME,2) END) AS referenced_schema_name FROM SYS.SYNONYMS WHERE is_ms_shipped='false' AND " + StringUtils.join(schemas, " OR ", new StringUtils.StringUtilsFormatter<String>() {
@Override
public String toString(String obj) {
return "SCHEMA_NAME(SCHEMA_ID)='" + obj + "'";
}
});
List<Map<String, ?>> rs = executor.queryForList(new RawSqlStatement(sql));
if (rs.size() > 0) {
for (Map<String, ?> row : rs) {
String bName = StringUtils.trimToNull((String) row.get("REFERENCED_SCHEMA_NAME")) + "." + StringUtils.trimToNull((String) row.get("REFERENCED_NAME"));
String tabName = StringUtils.trimToNull((String) row.get("REFERENCING_SCHEMA_NAME")) + "." + StringUtils.trimToNull((String) row.get("REFERENCING_NAME"));
if (!bName.equals(tabName)) {
graph.add(bName, tabName);
}
}
}
}
}
use of liquibase.executor.Executor in project liquibase by liquibase.
the class PendingSQLWriter method writeBody.
@Override
protected void writeBody(Writer fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) throws IOException, DatabaseHistoryException, DatabaseException {
Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), fileWriter, database);
ExecutorService.getInstance().setExecutor(database, loggingExecutor);
try {
if (changesToRun.size() == 0) {
fileWriter.append("<b>NONE</b>");
}
fileWriter.append("<code><pre>");
ChangeSet lastRunChangeSet = null;
for (Change change : changesToRun) {
ChangeSet thisChangeSet = change.getChangeSet();
if (thisChangeSet.equals(lastRunChangeSet)) {
continue;
}
lastRunChangeSet = thisChangeSet;
String anchor = thisChangeSet.toString(false).replaceAll("\\W", "_");
fileWriter.append("<a name='").append(anchor).append("'/>");
try {
thisChangeSet.execute(databaseChangeLog, null, this.database);
} catch (MigrationFailedException e) {
fileWriter.append("EXECUTION ERROR: ").append(ChangeFactory.getInstance().getChangeMetaData(change).getDescription()).append(": ").append(e.getMessage()).append("\n\n");
}
}
fileWriter.append("</pre></code>");
} finally {
ExecutorService.getInstance().setExecutor(database, oldTemplate);
}
}
use of liquibase.executor.Executor in project liquibase by liquibase.
the class SybaseDatabase method getViewDefinition.
@Override
public String getViewDefinition(CatalogAndSchema schema, String viewName) throws DatabaseException {
schema = schema.customize(this);
GetViewDefinitionStatement statement = new GetViewDefinitionStatement(schema.getCatalogName(), schema.getSchemaName(), viewName);
Executor executor = ExecutorService.getInstance().getExecutor(this);
@SuppressWarnings("unchecked") List<String> definitionRows = (List<String>) executor.queryForList(statement, String.class);
StringBuilder definition = new StringBuilder();
for (String d : definitionRows) {
definition.append(d);
}
return definition.toString();
}
use of liquibase.executor.Executor in project liquibase by liquibase.
the class ColumnSnapshotGenerator method setAutoIncrementDetails.
protected void setAutoIncrementDetails(Column column, Database database, DatabaseSnapshot snapshot) {
if (column.getAutoIncrementInformation() != null && database instanceof MSSQLDatabase && database.getConnection() != null && !(database.getConnection() instanceof OfflineConnection)) {
Map<String, Column.AutoIncrementInformation> autoIncrementColumns = (Map) snapshot.getScratchData("autoIncrementColumns");
if (autoIncrementColumns == null) {
autoIncrementColumns = new HashMap<String, Column.AutoIncrementInformation>();
Executor executor = ExecutorService.getInstance().getExecutor(database);
try {
List<Map<String, ?>> rows = executor.queryForList(new RawSqlStatement("select object_schema_name(object_id) as schema_name, object_name(object_id) as table_name, name as column_name, cast(seed_value as bigint) as start_value, cast(increment_value as bigint) as increment_by from sys.identity_columns"));
for (Map row : rows) {
String schemaName = (String) row.get("SCHEMA_NAME");
String tableName = (String) row.get("TABLE_NAME");
String columnName = (String) row.get("COLUMN_NAME");
Long startValue = (Long) row.get("START_VALUE");
Long incrementBy = (Long) row.get("INCREMENT_BY");
Column.AutoIncrementInformation info = new Column.AutoIncrementInformation(startValue, incrementBy);
autoIncrementColumns.put(schemaName + "." + tableName + "." + columnName, info);
}
snapshot.setScratchData("autoIncrementColumns", autoIncrementColumns);
} catch (DatabaseException e) {
LogFactory.getInstance().getLog().info("Could not read identity information", e);
}
}
if (column.getRelation() != null && column.getSchema() != null) {
Column.AutoIncrementInformation autoIncrementInformation = autoIncrementColumns.get(column.getSchema().getName() + "." + column.getRelation().getName() + "." + column.getName());
if (autoIncrementInformation != null) {
column.setAutoIncrementInformation(autoIncrementInformation);
}
}
}
}
use of liquibase.executor.Executor in project liquibase by liquibase.
the class PreconditionContainer method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
String ranOn = String.valueOf(changeLog);
if (changeSet != null) {
ranOn = String.valueOf(changeSet);
}
Executor executor = ExecutorService.getInstance().getExecutor(database);
try {
// Three cases for preConditions onUpdateSQL:
// 1. TEST: preConditions should be run, as in regular update mode
// 2. FAIL: the preConditions should fail if there are any
// 3. IGNORE: act as if preConditions don't exist
boolean testPrecondition = false;
if (executor.updatesDatabase()) {
testPrecondition = true;
} else {
if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.TEST)) {
testPrecondition = true;
} else if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.FAIL)) {
throw new PreconditionFailedException("Unexpected precondition in updateSQL mode with onUpdateSQL value: " + this.getOnSqlOutput(), changeLog, this);
} else if (this.getOnSqlOutput().equals(PreconditionContainer.OnSqlOutputOption.IGNORE)) {
testPrecondition = false;
}
}
if (testPrecondition) {
super.check(database, changeLog, changeSet);
}
} catch (PreconditionFailedException e) {
StringBuffer message = new StringBuffer();
message.append(" ").append(e.getFailedPreconditions().size()).append(" preconditions failed").append(StreamUtil.getLineSeparator());
for (FailedPrecondition invalid : e.getFailedPreconditions()) {
message.append(" ").append(invalid.toString());
message.append(StreamUtil.getLineSeparator());
}
if (getOnFailMessage() != null) {
message = new StringBuffer(getOnFailMessage());
}
if (this.getOnFail().equals(PreconditionContainer.FailOption.WARN)) {
LogFactory.getLogger().info("Executing: " + ranOn + " despite precondition failure due to onFail='WARN':\n " + message);
} else {
if (getOnFailMessage() == null) {
throw e;
} else {
throw new PreconditionFailedException(getOnFailMessage(), changeLog, this);
}
}
} catch (PreconditionErrorException e) {
StringBuffer message = new StringBuffer();
message.append(" ").append(e.getErrorPreconditions().size()).append(" preconditions failed").append(StreamUtil.getLineSeparator());
for (ErrorPrecondition invalid : e.getErrorPreconditions()) {
message.append(" ").append(invalid.toString());
message.append(StreamUtil.getLineSeparator());
}
if (this.getOnError().equals(PreconditionContainer.ErrorOption.CONTINUE)) {
LogFactory.getLogger().info("Continuing past: " + toString() + " despite precondition error:\n " + message);
throw e;
} else if (this.getOnError().equals(PreconditionContainer.ErrorOption.WARN)) {
LogFactory.getLogger().warning("Continuing past: " + toString() + " despite precondition error:\n " + message);
} else {
if (getOnErrorMessage() == null) {
throw e;
} else {
throw new PreconditionErrorException(getOnErrorMessage(), e.getErrorPreconditions());
}
}
}
}
Aggregations