Skip to content

Commit cc0fd8f

Browse files
committed
WIP: start sketching out flway migrate on apply
1 parent 43f3365 commit cc0fd8f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

src/FlywayMigrationPlugin.groovy

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
88

99
public void apply(TerraformEnvironmentStage stage) {
1010
stage.decorate(TerraformEnvironmentStage.PLAN, flywayInfoClosure())
11+
stage.decorate(TerraformEnvironmentStage.APPLY, flywayMigrateClosure())
1112
}
1213

1314
public Closure flywayInfoClosure() {
@@ -22,6 +23,17 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
2223
}
2324
}
2425

26+
public Closure flywayMigrateClosure() {
27+
return { innerClosure ->
28+
innerClosure()
29+
30+
def environmentVariables = buildEnvironmentVariableList(env)
31+
withEnv(environmentVariables) {
32+
sh "echo Run flyway migrate"
33+
}
34+
}
35+
}
36+
2537
public Collection buildEnvironmentVariableList(env) {
2638
def list = []
2739
if (passwordVariable) {

test/FlywayMigrationPluginTest.groovy

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,18 @@ class FlywayMigrationPluginTest {
3939

4040
verify(stage).decorate(TerraformEnvironmentStage.PLAN, infoClosure)
4141
}
42+
43+
@Test
44+
void addsFlywayMigrateClosureOnApply() {
45+
def migrateClosure = { -> }
46+
def plugin = spy(new FlywayMigrationPlugin())
47+
doReturn(migrateClosure).when(plugin).flywayMigrateClosure()
48+
def stage = mock(TerraformEnvironmentStage.class)
49+
50+
plugin.apply(stage)
51+
52+
verify(stage).decorate(TerraformEnvironmentStage.APPLY, migrateClosure)
53+
}
4254
}
4355

4456
@Nested
@@ -91,6 +103,36 @@ class FlywayMigrationPluginTest {
91103
}
92104
}
93105

106+
@Nested
107+
public class FlywayMigrateClosure {
108+
@Test
109+
void runsTheNestedClosure() {
110+
def plugin = new FlywayMigrationPlugin()
111+
def iWasCalled = false
112+
def nestedClosure = { -> iWasCalled = true }
113+
114+
def flywayClosure = plugin.flywayMigrateClosure()
115+
flywayClosure.delegate = new MockWorkflowScript()
116+
flywayClosure(nestedClosure)
117+
118+
assertThat(iWasCalled, equalTo(true))
119+
}
120+
121+
@Test
122+
void setsTheListOfOptionalEnvironmentVariables() {
123+
def plugin = spy(new FlywayMigrationPlugin())
124+
def expectedList = ['KEY=value']
125+
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
126+
def flywayClosure = plugin.flywayMigrateClosure()
127+
def mockWorkflowScript = spy(new MockWorkflowScript())
128+
flywayClosure.delegate = mockWorkflowScript
129+
130+
flywayClosure { -> }
131+
132+
verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
133+
}
134+
}
135+
94136
@Nested
95137
public class BuildEnvironmentVariableList {
96138
@Test

0 commit comments

Comments
 (0)