use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshot method compare.
/**
* Compare both snapshots and throw error if unexpected differences are found.
* Snapshots are compared table by table.
* In the usual case the tables are loaded from database prior to comparing.
* But if a snapshot was saved into a file, then its tables are loaded from the file, not from the database.
*
* @param that the snapshot to compare to
* @param compareOptions - (optional) additional options that change the comparison. by default is null, so the compare is as-is (e.g. if there is an error, the comparison fails)
* @throws DatabaseSnapshotException
*/
@PublicAtsApi
public void compare(DatabaseSnapshot that, CompareOptions compareOptions) throws DatabaseSnapshotException {
try {
if (that == null) {
throw new DatabaseSnapshotException("Snapshot to compare is null");
}
if (this.name.equals(that.name)) {
throw new DatabaseSnapshotException("You are trying to compare snapshots with same name: " + this.name);
}
if (this.metadataTimestamp == -1) {
throw new DatabaseSnapshotException("You are trying to compare snapshots but [" + this.name + "] snapshot is still not created");
}
if (that.metadataTimestamp == -1) {
throw new DatabaseSnapshotException("You are trying to compare snapshots but [" + that.name + "] snapshot is still not created");
}
if (log.isDebugEnabled()) {
log.debug("Comparing snapshots [" + this.name + "] taken on " + DatabaseSnapshotUtils.dateToString(this.metadataTimestamp) + " and [" + that.name + "] taken on " + DatabaseSnapshotUtils.dateToString(that.metadataTimestamp));
}
this.equality = new DatabaseEqualityState(this.name, that.name);
// make copies of the table info, as we will remove from these lists,
// but we do not want to remove from the original table info lists
List<TableDescription> thisTables = new ArrayList<TableDescription>(this.tables);
List<TableDescription> thatTables = new ArrayList<TableDescription>(that.tables);
// Merge all skip rules from both snapshot instances,
// so it is not needed to add same skip rules in both snapshot instances.
// merge all columns to be skipped(per table)
Map<String, SkipColumns> skipColumns = mergeSkipColumns(that.skipColumnsPerTable);
// merge all content to be skipper(per table)
Map<String, SkipContent> skipContent = mergeSkipContent(that.skipContentPerTable);
// merge all rows to be skipped(per table)
Map<String, SkipRows> skipRows = mergeSkipRows(that.skipRowsPerTable);
Set<String> tablesToSkip = getAllTablesToSkip(skipColumns);
// We can use just one index name matcher
IndexMatcher actualIndexNameMatcher = mergeIndexMatchers(that.indexMatcher);
compareTables(this.name, thisTables, that.name, thatTables, that.dbProvider, tablesToSkip, skipColumns, skipContent, skipRows, actualIndexNameMatcher, that.backupXmlFile, equality);
if (compareOptions != null) {
try {
// handle expected differences
handleExpectedMissingRows(compareOptions, equality);
} catch (Exception e) {
log.error("Error occured while handling missing rows", e);
}
}
if (equality.hasDifferences()) {
// there are some unexpected differences
throw new DatabaseSnapshotException(equality);
} else {
log.info("Successful verification");
}
} finally {
// close the database connections
disconnect(this.dbProvider, "after comparing database snapshots");
disconnect(that.dbProvider, "after comparing database snapshots");
}
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshot method mergeSkipColumns.
/**
* Here we merge skip table columns from both snapshots into a single list.
* It doesn't make sense to ask people to add same skip rules for same tables in both snapshot instances.
*
* @param thatSkipColumnsPerTable
* @return
*/
private Map<String, SkipColumns> mergeSkipColumns(Map<String, SkipColumns> thatSkipColumnsPerTable) {
// we will return a new instance containing all rules
Map<String, SkipColumns> allSkipColumnsPerTable = new HashMap<>();
// first add all rules for this instance
for (String table : this.skipColumnsPerTable.keySet()) {
allSkipColumnsPerTable.put(table, this.skipColumnsPerTable.get(table));
}
// then add all rules for the other instance
for (String table : thatSkipColumnsPerTable.keySet()) {
SkipColumns allSkipColumns = allSkipColumnsPerTable.get(table);
SkipColumns thatSkipColumns = thatSkipColumnsPerTable.get(table);
if (allSkipColumns != null) {
// there is already a rule for this table
SkipColumns thisSkipColumns = this.skipColumnsPerTable.get(table);
if (thisSkipColumns.isSkipWholeTable()) {
// nothing to do, the table will be skipped anyway
} else if (thatSkipColumns.isSkipWholeTable()) {
// replace the current rule, so skip the table
allSkipColumnsPerTable.put(table, thatSkipColumns);
} else {
// just some columns will be skipped, merge the list of columns
for (String column : thatSkipColumns.getColumnsToSkip()) {
allSkipColumns.addColumnToSkip(column);
}
}
} else {
// no current rule for this table, now we add one
allSkipColumnsPerTable.put(table, thatSkipColumns);
}
}
return allSkipColumnsPerTable;
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshot method compareTables.
/**
* Compares all tables between two snapshots
*
* @param thisSnapshotName
* @param thisTables
* @param thatSnapshotName
* @param thatTables
* @param thatDbProvider
* @param tablesToSkip
* @param skipColumns
* @param skipContent
* @param skipRows
* @param indexNameMatcher
* @param thatBackupXmlFile
* @param equality
*/
private void compareTables(String thisSnapshotName, List<TableDescription> thisTables, String thatSnapshotName, List<TableDescription> thatTables, DbProvider thatDbProvider, Set<String> tablesToSkip, Map<String, SkipColumns> skipColumns, Map<String, SkipContent> skipContent, Map<String, SkipRows> skipRows, IndexMatcher indexNameMatcher, Document thatBackupXmlFile, DatabaseEqualityState equality) {
// make a list of tables present in both snapshots
List<String> commonTables = getCommonTables(thisSnapshotName, thisTables, thatSnapshotName, thatTables, tablesToSkip);
for (String tableName : commonTables) {
// get tables to compare
TableDescription thisTable = null;
TableDescription thatTable = null;
for (TableDescription table : thisTables) {
if (table.getName().equalsIgnoreCase(tableName)) {
thisTable = table;
break;
}
}
for (TableDescription table : thatTables) {
if (table.getName().equalsIgnoreCase(tableName)) {
thatTable = table;
break;
}
}
SkipColumns skipColumnsPerTable = skipColumns.get(thisTable.getName());
if (skipColumnsPerTable != null && skipColumnsPerTable.isSkipWholeTable()) {
// if table is not of interest - skip it
continue;
}
List<String> thisValuesList = null;
List<String> thatValuesList = null;
int thisNumberOfRows = -1;
int thatNumberOfRows = -1;
SkipContent skipContentForThisTable = skipContent.get(tableName.toLowerCase());
if (skipContentForThisTable != null) {
if (skipContentForThisTable.isRememberNumberOfRows()) {
// we do not compare the content of the tables,
// but we still compare the number of rows
thisNumberOfRows = loadTableLength(thisSnapshotName, thisTable, this.dbProvider, this.backupXmlFile);
thatNumberOfRows = loadTableLength(thatSnapshotName, thatTable, thatDbProvider, thatBackupXmlFile);
}
// else -> we completely do not compare the content of the tables
} else {
// we want to compare the content of the tables,
// so load the table content
thisValuesList = loadTableData(thisSnapshotName, thisTable, skipColumns, skipRows, this.dbProvider, this.backupXmlFile);
thatValuesList = loadTableData(thatSnapshotName, thatTable, skipColumns, skipRows, thatDbProvider, thatBackupXmlFile);
}
// do the actual comparison
thisTable.compare(thatTable, thisValuesList, thatValuesList, thisNumberOfRows, thatNumberOfRows, indexNameMatcher, equality);
}
thisTables.clear();
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshot method constructSelectStatement.
private String constructSelectStatement(TableDescription table, Map<String, SkipColumns> skipColumns) {
Set<String> columns = table.getColumnNames();
// Search for skip columns for this table. The search must ignore the letters case
SkipColumns skipColumnsForThisTable = null;
for (String tableName : skipColumns.keySet()) {
if (tableName.equalsIgnoreCase(table.getName())) {
skipColumnsForThisTable = skipColumns.get(tableName);
break;
}
}
if (skipColumnsForThisTable == null) {
String query = "SELECT * FROM ";
if (this.dbProvider instanceof PostgreSqlDbProvider) {
List<String> sortedColumns = new ArrayList<>(columns);
Collections.sort(sortedColumns);
query = "SELECT " + String.join(", ", sortedColumns) + " FROM ";
}
// all columns are important
if (table.getSchema() != null) {
query += table.getSchema() + ".";
}
return query + table.getName();
} else {
// some columns must be skipped
StringBuilder sql = new StringBuilder("SELECT");
// add all important columns
for (String column : columns) {
if (!skipColumnsForThisTable.isSkipColumn(column)) {
sql.append(" " + column + ",");
}
}
if ("SELECT".equals(sql.toString())) {
// so no column are left for selection, so we won't select anything
return null;
} else {
// remove last comma
sql.setLength(sql.length() - 1);
sql.append(" FROM ");
if (table.getSchema() != null) {
sql.append(table.getSchema()).append(".");
}
sql.append(table.getName());
return sql.toString();
}
}
}
use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshotBackupUtils method loadFromFile.
/**
* Load a snapshot from a file
*
* @param newSnapshotName the name of the new snapshot
* @param snapshot the snapshot instance to fill with new data
* @param sourceFile the backup file name
* @return the XML document
*/
public Document loadFromFile(String newSnapshotName, DatabaseSnapshot snapshot, String sourceFile) {
log.info("Load database snapshot from file " + sourceFile + " - START");
// first clean up the current instance, in case some snapshot was taken before
snapshot.tables.clear();
Document doc;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(sourceFile));
doc.getDocumentElement().normalize();
} catch (Exception e) {
throw new DatabaseSnapshotException("Error reading database snapshot backup file " + sourceFile, e);
}
Element databaseNode = doc.getDocumentElement();
if (!DatabaseSnapshotUtils.NODE_DB_SNAPSHOT.equals(databaseNode.getNodeName())) {
throw new DatabaseSnapshotException("Bad backup file. Root node name is expeced to be '" + DatabaseSnapshotUtils.NODE_DB_SNAPSHOT + "', but it is '" + databaseNode.getNodeName() + "'");
}
if (StringUtils.isNullOrEmpty(newSnapshotName)) {
snapshot.name = databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME);
} else {
// user wants to change the snapshot name
snapshot.name = newSnapshotName;
}
// the timestamps
snapshot.metadataTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME));
snapshot.contentTimestamp = DatabaseSnapshotUtils.stringToDate(databaseNode.getAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME));
// the tables
List<Element> tableNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_TABLE);
for (Element tableNode : tableNodes) {
snapshot.tables.add(TableDescription.fromXmlNode(snapshot.name, tableNode));
}
// any skip table content rules
snapshot.skipContentPerTable.clear();
List<Element> skipContentNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_CONTENT);
for (Element skipContentNode : skipContentNodes) {
SkipContent skipContent = SkipContent.fromXmlNode(skipContentNode);
snapshot.skipContentPerTable.put(skipContent.getTable().toLowerCase(), skipContent);
}
// any skip table column rules
snapshot.skipColumnsPerTable.clear();
List<Element> skipColumnNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_COLUMNS);
for (Element skipColumnNode : skipColumnNodes) {
SkipColumns skipColumns = SkipColumns.fromXmlNode(skipColumnNode);
snapshot.skipColumnsPerTable.put(skipColumns.getTable().toLowerCase(), skipColumns);
}
// any skip index attribute rules
snapshot.skipIndexAttributesPerTable.clear();
List<Element> skipIndexAttributesNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_INDEX_ATTRIBUTES);
for (Element skipIndexAttributesNode : skipIndexAttributesNodes) {
SkipIndexAttributes skipIndexAttributes = SkipIndexAttributes.fromXmlNode(skipIndexAttributesNode);
snapshot.skipIndexAttributesPerTable.put(skipIndexAttributes.getTable().toLowerCase(), skipIndexAttributes);
}
// any skip table row rules
snapshot.skipRowsPerTable.clear();
List<Element> skipRowNodes = DatabaseSnapshotUtils.getChildrenByTagName(databaseNode, DatabaseSnapshotUtils.NODE_SKIP_ROWS);
for (Element skipRowNode : skipRowNodes) {
SkipRows skipRows = SkipRows.fromXmlNode(skipRowNode);
snapshot.skipRowsPerTable.put(skipRows.getTable().toLowerCase(), skipRows);
}
log.info("Load database snapshot from file " + sourceFile + " - END");
return doc;
}
Aggregations