Skip to content

Commit

Permalink
Merge pull request #583 from liquibase/DAT-14657
Browse files Browse the repository at this point in the history
DAT-14657. Fixed advanced test failures.
  • Loading branch information
PavloTytarchuk authored Nov 3, 2023
2 parents 4cc5b36 + 548f4bf commit b342b3e
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import liquibase.database.jvm.JdbcConnection
import liquibase.harness.config.DatabaseUnderTest
import liquibase.harness.config.TestConfig
import liquibase.harness.util.rollback.RollbackStrategy
import liquibase.resource.SearchPathResourceAccessor
import org.apache.commons.io.FileUtils
import spock.lang.Shared
import spock.lang.Specification
Expand All @@ -24,8 +25,8 @@ class AdvancedTest extends Specification {
@Shared
List<DatabaseUnderTest> databases;
String inputResourcesDirFullPath = System.getProperty("user.dir") + "/src/main/resources/"
String outputResourcesDirFullPath = System.getProperty("user.dir") + "/src/test/resources/"
String outputResourcesDirPath = "/src/test/resources/"
String outputResourcesDirFullPath = System.getProperty("user.dir") + "/target/test-classes/"
String outputResourcesDirPath = "/target/test-classes/"

def setupSpec() {
databases = TestConfig.instance.getFilteredDatabasesUnderTest()
Expand Down Expand Up @@ -91,7 +92,7 @@ class AdvancedTest extends Specification {

when: "execute updateSql command on generated changelogs"
argsMapPrimary.put("changelogFile", outputResourcesDirPath + entry.value)
def generatedSql = parseQuery(executeCommandScope("updateSql", argsMapPrimary).toString())
def generatedSql = parseQuery(executeCommandScopeWithSearchPathResourceAccessor("updateSql", argsMapPrimary).toString())
generatedSql = removeSchemaNames(generatedSql, testInput.database, testInput.primaryDbSchemaName)
def expectedSql = testInput.expectedGenerateChangelogSql

Expand All @@ -113,7 +114,7 @@ class AdvancedTest extends Specification {
when: "apply generated changelog to secondary database instance and execute diff command"
argsMapSecondary.put("changelogFile", outputResourcesDirPath + generateChangelogMap.get("jsonChangelog"))
Scope.getCurrentScope().getUI().sendMessage("APPLY GENERATED CHANGELOG TO SECONDARY DATABASE")
executeCommandScope("update", argsMapSecondary)
executeCommandScopeWithSearchPathResourceAccessor("update", argsMapSecondary)
generatedDiff = removeDatabaseInfoFromDiff(executeCommandScope("diff", argsMapSecondary).toString())
expectedDiff = removeDatabaseInfoFromDiff(getResourceContent("/$testInput.pathToEmptyDiffFile"))

Expand Down Expand Up @@ -142,7 +143,7 @@ class AdvancedTest extends Specification {

when: "execute updateSql command on generated changelogs"
argsMapSecondary.put("changelogFile", outputResourcesDirPath + entry.value)
def generatedSql = parseQuery(executeCommandScope("updateSql", argsMapSecondary).toString())
def generatedSql = parseQuery(executeCommandScopeWithSearchPathResourceAccessor("updateSql", argsMapSecondary).toString())
generatedSql = removeSchemaNames(generatedSql, testInput.database, testInput.secondaryDbSchemaName)
def expectedSql = testInput.expectedDiffChangelogSql

Expand All @@ -157,7 +158,7 @@ class AdvancedTest extends Specification {
when: "apply generated diffChangelog to secondary database instance and execute diff command"
argsMapSecondary.put("changelogFile", outputResourcesDirPath + diffChangelogMap.get("xmlChangelog"))
Scope.getCurrentScope().getUI().sendMessage("APPLY GENERATED DIFFCHANGELOG TO SECONDARY DATABASE")
executeCommandScope("update", argsMapSecondary)
executeCommandScopeWithSearchPathResourceAccessor("update", argsMapSecondary)
generatedDiff = removeDatabaseInfoFromDiff(executeCommandScope("diff", argsMapSecondary).toString())
expectedDiff = removeDatabaseInfoFromDiff(getResourceContent("/$testInput.pathToEmptyDiffFile"))

Expand Down
36 changes: 36 additions & 0 deletions src/main/groovy/liquibase/harness/util/TestUtils.groovy
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package liquibase.harness.util

import liquibase.Scope
import liquibase.command.CommandScope
import liquibase.exception.CommandExecutionException
import liquibase.harness.util.rollback.RollbackByTag
import liquibase.harness.util.rollback.RollbackStrategy
import liquibase.harness.util.rollback.RollbackToDate
import liquibase.resource.SearchPathResourceAccessor
import org.junit.Assert

import java.nio.file.Path
Expand Down Expand Up @@ -100,6 +102,40 @@ class TestUtils {
return outputStream
}


static OutputStream executeCommandScopeWithSearchPathResourceAccessor(String commandName, Map<String, Object> arguments) {
def commandScope = new CommandScope(commandName)
def outputStream = new ByteArrayOutputStream()
def resourceAccessor = new SearchPathResourceAccessor(".", Scope.getCurrentScope().getResourceAccessor())
Map<String, Object> map = new HashMap<>();
map.put(Scope.Attr.resourceAccessor.name(),resourceAccessor)

for (Map.Entry<String, Object> entry : arguments) {
commandScope.addArgumentValue(entry.getKey(), entry.getValue())
}
commandScope.setOutput(outputStream)
try {
Logger.getLogger(this.class.name).info(String.format("Executing liquibase command: %s ", commandName))
Scope.child(map, new Scope.ScopedRunner() {
@Override
void run() throws Exception {
commandScope.execute()
}
})
} catch (Exception exception) {
if (exception instanceof CommandExecutionException && exception.toString().contains("is not available in SQL output mode")) {
//Here we check whether updateSql command throws specific exception and skip it (updateSql doesn't work for SQLite for some change types)
return outputStream
}
Logger.getLogger(this.class.name).severe("Failed to execute command scope for command " +
commandScope.getCommand().toString() + ". " + exception.printStackTrace())
Logger.getLogger(this.class.name).info("If this is expected to be invalid query for this database/version, " +
"create an 'expectedSql.sql' file that starts with 'INVALID TEST' and an explanation of why.")
Assert.fail exception.message
}
return outputStream
}

static RollbackStrategy chooseRollbackStrategy() {
return "rollbackByTag".equalsIgnoreCase(System.getProperty("rollbackStrategy")) ? new RollbackByTag() : new RollbackToDate()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CREATE TABLE test_table_xml (test_column INT NULL)
ALTER TABLE test_table_xml ADD CONSTRAINT test_check_constraint CHECK (test_column > 0)
CREATE TABLE test_table_xml (test_column INT NULL);
ALTER TABLE test_table_xml ADD CONSTRAINT test_check_constraint CHECK (test_column > 0);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE test_table_base (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_BASE PRIMARY KEY (id))
CREATE TABLE test_table_reference (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_REFERENCE PRIMARY KEY (id))
CREATE INDEX test_table_reference_index ON test_table_reference(test_column)
ALTER TABLE test_table_base ADD CONSTRAINT test_fk FOREIGN KEY (id) REFERENCES test_table_reference (test_column) ON UPDATE RESTRICT ON DELETE CASCADE
CREATE TABLE test_table_base (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_BASE PRIMARY KEY (id));
CREATE TABLE test_table_reference (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_REFERENCE PRIMARY KEY (id));
CREATE INDEX test_table_reference_index ON test_table_reference(test_column);
ALTER TABLE test_table_base ADD CONSTRAINT test_fk FOREIGN KEY (id) REFERENCES test_table_reference (test_column) ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE test_table (test_column INT NULL)
ALTER TABLE test_table ADD varcharColumn VARCHAR(25) NULL, ADD intColumn INT NULL, ADD dateColumn date NULL
UPDATE test_table SET varcharColumn = 'INITIAL_VALUE'
UPDATE test_table SET intColumn = 5
UPDATE test_table SET dateColumn = '2020-09-21'
CREATE TABLE test_table (test_column INT NULL);
ALTER TABLE test_table ADD varcharColumn VARCHAR(25) NULL, ADD intColumn INT NULL, ADD dateColumn date NULL;
UPDATE test_table SET varcharColumn = 'INITIAL_VALUE';
UPDATE test_table SET intColumn = 5;
UPDATE test_table SET dateColumn = '2020-09-21';
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP FUNCTION IF EXISTS test_function
DROP FUNCTION IF EXISTS test_function;
CREATE FUNCTION test_function()
RETURNS VARCHAR(20)
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CREATE TABLE lbcat.test_table (id INT NULL, test_column INT NULL)
CREATE INDEX idx_first_name ON lbcat.test_table(id)
CREATE TABLE lbcat.test_table (id INT NULL, test_column INT NULL);
CREATE INDEX idx_first_name ON lbcat.test_table(id);
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
CREATE TABLE test_table_xml (test_column INT NULL)
ALTER TABLE test_table_xml ADD CONSTRAINT secondary_check_constraint CHECK (test_column > 0)
CREATE TABLE test_table_xml (test_column INT NULL);
ALTER TABLE test_table_xml ADD CONSTRAINT secondary_check_constraint CHECK (test_column > 0);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE test_table_base (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_BASE PRIMARY KEY (id))
CREATE TABLE test_table_reference (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_REFERENCE PRIMARY KEY (id))
CREATE INDEX test_table_base_index ON test_table_base(test_column)
ALTER TABLE test_table_reference ADD CONSTRAINT secondary_test_fk FOREIGN KEY (id) REFERENCES test_table_base (test_column) ON UPDATE RESTRICT ON DELETE CASCADE
CREATE TABLE test_table_base (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_BASE PRIMARY KEY (id));
CREATE TABLE test_table_reference (id INT NOT NULL, test_column INT NULL, CONSTRAINT PK_TEST_TABLE_REFERENCE PRIMARY KEY (id));
CREATE INDEX test_table_base_index ON test_table_base(test_column);
ALTER TABLE test_table_reference ADD CONSTRAINT secondary_test_fk FOREIGN KEY (id) REFERENCES test_table_base (test_column) ON UPDATE RESTRICT ON DELETE CASCADE;
Original file line number Diff line number Diff line change
@@ -1 +1 @@
CREATE TABLE test_table (test_column INT NULL, secondary_column INT NULL)
CREATE TABLE test_table (test_column INT NULL, secondary_column INT NULL);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CREATE TABLE secondarydb.test_table (id INT NULL, test_column INT NULL);
CREATE INDEX idx_secondary ON secondarydb.test_table(test_column);

0 comments on commit b342b3e

Please sign in to comment.