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

Fix load relative file for LoadDataChange, CreateProcedureChange or CreateViewChange #2073

Merged
merged 9 commits into from
Dec 16, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public InputStream openSqlStream() throws IOException {
String relativeTo = null;
final Boolean isRelative = isRelativeToChangelogFile();
if (isRelative != null && isRelative) {
relativeTo = getChangeSet().getFilePath();
relativeTo = getChangeSet().getChangeLog().getPhysicalFilePath();
}
return Scope.getCurrentScope().getResourceAccessor().openStream(relativeTo, path);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ protected InputStream openSqlStream() throws IOException {
String path = getPath();
String relativeTo = null;
if (ObjectUtil.defaultIfNull(getRelativeToChangelogFile(), false)) {
relativeTo = getChangeSet().getFilePath();
relativeTo = getChangeSet().getChangeLog().getPhysicalFilePath();
}
return Scope.getCurrentScope().getResourceAccessor().openStream(relativeTo, path);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ public CSVReader getCSVReader() throws IOException, LiquibaseException {
protected String getRelativeTo() {
String relativeTo = null;
if (ObjectUtil.defaultIfNull(isRelativeToChangelogFile(), false)) {
relativeTo = getChangeSet().getFilePath();
relativeTo = getChangeSet().getChangeLog().getPhysicalFilePath();
}
return relativeTo;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package liquibase.change.core

import liquibase.Scope
import liquibase.change.StandardChangeTest
import liquibase.changelog.ChangeSet
import liquibase.changelog.DatabaseChangeLog
import liquibase.database.core.OracleDatabase
import liquibase.parser.core.ParsedNode
import liquibase.database.core.MockDatabase
import liquibase.sdk.resource.MockResourceAccessor
import liquibase.snapshot.MockSnapshotGeneratorFactory
import liquibase.snapshot.SnapshotGeneratorFactory
import liquibase.test.JUnitResourceAccessor
import liquibase.util.StreamUtil
import spock.lang.Unroll

public class CreateProcedureChangeTest extends StandardChangeTest {

Expand Down Expand Up @@ -39,4 +45,32 @@ public class CreateProcedureChangeTest extends StandardChangeTest {
then:
change.serialize().toString() == "createProcedure[procedureBody=create procedure sql]"
}

@Unroll
def "load correct file"() {
when:
def changelog = new DatabaseChangeLog("com/example/changelog.xml")

def changeset = new ChangeSet("1", "auth", false, false, logicalFilePath, null, null, changelog)

def change = new CreateProcedureChange()
change.path = sqlPath
change.relativeToChangelogFile = relativeToChangelogFile
change.setChangeSet(changeset)

String fileContents = Scope.child([(Scope.Attr.resourceAccessor.name()): new JUnitResourceAccessor()], {
return StreamUtil.readStreamAsString(change.openSqlStream())
} as Scope.ScopedRunnerWithReturn<String>)

then:
fileContents.trim() == "My Logic Here"

where:
sqlPath | logicalFilePath | relativeToChangelogFile
"com/example/my-logic.sql" | null | false
"com/example/my-logic.sql" | "a/logical/path.xml" | false
"my-logic.sql" | null | true
"my-logic.sql" | "a/logical/path.xml" | true

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package liquibase.change.core

import liquibase.Scope
import liquibase.change.ChangeStatus
import liquibase.change.StandardChangeTest
import liquibase.changelog.ChangeSet
import liquibase.changelog.DatabaseChangeLog
import liquibase.database.core.MockDatabase
import liquibase.exception.SetupException
import liquibase.parser.core.ParsedNodeException
import liquibase.snapshot.MockSnapshotGeneratorFactory
import liquibase.snapshot.SnapshotGeneratorFactory
import liquibase.structure.core.View
import liquibase.test.JUnitResourceAccessor
import liquibase.util.StreamUtil
import spock.lang.Unroll

public class CreateViewChangeTest extends StandardChangeTest {

Expand Down Expand Up @@ -56,4 +62,32 @@ public class CreateViewChangeTest extends StandardChangeTest {
change.viewName == "my_view"
change.selectQuery == "select * from test"
}

@Unroll
def "openSqlStream correctly opens files"() {
when:
def changelog = new DatabaseChangeLog("com/example/changelog.xml")

def changeset = new ChangeSet("1", "auth", false, false, logicalFilePath, null, null, changelog)

def change = new CreateViewChange()
change.path = sqlPath
change.relativeToChangelogFile = relativeToChangelogFile
change.setChangeSet(changeset)

String fileContents = Scope.child([(Scope.Attr.resourceAccessor.name()): new JUnitResourceAccessor()], {
return StreamUtil.readStreamAsString(change.openSqlStream())
} as Scope.ScopedRunnerWithReturn<String>)

then:
fileContents.trim() == "My Logic Here"

where:
sqlPath | logicalFilePath | relativeToChangelogFile
"com/example/my-logic.sql" | null | false
"com/example/my-logic.sql" | "a/logical/path.xml" | false
"my-logic.sql" | null | true
"my-logic.sql" | "a/logical/path.xml" | true

}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package liquibase.change.core

import liquibase.Scope
import liquibase.change.ChangeStatus
import liquibase.change.StandardChangeTest
import liquibase.changelog.ChangeSet
import liquibase.changelog.DatabaseChangeLog
import liquibase.database.DatabaseConnection
import liquibase.database.DatabaseFactory
import liquibase.database.core.MSSQLDatabase
Expand All @@ -22,7 +24,9 @@ import liquibase.structure.DatabaseObject
import liquibase.structure.core.Column
import liquibase.structure.core.DataType
import liquibase.structure.core.Table
import liquibase.test.JUnitResourceAccessor
import liquibase.test.TestContext
import liquibase.util.csv.CSVReader
import spock.lang.Unroll

import java.sql.Timestamp
Expand Down Expand Up @@ -347,9 +351,10 @@ public class LoadDataChangeTest extends StandardChangeTest {

def "relativeToChangelogFile works"() throws Exception {
when:
def changelog = new DatabaseChangeLog("liquibase/changelog.xml")
ChangeSet changeSet = new ChangeSet(null, null, true, false,
"liquibase/empty.changelog.xml",
null, null, false, null, null);
"logical or physical file name",
null, null, false, null, changelog);

LoadDataChange relativeChange = new LoadDataChange();

Expand All @@ -375,6 +380,33 @@ public class LoadDataChangeTest extends StandardChangeTest {
assert relativeStatements.size() == nonRelativeStatements.size()
}

@Unroll
def "openSqlStream correctly opens files"() {
when:
def changelog = new DatabaseChangeLog("com/example/changelog.xml")

def changeset = new ChangeSet("1", "auth", false, false, logicalFilePath, null, null, changelog)

def change = new LoadDataChange()
change.file = csvPath
change.relativeToChangelogFile = relativeToChangelogFile
change.setChangeSet(changeset)

CSVReader csvReader = Scope.child([(Scope.Attr.resourceAccessor.name()): new JUnitResourceAccessor()], {
return change.getCSVReader()
} as Scope.ScopedRunnerWithReturn<CSVReader>)

then:
csvReader != null

where:
csvPath | logicalFilePath | relativeToChangelogFile
"com/example/users.csv" | null | false
"com/example/users.csv" | "a/logical/path.xml" | false
"users.csv" | null | true
"users.csv" | "a/logical/path.xml" | true
}

def "checksum does not change when no comments in CSV and comment property changes"() {
when:
LoadDataChange refactoring = new LoadDataChange();
Expand Down