Skip to content

Commit

Permalink
RAP-100 Update logic for transforming SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
QuyenLy87 committed Sep 18, 2024
1 parent 7b8fd23 commit 947fe13
Showing 1 changed file with 9 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import jakarta.annotation.Resource;
import javax.naming.ConfigurationException;
import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.Pattern;

Expand All @@ -26,6 +24,8 @@
public class AssertionExecutionService {

private static final String FAILED_TO_FIND_RVF_DB_SCHEMA = "Failed to find rvf db schema for ";
private static final String NOT_SUPPLIED = "NOT_SUPPLIED";

@Autowired
private AssertionService assertionService;
@Resource(name = "dataSource")
Expand Down Expand Up @@ -232,19 +232,14 @@ private List<String> transformSql(List<String> parts, Assertion assertion, Mysql
String prospectiveSchema = config.getProspectiveVersion();
final String[] nameParts = config.getProspectiveVersion().split("_");
String defaultModuleId = StringUtils.hasLength(config.getDefaultModuleId()) ? config.getDefaultModuleId() : getModuleId(nameParts);
String includedModules = String.join(",", config.getIncludedModules());
String version = (nameParts.length >= 3 ? nameParts[2] : "NOT_SUPPLIED");
String includedModules = CollectionUtils.isEmpty(config.getIncludedModules()) ? "NULL" : String.join(",", config.getIncludedModules());
String version = (nameParts.length >= 3 ? nameParts[2] : NOT_SUPPLIED);

String previousReleaseSchema = config.getPreviousVersion();
String dependencyReleaseSchema = config.getExtensionDependencyVersion();
validateSchemas(config, prospectiveSchema, previousReleaseSchema);

for ( String part : parts) {
if ((part.contains("<PREVIOUS>") && previousReleaseSchema == null)
|| (part.contains("<DEPENDENCY>") && dependencyReleaseSchema == null)) {
continue;
}

logger.debug("Original sql statement: {}", part);
final Pattern commentPattern = Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL);
part = commentPattern.matcher(part).replaceAll("");
Expand All @@ -259,12 +254,8 @@ private List<String> transformSql(List<String> parts, Assertion assertion, Mysql
part = part.replace("<PROSPECTIVE>", prospectiveSchema);
part = part.replace("<TEMP>", prospectiveSchema);
part = part.replace("<INTERNATIONAL_MODULES>", internationalModules);
if (previousReleaseSchema != null) {
part = part.replace("<PREVIOUS>", previousReleaseSchema);
}
if (dependencyReleaseSchema != null) {
part = part.replace("<DEPENDENCY>", dependencyReleaseSchema);
}
part = part.replace("<PREVIOUS>", previousReleaseSchema);
part = part.replace("<DEPENDENCY>", dependencyReleaseSchema);
part = part.replace("<DELTA>", DELTA_TABLE_SUFFIX);
part = part.replace("<SNAPSHOT>", SNAPSHOT_TABLE_SUFFIX);
part = part.replace("<FULL>", FULL_TABLE_SUFFIX);
Expand All @@ -276,7 +267,7 @@ private List<String> transformSql(List<String> parts, Assertion assertion, Mysql
}

private static String getModuleId(String[] nameParts) {
return nameParts.length >= 2 ? ProductName.toModuleId(nameParts[1]) : "NOT_SUPPLIED";
return nameParts.length >= 2 ? ProductName.toModuleId(nameParts[1]) : NOT_SUPPLIED;
}

private static void validateSchemas(MysqlExecutionConfig config, String prospectiveSchema, String previousReleaseSchema) throws ConfigurationException {
Expand Down

0 comments on commit 947fe13

Please sign in to comment.