Skip to content

Commit

Permalink
generated addPrimaryKey changes should not include column direction (…
Browse files Browse the repository at this point in the history
…DAT-17467) (#5922)

* generated addPrimaryKey changes should not include column direction

* close output stream after running snapshot

* update test name and fix delete

---------

Co-authored-by: suryaaki2 <80348493+suryaaki2@users.noreply.github.com>
  • Loading branch information
StevenMassaro and suryaaki2 committed May 23, 2024
1 parent 8f44b36 commit 533ea05
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class CommandUtil {
OutputStream outputStream = new FileOutputStream(new File(outputFile))
commandScope.setOutput(outputStream)
commandScope.execute()
outputStream.close()
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package liquibase.diffchangelog

import liquibase.Scope
import liquibase.command.CommandScope
import liquibase.command.core.DiffChangelogCommandStep
import liquibase.command.core.helpers.DbUrlConnectionCommandStep
import liquibase.command.core.helpers.ReferenceDbUrlConnectionCommandStep
import liquibase.command.util.CommandUtil
import liquibase.extension.testing.testsystem.DatabaseTestSystem
import liquibase.extension.testing.testsystem.TestSystemFactory
import liquibase.extension.testing.testsystem.spock.LiquibaseIntegrationTest
import liquibase.util.FileUtil
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.RandomStringUtils
import spock.lang.Shared
import spock.lang.Specification

@LiquibaseIntegrationTest
class DiffChangelogMssqlIntegrationTest extends Specification {

@Shared
private DatabaseTestSystem mssql =
(DatabaseTestSystem) Scope.getCurrentScope().getSingleton(TestSystemFactory.class).getTestSystem("mssql")

def "column direction is not included in addPrimaryKey change" () {
def snapshotFilename = "target-${RandomStringUtils.randomAlphabetic(10)}.json"
def snapshotFilepath = "target/test-classes/" + snapshotFilename
def changelogFile = "target/test-classes/diffChangelog-${RandomStringUtils.randomAlphabetic(10)}.xml"
when:
mssql.executeSql("""
CREATE TABLE MyTest (
ID INT not null,
Name VARCHAR(50),
Age INT);""")
CommandUtil.runSnapshot(mssql, snapshotFilepath)
mssql.executeSql("""
ALTER TABLE MyTest
ADD CONSTRAINT PK_MyTest PRIMARY KEY (ID DESC);""")
CommandScope commandScope = new CommandScope(DiffChangelogCommandStep.COMMAND_NAME)
commandScope.addArgumentValue(DiffChangelogCommandStep.CHANGELOG_FILE_ARG, changelogFile)
commandScope.addArgumentValue(ReferenceDbUrlConnectionCommandStep.REFERENCE_URL_ARG, mssql.getConnectionUrl())
commandScope.addArgumentValue(ReferenceDbUrlConnectionCommandStep.REFERENCE_USERNAME_ARG, mssql.getUsername())
commandScope.addArgumentValue(ReferenceDbUrlConnectionCommandStep.REFERENCE_PASSWORD_ARG, mssql.getPassword())
commandScope.addArgumentValue(DbUrlConnectionCommandStep.URL_ARG, "offline:mssql?snapshot=" + snapshotFilename)

then:
commandScope.execute()
def generatedChangelog = new File(changelogFile)
def generatedChangelogContents = FileUtil.getContents(generatedChangelog)
generatedChangelogContents.contains('<addPrimaryKey columnNames="ID" constraintName="PK_MyTest" tableName="MyTest"/>')

cleanup:
generatedChangelog.delete()
FileUtils.forceDelete(new File(snapshotFilepath))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Schema getSchema() {
}

public String getColumnNames() {
return StringUtil.join(getColumns(), ", ", obj -> ((Column) obj).toString(false));
return StringUtil.join(getColumns(), ", ", obj -> ((Column) obj).getName());
}

/**
Expand Down

0 comments on commit 533ea05

Please sign in to comment.