Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional verification for GenerateChangelogTest #486

Merged
merged 15 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class GenerateChangelogTest extends Specification {
List<DatabaseUnderTest> databases
@Shared
UIService uiService = Scope.getCurrentScope().getUI()
String testResourcesPath = System.getProperty("user.dir") + "/src/test/resources/"
String resourcesDirFullPath = System.getProperty("user.dir") + "/src/test/resources/"
String resourcesDirPath = "/src/test/resources/"
long timeMillisBeforeTest
long timeMillisAfterTest

Expand Down Expand Up @@ -65,11 +66,12 @@ class GenerateChangelogTest extends Specification {
when: "execute generateChangelog command using different changelog formats"
argsMap.put("changeLogFile", testInput.xmlChangelogPath)
executeCommandScope("update", argsMap)
argsMap.put("changeLogFile", testResourcesPath + entry.value)
argsMap.put("changeLogFile", resourcesDirFullPath + entry.value)
argsMap.put("excludeObjects", "(?i)posts, (?i)authors")//excluding static test-harness objects from generated changelog
if (entry.key.equalsIgnoreCase("expectedSqlChangelog")) {
def shortDbName = getShortDatabaseName(testInput.databaseName)
sqlSpecificChangelogFile = entry.value.replace(".sql", ".$shortDbName" + ".sql")
argsMap.put("changeLogFile", testResourcesPath + sqlSpecificChangelogFile)
argsMap.put("changeLogFile", resourcesDirFullPath + sqlSpecificChangelogFile)
}
executeCommandScope("generateChangelog", argsMap, testInput.databaseName)

Expand All @@ -81,6 +83,35 @@ class GenerateChangelogTest extends Specification {
assert generatedChangelog.contains("$testInput.change")
}

when: "get sql generated for the change set"
String generatedSql
argsMap.put("changeLogFile", resourcesDirPath + entry.value)
if (!entry.key.equalsIgnoreCase("expectedSqlChangelog")) {
generatedSql = parseQuery(executeCommandScope("updateSql", argsMap).toString())
generatedSql = removeSchemaNames(generatedSql, testInput.database)
}


then: "execute updateSql command on generated changelogs"
if (!entry.key.equalsIgnoreCase("expectedSqlChangelog")) {
def expectedSql
try {
expectedSql = parseQuery(getSqlFileContent(testInput.change, testInput.databaseName, testInput.version,
"liquibase/harness/generateChangelog/verificationSql")).toLowerCase()
} catch (NullPointerException exception) {
expectedSql = parseQuery(getSqlFileContent(testInput.change, testInput.databaseName, testInput.version,
"liquibase/harness/generateChangelog/expectedSql")).toLowerCase()
}
def generatedSqlIsCorrect = generatedSql == expectedSql
if (!generatedSqlIsCorrect) {
Scope.getCurrentScope().getUI().sendMessage("FAIL! Expected sql doesn't " +
"match generated sql! \nEXPECTED SQL: \n" + expectedSql + " \n" +
"GENERATED SQL: \n" + generatedSql)
assert generatedSql == expectedSql
}
}


and: "rollback changes"
argsMap.put("changeLogFile", testInput.xmlChangelogPath)
strategy.performRollback(argsMap)
Expand All @@ -97,9 +128,9 @@ class GenerateChangelogTest extends Specification {
}
for (Map.Entry<String, String> entry : map.entrySet()) {
if (entry.key.equalsIgnoreCase("expectedSqlChangelog")) {
deleteFile(testResourcesPath + sqlSpecificChangelogFile)
deleteFile(resourcesDirFullPath + sqlSpecificChangelogFile)
} else {
deleteFile(testResourcesPath + entry.value)
deleteFile(resourcesDirFullPath + entry.value)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class GenerateChangelogTestHelper {
for (DatabaseUnderTest databaseUnderTest : databaseConnectionUtil
.initializeDatabasesConnection(TestConfig.instance.getFilteredDatabasesUnderTest())) {
for (def changeLogEntry : FileUtils.resolveInputFilePaths(databaseUnderTest, baseChangelogPath +
"expectedChangeLog", "sql").entrySet()) {
"expectedChangeLog", "xml").entrySet()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we use TestConfig.instance.inputFormat here? Or maybe we can change to build testInput in other way as we don't really pick up only files with this extension

if (!commandLineChangesList || commandLineChangesList.contains(changeLogEntry.key)) {
inputList.add(TestInput.builder()
.databaseName(databaseUnderTest.name)
Expand All @@ -46,8 +46,12 @@ class GenerateChangelogTestHelper {
"stress/select", "xml").get(changeLogEntry.key))
.xmlChangelogPath(FileUtils.resolveInputFilePaths(databaseUnderTest, baseChangelogPath +
"expectedChangeLog", "xml").get(changeLogEntry.key))
.jsonChangelogPath(FileUtils.resolveInputFilePaths(databaseUnderTest, baseChangelogPath +
"expectedChangeLog", "json").get(changeLogEntry.key))
.ymlChangelogPath(FileUtils.resolveInputFilePaths(databaseUnderTest, baseChangelogPath +
"expectedChangeLog", "yml").get(changeLogEntry.key))
.sqlChangelogPath(FileUtils.resolveInputFilePaths(databaseUnderTest, baseChangelogPath +
"expectedChangeLog", "sql").get(changeLogEntry.key))
"expectedSql", "sql").get(changeLogEntry.key))
.change(changeLogEntry.key)
.database(databaseUnderTest.database)
.build())
Expand Down Expand Up @@ -83,6 +87,19 @@ class GenerateChangelogTestHelper {
}
}

static String getSqlSpecificChangelogFile (String dbName, String changelogFileName) {
def replacementName = String.format(".%s.sql", getShortDatabaseName(dbName))
return changelogFileName.replace(".sql", replacementName)
}

static String removeSchemaNames(String generatedSql, Database database) {
if (database.getShortName().equals("sqlite")) {
return generatedSql.toLowerCase()
}
def schemaName = database.getDefaultSchemaName().toLowerCase()
return generatedSql.toLowerCase().replace(schemaName + ".", "").replace("\"" + schemaName + "\".", "")
}

@Builder
@ToString(includeNames = true, includeFields = true, includePackage = false, excludes = 'database,password')
static class TestInput {
Expand All @@ -96,6 +113,8 @@ class GenerateChangelogTestHelper {
String updateChangelogPath
String selectChangelogPath
String xmlChangelogPath
String jsonChangelogPath
String ymlChangelogPath
String sqlChangelogPath
String dbSchema
String change
Expand Down
5 changes: 3 additions & 2 deletions src/main/groovy/liquibase/harness/util/TestUtils.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ class TestUtils {
}
commandScope.setOutput(outputStream)
try {
Logger.getLogger(this.class.name).info(String.format("Executing liquibase command: %s ", commandName))
commandScope.execute()
//TODO investigate and fix mssql issue with autogenerated objects
if (commandName.equals("generateChangelog")) {
String testResourcesPath = String.format("%s/src/test/resources/liquibase/harness/generateChangelog/expectedChangeLog/%s/objects/", System.getProperty("user.dir"), dbName) + ""
Expand All @@ -85,6 +83,9 @@ class TestUtils {
FileUtils.forceDelete(new File(testResourcesPath))
}
}
Logger.getLogger(this.class.name).info(String.format("Executing liquibase command: %s ", commandName))
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)
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table (id BIGINT NOT NULL, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT "primary" PRIMARY KEY (id));
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table (test_column BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, "varcharColumn" VARCHAR(25), "intColumn" BIGINT, "dateColumn" date, CONSTRAINT test_table_pkey PRIMARY KEY (rowid));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test_table (id BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT test_table_pkey PRIMARY KEY (rowid));

CREATE INDEX idx_first_name ON test_table(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table_xml (test_column BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT test_table_xml_pkey PRIMARY KEY (rowid));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test_table (id BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT test_table_pkey PRIMARY KEY (rowid));

ALTER TABLE test_table ADD CONSTRAINT test_unique_constraint UNIQUE (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE posts ADD CONSTRAINT test_check_constraint CHECK (((id > 0)));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE test_table_base (id BIGINT NOT NULL, CONSTRAINT test_table_base_pkey PRIMARY KEY (id));

CREATE TABLE test_table_reference (id BIGINT NOT NULL, test_column BIGINT NOT NULL, CONSTRAINT test_table_reference_pkey PRIMARY KEY (id));

ALTER TABLE test_table_reference ADD CONSTRAINT test_table_reference_test_column_key UNIQUE (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
@@ -0,0 +1 @@
CREATE TABLE test_table (test_column BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, "varcharColumn" VARCHAR(25), "intColumn" BIGINT, "dateColumn" date, CONSTRAINT "primary" PRIMARY KEY (rowid));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test_table (id BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT "primary" PRIMARY KEY (rowid));

CREATE INDEX idx_first_name ON test_table(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SEQUENCE IF NOT EXISTS test_sequence START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table_xml (test_column BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT "primary" PRIMARY KEY (rowid));
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE VIEW test_view AS SELECT id, first_name, last_name, email FROM lbcat.authors;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table (id BIGINT NOT NULL, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT test_pk PRIMARY KEY (id));
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test_table (id BIGINT, rowid BIGINT DEFAULT unique_rowid() NOT NULL, CONSTRAINT "primary" PRIMARY KEY (rowid));

ALTER TABLE test_table ADD CONSTRAINT test_unique_constraint UNIQUE (id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE TEST_TABLE (ID INTEGER NOT NULL, CONSTRAINT PK_TEST_TABLE PRIMARY KEY (ID));

CREATE VIEW test_view AS select id from test_table;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test trigger function created';
RETURN NEW;
END;
$function$;

CREATE TRIGGER test_trigger BEFORE INSERT ON posts FOR EACH ROW EXECUTE PROCEDURE test_function();
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test trigger function created';
RETURN NEW;
END;
$function$;

CREATE TRIGGER test_trigger BEFORE INSERT ON posts FOR EACH ROW EXECUTE PROCEDURE test_function();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SEQUENCE IF NOT EXISTS test_sequence;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test trigger function created';
RETURN NEW;
END;
$function$;

CREATE TRIGGER test_trigger BEFORE INSERT ON posts FOR EACH ROW EXECUTE PROCEDURE test_function();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SEQUENCE IF NOT EXISTS test_sequence;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test trigger function created';
RETURN NEW;
END;
$function$;

CREATE TRIGGER test_trigger BEFORE INSERT ON posts FOR EACH ROW EXECUTE PROCEDURE test_function();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE posts ADD CONSTRAINT test_check_constraint CHECK (((id > 0)));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE test_table_reference (id INTEGER NOT NULL, test_column INTEGER NOT NULL, CONSTRAINT test_table_reference_pkey PRIMARY KEY (id));

ALTER TABLE test_table_reference ADD CONSTRAINT test_table_reference_test_column_key UNIQUE (test_column);

CREATE TABLE test_table_base (id INTEGER NOT NULL, CONSTRAINT test_table_base_pkey PRIMARY KEY (id));

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
@@ -0,0 +1 @@
CREATE TABLE test_table (test_column INTEGER, "varcharColumn" VARCHAR(25), "intColumn" INTEGER, "dateColumn" TIMESTAMP WITHOUT TIME ZONE);
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test function created';
RETURN NEW;
END;
$function$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE TABLE test_table (id INTEGER);

CREATE INDEX idx_first_name ON test_table(id);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE OR REPLACE PACKAGE test_package IS
PROCEDURE test_procedure();
END;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE OR REPLACE PACKAGE test_package IS
PROCEDURE test_procedure();
END;

CREATE OR REPLACE PACKAGE BODY test_package IS
PROCEDURE test_procedure() IS
BEGIN
DELETE FROM posts WHERE author_id = '15';
END;
END
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE OR REPLACE PROCEDURE test_procedure()
SECURITY DEFINER
AS $procedure$
BEGIN
END;
$procedure$
LANGUAGE edbspl;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE SEQUENCE IF NOT EXISTS test_sequence AS integer START WITH 1 INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE PUBLIC SYNONYM TEST_SYNONYM FOR AUTHORS;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE test_table_xml (test_column INTEGER);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION test_function()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
RAISE NOTICE 'Test trigger function created';
RETURN NEW;
END;
$function$;

CREATE TRIGGER test_trigger BEFORE INSERT ON posts FOR EACH ROW EXECUTE FUNCTION test_function();
Loading