Skip to content

Commit

Permalink
Issue 21: Run flyway migrate on apply
Browse files Browse the repository at this point in the history
  • Loading branch information
kmanning committed Apr 14, 2021
1 parent a778284 commit af97e76
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/FlywayMigrationPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab

public void apply(TerraformEnvironmentStage stage) {
stage.decorate(TerraformEnvironmentStage.PLAN, flywayInfoClosure())
stage.decorate(TerraformEnvironmentStage.APPLY, flywayMigrateClosure())
}

public Closure flywayInfoClosure() {
Expand All @@ -22,6 +23,18 @@ class FlywayMigrationPlugin implements TerraformEnvironmentStagePlugin, Resettab
}
}

public Closure flywayMigrateClosure() {
return { innerClosure ->
innerClosure()

def environmentVariables = buildEnvironmentVariableList(env)
withEnv(environmentVariables) {
def command = new FlywayCommand('migrate')
sh command.toString()
}
}
}

public Collection buildEnvironmentVariableList(env) {
def list = []
if (passwordVariable) {
Expand Down
42 changes: 42 additions & 0 deletions test/FlywayMigrationPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ class FlywayMigrationPluginTest {

verify(stage).decorate(TerraformEnvironmentStage.PLAN, infoClosure)
}

@Test
void addsFlywayMigrateClosureOnApply() {
def migrateClosure = { -> }
def plugin = spy(new FlywayMigrationPlugin())
doReturn(migrateClosure).when(plugin).flywayMigrateClosure()
def stage = mock(TerraformEnvironmentStage.class)

plugin.apply(stage)

verify(stage).decorate(TerraformEnvironmentStage.APPLY, migrateClosure)
}
}

@Nested
Expand Down Expand Up @@ -91,6 +103,36 @@ class FlywayMigrationPluginTest {
}
}

@Nested
public class FlywayMigrateClosure {
@Test
void runsTheNestedClosure() {
def plugin = new FlywayMigrationPlugin()
def iWasCalled = false
def nestedClosure = { -> iWasCalled = true }

def flywayClosure = plugin.flywayMigrateClosure()
flywayClosure.delegate = new MockWorkflowScript()
flywayClosure(nestedClosure)

assertThat(iWasCalled, equalTo(true))
}

@Test
void setsTheListOfOptionalEnvironmentVariables() {
def plugin = spy(new FlywayMigrationPlugin())
def expectedList = ['KEY=value']
doReturn(expectedList).when(plugin).buildEnvironmentVariableList(any(List.class))
def flywayClosure = plugin.flywayMigrateClosure()
def mockWorkflowScript = spy(new MockWorkflowScript())
flywayClosure.delegate = mockWorkflowScript

flywayClosure { -> }

verify(mockWorkflowScript).withEnv(eq(expectedList), any(Closure.class))
}
}

@Nested
public class BuildEnvironmentVariableList {
@Test
Expand Down

0 comments on commit af97e76

Please sign in to comment.