diff --git a/Rakefile b/Rakefile index 8ac5a58..006b731 100644 --- a/Rakefile +++ b/Rakefile @@ -2,4 +2,5 @@ require 'rubygems' require 'puppetlabs_spec_helper/rake_tasks' require 'puppet-lint/tasks/puppet-lint' -PuppetLint.configuration.send('disable_80chars') \ No newline at end of file +PuppetLint.configuration.send('disable_80chars') +PuppetLint.configuration.send('disable_140chars') diff --git a/manifests/flyway.pp b/manifests/flyway.pp index b28ae45..a81d289 100644 --- a/manifests/flyway.pp +++ b/manifests/flyway.pp @@ -34,7 +34,7 @@ include ::java Class['::java'] -> Database_schema::Flyway_migration<||> } - + archive { "flyway-commandline-${version}": ensure => $ensure, url => $real_source, @@ -42,6 +42,6 @@ root_dir => "flyway-${version}", checksum => false } - + Class['database_schema::flyway'] -> Database_schema::Flyway_migration<||> -} \ No newline at end of file +} diff --git a/manifests/flyway_migration.pp b/manifests/flyway_migration.pp index a9d35e0..2a2c78e 100644 --- a/manifests/flyway_migration.pp +++ b/manifests/flyway_migration.pp @@ -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, @@ -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}" @@ -39,13 +44,20 @@ 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 } @@ -53,7 +65,7 @@ $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", diff --git a/manifests/liquibase.pp b/manifests/liquibase.pp index 6efe51b..088f432 100644 --- a/manifests/liquibase.pp +++ b/manifests/liquibase.pp @@ -30,22 +30,22 @@ 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, @@ -53,6 +53,6 @@ root_dir => 'liquibase', checksum => false } - + Class['database_schema::liquibase'] -> Database_schema::Liquibase_migration<||> -} \ No newline at end of file +} diff --git a/manifests/liquibase_migration.pp b/manifests/liquibase_migration.pp index 4d6805f..3f6b99b 100644 --- a/manifests/liquibase_migration.pp +++ b/manifests/liquibase_migration.pp @@ -40,16 +40,16 @@ 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", @@ -57,4 +57,4 @@ command => "${flyway_command} update", require => File[$changelog_path] } -} \ No newline at end of file +} diff --git a/spec/defines/flyway_migration_spec.rb b/spec/defines/flyway_migration_spec.rb index 68937e9..0f6fcd4 100644 --- a/spec/defines/flyway_migration_spec.rb +++ b/spec/defines/flyway_migration_spec.rb @@ -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