From 028423c2f51ff738d151df32254913664ac8e898 Mon Sep 17 00:00:00 2001 From: Grzegorz Bizon Date: Mon, 5 Jun 2017 11:07:10 +0200 Subject: Calculate previous migration version in specs support This makes it possible to test migration on the schema this migration was written for, without a need to specify a previous schema version manually. --- spec/migrations/migrate_build_stage_reference_spec.rb | 2 +- spec/migrations/migrate_pipeline_stages_spec.rb | 2 +- spec/spec_helper.rb | 6 +++--- spec/support/migrations_helpers.rb | 10 ++++++++++ 4 files changed, 15 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/migrations/migrate_build_stage_reference_spec.rb b/spec/migrations/migrate_build_stage_reference_spec.rb index 979f13a1398..eaac8f95892 100644 --- a/spec/migrations/migrate_build_stage_reference_spec.rb +++ b/spec/migrations/migrate_build_stage_reference_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170526185921_migrate_build_stage_reference.rb') -describe MigrateBuildStageReference, :migration, schema: 20170526185602 do +describe MigrateBuildStageReference, :migration do ## # Create test data - pipeline and CI/CD jobs. # diff --git a/spec/migrations/migrate_pipeline_stages_spec.rb b/spec/migrations/migrate_pipeline_stages_spec.rb index c9b38086deb..36d3bd13ac0 100644 --- a/spec/migrations/migrate_pipeline_stages_spec.rb +++ b/spec/migrations/migrate_pipeline_stages_spec.rb @@ -1,7 +1,7 @@ require 'spec_helper' require Rails.root.join('db', 'post_migrate', '20170526185842_migrate_pipeline_stages.rb') -describe MigratePipelineStages, :migration, schema: 20170526185602 do +describe MigratePipelineStages, :migration do ## # Create test data - pipeline and CI/CD jobs. # diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6a0e29f2edb..5cbb3dafcdf 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -94,10 +94,10 @@ RSpec.configure do |config| Sidekiq.redis(&:flushall) end - config.around(:example, migration: true) do |example| + config.around(:example, :migration) do |example| begin - schema_version = example.metadata.fetch(:schema) - ActiveRecord::Migrator.migrate(migrations_paths, schema_version) + ActiveRecord::Migrator + .migrate(migrations_paths, previous_migration.version) example.run ensure diff --git a/spec/support/migrations_helpers.rb b/spec/support/migrations_helpers.rb index ee17d1a40b7..91fbb4eaf48 100644 --- a/spec/support/migrations_helpers.rb +++ b/spec/support/migrations_helpers.rb @@ -11,6 +11,16 @@ module MigrationsHelpers ActiveRecord::Base.connection.table_exists?(name) end + def migrations + ActiveRecord::Migrator.migrations(migrations_paths) + end + + def previous_migration + migrations.each_cons(2) do |previous, migration| + break previous if migration.name == described_class.name + end + end + def migrate! ActiveRecord::Migrator.up(migrations_paths) do |migration| migration.name == described_class.name -- cgit v1.2.1