Skip to content

Commit

Permalink
Merge branch 'master' into DHIS2-18417
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge committed Dec 16, 2024
2 parents d376053 + aaf0205 commit 2c64306
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,17 +231,19 @@ public void swapTable(AnalyticsTableUpdateParams params, AnalyticsTable table) {
params.isPartialUpdate() && tableExists && table.getTableType().isLatestPartition();

log.info("Swapping table: '{}'", table.getMainName());
log.info("Master table exists: {}, skip master table: {}", tableExists, skipMasterTable);
log.info("Supports multi-statements: {}", sqlBuilder.supportsMultiStatements());
log.info("Master table exists: '{}', skip master table: '{}'", tableExists, skipMasterTable);

List<Table> swapPartitions = new UniqueArrayList<>();
table.getTablePartitions().stream()
.forEach(partition -> swapPartitions.add(swapTable(partition, partition.getMainName())));
List<Table> swappedPartitions = new UniqueArrayList<>();

table.getTablePartitions().forEach(part -> swapTable(part, part.getMainName()));
table.getTablePartitions().forEach(part -> swappedPartitions.add(part.fromStaging()));

if (!skipMasterTable) {
// Full replace update and main table exist, swap main table
swapTable(table, table.getMainName());
} else {
swapPartitions.forEach(
// Incremental append update, update parent of partitions to existing main table
swappedPartitions.forEach(
partition -> swapParentTable(partition, table.getName(), table.getMainName()));
dropTable(table);
}
Expand Down Expand Up @@ -289,14 +291,8 @@ public int invokeAnalyticsTableSqlHooks() {
* @param stagingTable the staging table.
* @param mainTableName the main table name.
*/
private Table swapTable(Table stagingTable, String mainTableName) {
if (sqlBuilder.supportsMultiStatements()) {
executeSilently(sqlBuilder.swapTable(stagingTable, mainTableName));
} else {
executeSilently(sqlBuilder.dropTableIfExistsCascade(mainTableName));
executeSilently(sqlBuilder.renameTable(stagingTable, mainTableName));
}
return stagingTable.fromStaging();
private void swapTable(Table stagingTable, String mainTableName) {
executeSilently(sqlBuilder.swapTable(stagingTable, mainTableName));
}

/**
Expand All @@ -307,12 +303,7 @@ private Table swapTable(Table stagingTable, String mainTableName) {
* @param mainMasterName the main master table name.
*/
private void swapParentTable(Table partition, String stagingMasterName, String mainMasterName) {
if (sqlBuilder.supportsMultiStatements()) {
executeSilently(sqlBuilder.swapParentTable(partition, stagingMasterName, mainMasterName));
} else {
sqlBuilder.removeParentTable(partition, stagingMasterName);
sqlBuilder.setParentTable(partition, mainMasterName);
}
executeSilently(sqlBuilder.swapParentTable(partition, stagingMasterName, mainMasterName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ public boolean hasPartitions() {
return isNotEmpty(partitions);
}

/**
* Returns this {@link Table} with the staging table name converted to the main table name.
*
* @return a {@link Table}.
*/
public Table fromStaging() {
return new Table(
fromStaging(this.getName()),
Expand Down

0 comments on commit 2c64306

Please sign in to comment.