Skip to content

Commit

Permalink
Allow snapshot control creation to be overridden for snapshot filters…
Browse files Browse the repository at this point in the history
… DAT-16054 (#5406)

* Added snapshot types argument

DAT-15874

* Show snapshotTypes in help

DAT-15784

* DAT-15784

WIP

* DAT-16054

Made changes to index retrieval query

* DAT-16054

Work on calling types constructor

* Refactor to make snapshot command extensible

DAT-16054

* Added a file check to CommandTests

DAT-16054

* Remove unused argument

DAT-16054
  • Loading branch information
wwillard7800 committed Jan 29, 2024
1 parent 77cb72c commit 7099b33
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ Long Description: ${commandDefinition.getLongDescription() ?: "NOT SET"}
contents = StreamUtil.readStreamAsString(resource.openInputStream())
} else {
contents = null
throw new FileNotFoundException("File ${path} not found")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.SnapshotControl;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtil;
import lombok.Getter;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.*;

@Getter
public class SnapshotCommandStep extends AbstractCommandStep {

public static final String[] COMMAND_NAME = {"snapshot"};
Expand Down Expand Up @@ -64,14 +67,6 @@ private CatalogAndSchema[] parseSchemas(Database database, String... schemas) {
return finalList.toArray(new CatalogAndSchema[0]);
}

public Map<String, Object> getSnapshotMetadata() {
return snapshotMetadata;
}

public void setSnapshotMetadata(Map<String, Object> snapshotMetadata) {
this.snapshotMetadata = snapshotMetadata;
}

@Override
public void run(CommandResultsBuilder resultsBuilder) throws Exception {
CommandScope commandScope = resultsBuilder.getCommandScope();
Expand All @@ -82,11 +77,7 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {

InternalSnapshotCommandStep.logUnsupportedDatabase(database, this.getClass());
SnapshotControl snapshotControl;
if (commandScope.getArgumentValue(SNAPSHOT_CONTROL_ARG) == null) {
snapshotControl = new SnapshotControl(database);
} else {
snapshotControl = commandScope.getArgumentValue(SnapshotCommandStep.SNAPSHOT_CONTROL_ARG);
}
snapshotControl = createSnapshotControl(commandScope, database);

if (schemas == null) {
schemas = new CatalogAndSchema[]{database.getDefaultSchema()};
Expand Down Expand Up @@ -119,6 +110,16 @@ public void run(CommandResultsBuilder resultsBuilder) throws Exception {
}
}

protected SnapshotControl createSnapshotControl(CommandScope commandScope, Database database) {
SnapshotControl snapshotControl;
if (commandScope.getArgumentValue(SNAPSHOT_CONTROL_ARG) == null) {
snapshotControl = new SnapshotControl(database);
} else {
snapshotControl = commandScope.getArgumentValue(SnapshotCommandStep.SNAPSHOT_CONTROL_ARG);
}
return snapshotControl;
}

private Writer getOutputWriter(final OutputStream outputStream) throws IOException {
String charsetName = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.sql.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

public class JdbcDatabaseSnapshot extends DatabaseSnapshot {

Expand All @@ -28,6 +29,8 @@ public class JdbcDatabaseSnapshot extends DatabaseSnapshot {

private CachingDatabaseMetaData cachingDatabaseMetaData;

private Map<String, CachedRow> cachedExpressionMap = null;

private Set<String> userDefinedTypes;

public JdbcDatabaseSnapshot(DatabaseObject[] examples, Database database, SnapshotControl snapshotControl) throws DatabaseException, InvalidExampleException {
Expand Down Expand Up @@ -116,14 +119,14 @@ public List<CachedRow> fastFetch() throws SQLException, DatabaseException {
"c.TABLE_NAME, " +
"c.COLUMN_NAME, " +
"c.COLUMN_POSITION AS ORDINAL_POSITION, " +
"e.COLUMN_EXPRESSION AS FILTER_CONDITION, " +
"NULL AS FILTER_CONDITION, " +
"c.INDEX_OWNER, " +
"CASE I.UNIQUENESS WHEN 'UNIQUE' THEN 0 ELSE 1 END AS NON_UNIQUE, " +
"CASE c.DESCEND WHEN 'Y' THEN 'D' WHEN 'DESC' THEN 'D' WHEN 'N' THEN 'A' WHEN 'ASC' THEN 'A' END AS ASC_OR_DESC, " +
"CASE WHEN tablespace_name = (SELECT default_tablespace FROM user_users) " +
"THEN NULL ELSE tablespace_name END AS tablespace_name " +
"FROM ALL_IND_COLUMNS c " +
"JOIN ALL_INDEXES i ON i.owner=c.index_owner AND i.index_name = c.index_name and i.table_owner = c.table_owner " +
"LEFT OUTER JOIN all_ind_expressions e ON e.index_owner=c.index_owner AND e.index_name = c.index_name AND e.column_position = c.column_position " +
"LEFT OUTER JOIN " + (((OracleDatabase) database).canAccessDbaRecycleBin() ? "dba_recyclebin" : "user_recyclebin") + " d ON d.object_name=c.table_name ";
if (!isBulkFetchMode || getAllCatalogsStringScratchData() == null) {
sql += "WHERE c.TABLE_OWNER = '" + database.correctObjectName(catalogAndSchema.getCatalogName(), Schema.class) + "' ";
Expand All @@ -144,7 +147,7 @@ public List<CachedRow> fastFetch() throws SQLException, DatabaseException {

sql += " ORDER BY c.INDEX_NAME, ORDINAL_POSITION";

returnList.addAll(executeAndExtract(sql, database));
returnList.addAll(setIndexExpressions(executeAndExtract(sql, database)));
} else if (database instanceof MSSQLDatabase) {
String tableCat = "original_db_name()";

Expand Down Expand Up @@ -276,6 +279,35 @@ public List<CachedRow> fastFetch() throws SQLException, DatabaseException {
return returnList;
}

private List<CachedRow> setIndexExpressions(List<CachedRow> c) throws DatabaseException, SQLException {
Map<String, CachedRow> expressionMap = getCachedExpressionMap();
c.forEach(row -> {
row.set("FILTER_CONDITION", null);
String key = row.getString("INDEX_OWNER") + "::" + row.getString("INDEX_NAME") + "::" +
row.getInt("ORDINAL_POSITION");
CachedRow fromMap = expressionMap.get(key);
if (fromMap != null) {
row.set("FILTER_CONDITION", fromMap.get("COLUMN_EXPRESSION"));
}
});
return c;
}

private Map<String, CachedRow> getCachedExpressionMap() throws DatabaseException, SQLException {
if (cachedExpressionMap != null) {
return cachedExpressionMap;
}
String expSql = "SELECT e.column_expression, e.index_owner, e.index_name, e.column_position FROM all_ind_expressions e";
List<CachedRow> ec = executeAndExtract(expSql, database);
cachedExpressionMap = new HashMap<>();
ec.forEach(row -> {
String key = row.getString("INDEX_OWNER") + "::" + row.getString("INDEX_NAME") + "::" +
row.getInt("COLUMN_POSITION");
cachedExpressionMap.put(key, row);
});
return cachedExpressionMap;
}

@Override
public List<CachedRow> bulkFetch() throws SQLException, DatabaseException {
this.isBulkFetchMode = true;
Expand All @@ -292,6 +324,7 @@ protected boolean shouldBulkSelect(String schemaKey, ResultSetCache resultSetCac
});
}


protected void warnAboutDbaRecycleBin() {
if (!ignoreWarnAboutDbaRecycleBin && !warnedAboutDbaRecycleBin && !(((OracleDatabase) database).canAccessDbaRecycleBin())) {
Scope.getCurrentScope().getLog(getClass()).warning(((OracleDatabase) database).getDbaRecycleBinWarning());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ public SnapshotControl(Database database) {
* @param database the DBMS for which snapshots should be generated
* @param types the list of object types to be included in the snapshot
*/
@SafeVarargs
public SnapshotControl(Database database, Class<? extends DatabaseObject>... types) {
this(database, true, types);
}

@SafeVarargs
public SnapshotControl(Database database, boolean expandTypesIfNeeded, Class<? extends DatabaseObject>... types) {
if ((types == null) || (types.length == 0)) {
setTypes(DatabaseObjectFactory.getInstance().getStandardTypes(), database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public DatabaseSnapshot createSnapshot(CatalogAndSchema[] examples, Database dat

}

Scope.getCurrentScope().getLog(SnapshotGeneratorFactory.class).info("Creating snapshot");
return createSnapshot(schemas, database, snapshotControl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,12 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
// Is this column a simple column (definition == null)
// or is it a computed expression (definition != null)
if (definition == null || database instanceof PostgresDatabase || database instanceof MSSQLDatabase) {
String ascOrDesc;
if (database instanceof Db2zDatabase) {
ascOrDesc = row.getString("ORDER");
} else {
ascOrDesc = row.getString("ASC_OR_DESC");
}
String ascOrDesc;
if (database instanceof Db2zDatabase) {
ascOrDesc = row.getString("ORDER");
} else {
ascOrDesc = row.getString("ASC_OR_DESC");
}
Boolean descending = "D".equals(ascOrDesc) ? Boolean.TRUE : ("A".equals(ascOrDesc) ?
Boolean.FALSE : null);
returnIndex.getColumns().set(position - 1, new Column(columnName)
Expand Down

0 comments on commit 7099b33

Please sign in to comment.