Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ require 'rubygems'
require 'puppetlabs_spec_helper/rake_tasks'
require 'puppet-lint/tasks/puppet-lint'

PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.send('disable_80chars')
PuppetLint.configuration.send('disable_140chars')
6 changes: 3 additions & 3 deletions manifests/flyway.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@
include ::java
Class['::java'] -> Database_schema::Flyway_migration<||>
}

archive { "flyway-commandline-${version}":
ensure => $ensure,
url => $real_source,
target => $target_dir,
root_dir => "flyway-${version}",
checksum => false
}

Class['database_schema::flyway'] -> Database_schema::Flyway_migration<||>
}
}
20 changes: 16 additions & 4 deletions manifests/flyway_migration.pp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
# Version number to migrate up to (see the migrate option "target" in the flyway docs). Defaults to "latest"
# [*placeholders*]
# A hash containing placeholders you want flyway to use. Each key expands to a "-placeholder.KEY='VALUE'" argument to flyway.
# [*baseline_on_migrate*]
# Whether to automatically call baseline when migrate is executed against a non-empty schema with no metadata table (see baselineOnMigrate flyway docs).
# Defaults to 'undef'. When 'undef' the -baselineOnMigrate command option is not passed to flyway and flyway will use its own default or the value in its configuration file (if present).
# Set to `true` or `false` to explicitly override the setting for this migration.
#
define database_schema::flyway_migration (
$schema_source,
Expand All @@ -31,6 +35,7 @@
$target_schemas = undef,
$ensure = latest,
$placeholders = {},
$baseline_on_migrate = undef,
){
$title_hash = sha1($title)
$staging_path = "/tmp/flyway-migration-${title_hash}"
Expand All @@ -39,21 +44,28 @@
recurse => true,
source => $schema_source
}

validate_hash($placeholders)
$placeholders_str = flyway_cmd_placeholders($placeholders)

if $baseline_on_migrate == undef {
$baseline_on_migrate_str = undef
} else {
validate_bool($baseline_on_migrate)
$baseline_on_migrate_str = " -baselineOnMigrate=${baseline_on_migrate}"
}

$target_version = $ensure ? {latest => '', default => " -target=${ensure}"}
$flyway_base_command = "flyway -user='${db_username}' -password='${db_password}' -url='${jdbc_url}' ${placeholders_str} -locations='filesystem:${staging_path}'$target_version"
$flyway_base_command = "flyway -user='${db_username}' -password='${db_password}' -url='${jdbc_url}' ${placeholders_str} -locations='filesystem:${staging_path}'${target_version}${baseline_on_migrate_str}"

if $target_schemas == undef {
$flyway_command = $flyway_base_command
}
else {
$joined_schemas = join($target_schemas, ',')
$flyway_command = "${flyway_base_command} -schemas='${joined_schemas}'"
}

exec { "Migration for ${title}":
cwd => $flyway_path,
path => "${flyway_path}:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin",
Expand Down
12 changes: 6 additions & 6 deletions manifests/liquibase.pp
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,29 @@
undef => "http://repo1.maven.org/maven2/org/liquibase/liquibase-core/${version}/liquibase-core-${version}-bin.tar.gz",
default => $source
}

$dir_ensure = $ensure ? {
absent => absent,
default => directory
}

if $ensure == present and $manage_java {
include ::java
Class['::java'] -> Database_schema::Liquibase_migration<||>
}

file { "${target_dir}/liquibase":
ensure => $dir_ensure,
force => true
}

archive { "liquibase-core-${version}-bin":
ensure => $ensure,
url => $real_source,
target => "${target_dir}/liquibase",
root_dir => 'liquibase',
checksum => false
}

Class['database_schema::liquibase'] -> Database_schema::Liquibase_migration<||>
}
}
8 changes: 4 additions & 4 deletions manifests/liquibase_migration.pp
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@
ensure => present,
source => $changelog_source
}

$liquibase_base_command = "liquibase --username='${db_username}' --password='${db_password}' --url='${jdbc_url}' --changeLogFile='${changelog_path}'"

if $default_schema == undef {
$flyway_command = $liquibase_base_command
}
else {
$flyway_command = "${liquibase_base_command} --defaultSchemaNAme='${default_schema}'"
}

exec { "Migration for ${title}":
cwd => $liquibase_path,
path => "${liquibase_path}:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin",
onlyif => "${flyway_command} status | grep 'change sets have not been applied'",
command => "${flyway_command} update",
require => File[$changelog_path]
}
}
}
34 changes: 34 additions & 0 deletions spec/defines/flyway_migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,38 @@
end
end
end
describe 'baseline_on_migrate' do
context 'when not specified' do
it 'should not have baselineOnMigrate in exec command' do
command = catalogue.resource('exec', 'Migration for example db').send(:parameters)[:command]
expect(command).not_to include('baselineOnMigrate')
end
end
context 'when true' do
let(:params){{
:schema_source => '/some/path',
:db_username => 'user',
:db_password => 'supersecret',
:jdbc_url => 'jdbc:h2:test',
:baseline_on_migrate => true,
}}
it 'should have -baselineOnMigrate=true in exec command' do
command = catalogue.resource('exec', 'Migration for example db').send(:parameters)[:command]
expect(command).to match('flyway -user=\'user\' -password=\'supersecret\' -url=\'jdbc:h2:test\' -locations=\'.*\' -baselineOnMigrate=true')
end
end
context 'when true' do
let(:params){{
:schema_source => '/some/path',
:db_username => 'user',
:db_password => 'supersecret',
:jdbc_url => 'jdbc:h2:test',
:baseline_on_migrate => false,
}}
it 'should have -baselineOnMigrate=true in exec command' do
command = catalogue.resource('exec', 'Migration for example db').send(:parameters)[:command]
expect(command).to match('flyway -user=\'user\' -password=\'supersecret\' -url=\'jdbc:h2:test\' -locations=\'.*\' -baselineOnMigrate=false')
end
end
end
end