Skip to content

Commit b5e2782

Browse files
committed
WIP Issue 368: actually prompt user again if pending migration detected
1 parent a1fdbc5 commit b5e2782

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

src/FlywayMigrationPlugin.groovy

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
2020
withEnv(environmentVariables) {
2121
def command = new FlywayCommand('info')
2222
sh buildFlywayCommand(command)
23-
if (confirmBeforeApply && hasPendingMigration(delegate)) {
24-
confirmMigration(delegate)
25-
}
2623
}
2724
}
2825
}
@@ -42,7 +39,15 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
4239

4340
public void confirmMigration(workflowScript) {
4441
def closure = {
45-
sh "echo Pending migration detected, prompt again"
42+
def userInput
43+
try {
44+
timeout(time: 1, unit: 'MINUTES') {
45+
userInput = input("There is a pending migration that will be applied immediately if you continue. Please review the flyway info output. Are you sure you want to continue?")
46+
sh "echo userInput was: ${userInput}"
47+
}
48+
} catch (ex) {
49+
throw ex
50+
}
4651
}
4752

4853
closure.delegate = workflowScript
@@ -51,6 +56,10 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
5156

5257
public Closure flywayMigrateClosure() {
5358
return { innerClosure ->
59+
if (confirmBeforeApply && hasPendingMigration(delegate)) {
60+
confirmMigration(delegate)
61+
}
62+
5463
innerClosure()
5564

5665
def environmentVariables = buildEnvironmentVariableList(env)

test/FlywayMigrationPluginTest.groovy

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -103,73 +103,73 @@ class FlywayMigrationPluginTest {
103103

104104
verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
105105
}
106+
}
106107

108+
@Nested
109+
public class FlywayMigrateClosure {
107110
@Test
108-
void runsConfirmMigrationIfConfirmBeforeApplyAndHasPendingMigration() {
109-
def plugin = spy(new FlywayMigrationPlugin())
110-
doReturn(true).when(plugin).hasPendingMigration(any(Object.class))
111-
FlywayMigrationPlugin.confirmBeforeApplyingMigration()
111+
void runsTheNestedClosure() {
112+
def plugin = new FlywayMigrationPlugin()
113+
def iWasCalled = false
114+
def nestedClosure = { -> iWasCalled = true }
112115

113-
def flywayClosure = plugin.flywayInfoClosure()
116+
def flywayClosure = plugin.flywayMigrateClosure()
114117
flywayClosure.delegate = new MockWorkflowScript()
115-
flywayClosure { -> }
118+
flywayClosure(nestedClosure)
116119

117-
verify(plugin).confirmMigration(any(Object.class))
120+
assertThat(iWasCalled, equalTo(true))
118121
}
119122

120123
@Test
121-
void doesNotRunConfirmMigrationIfNotConfirmBeforeApplyAndHasPendingMigration() {
124+
void setsTheListOfOptionalEnvironmentVariables() {
122125
def plugin = spy(new FlywayMigrationPlugin())
123-
doReturn(true).when(plugin).hasPendingMigration(any(Object.class))
126+
def expectedList = ['KEY=value']
127+
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
128+
def flywayClosure = plugin.flywayMigrateClosure()
129+
def mockWorkflowScript = spy(new MockWorkflowScript())
130+
flywayClosure.delegate = mockWorkflowScript
124131

125-
def flywayClosure = plugin.flywayInfoClosure()
126-
flywayClosure.delegate = new MockWorkflowScript()
127132
flywayClosure { -> }
128133

129-
verify(plugin, times(0)).confirmMigration(any(Object.class))
134+
verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
130135
}
131136

132137
@Test
133-
void doesNotRunConfirmMigrationIfConfirmBeforeApplyAndDoesNotHavePendingMigration() {
138+
void runsConfirmMigrationIfConfirmBeforeApplyAndHasPendingMigration() {
134139
def plugin = spy(new FlywayMigrationPlugin())
135-
doReturn(false).when(plugin).hasPendingMigration(any(Object.class))
140+
doReturn(true).when(plugin).hasPendingMigration(any(Object.class))
136141
FlywayMigrationPlugin.confirmBeforeApplyingMigration()
137142

138-
def flywayClosure = plugin.flywayInfoClosure()
143+
def flywayClosure = plugin.flywayMigrateClosure()
139144
flywayClosure.delegate = new MockWorkflowScript()
140145
flywayClosure { -> }
141146

142-
verify(plugin, times(0)).confirmMigration(any(Object.class))
147+
verify(plugin).confirmMigration(any(Object.class))
143148
}
144-
}
145149

146-
@Nested
147-
public class FlywayMigrateClosure {
148150
@Test
149-
void runsTheNestedClosure() {
150-
def plugin = new FlywayMigrationPlugin()
151-
def iWasCalled = false
152-
def nestedClosure = { -> iWasCalled = true }
151+
void doesNotRunConfirmMigrationIfNotConfirmBeforeApplyAndHasPendingMigration() {
152+
def plugin = spy(new FlywayMigrationPlugin())
153+
doReturn(true).when(plugin).hasPendingMigration(any(Object.class))
153154

154155
def flywayClosure = plugin.flywayMigrateClosure()
155156
flywayClosure.delegate = new MockWorkflowScript()
156-
flywayClosure(nestedClosure)
157+
flywayClosure { -> }
157158

158-
assertThat(iWasCalled, equalTo(true))
159+
verify(plugin, times(0)).confirmMigration(any(Object.class))
159160
}
160161

161162
@Test
162-
void setsTheListOfOptionalEnvironmentVariables() {
163+
void doesNotRunConfirmMigrationIfConfirmBeforeApplyAndDoesNotHavePendingMigration() {
163164
def plugin = spy(new FlywayMigrationPlugin())
164-
def expectedList = ['KEY=value']
165-
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
166-
def flywayClosure = plugin.flywayMigrateClosure()
167-
def mockWorkflowScript = spy(new MockWorkflowScript())
168-
flywayClosure.delegate = mockWorkflowScript
165+
doReturn(false).when(plugin).hasPendingMigration(any(Object.class))
166+
FlywayMigrationPlugin.confirmBeforeApplyingMigration()
169167

168+
def flywayClosure = plugin.flywayMigrateClosure()
169+
flywayClosure.delegate = new MockWorkflowScript()
170170
flywayClosure { -> }
171171

172-
verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
172+
verify(plugin, times(0)).confirmMigration(any(Object.class))
173173
}
174174
}
175175

0 commit comments

Comments
 (0)