Sha256: 2de340713dd10619f8f534ff648ecb71c9c7b2f9c02985499649603cd9096343

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

require_relative '../matchers/migration_matchers'

require 'rspec'

module Spec
  module Example
    class MigrationExampleGroup < RSpec::Core::ExampleGroup
      include Spec::Matchers::Migration

      before(:all) do
        run_prereq_migrations if this_migration.adapter.supports_schema_transactions?
      end

      before(:each) do
        if this_migration.adapter.supports_schema_transactions?
          this_migration.adapter.begin_transaction
        else
          run_prereq_migrations
        end
      end

      after(:each) do
        this_migration.adapter.rollback_transaction if this_migration.adapter.supports_schema_transactions?
      end

      after(:all) do
        this_migration.adapter.recreate_database
      end

      def run_prereq_migrations
        # 'running n-1 migrations'
        all_databases.each do |db|
          db.adapter.recreate_database
        end
        @@migrations.sort.each do |migration|
          break if migration.name.to_s == migration_name.to_s

          migration.perform_up
        end
      end

      def run_migration
        this_migration.perform_up
      end

      def migration_name
        @migration_name ||= self.class.instance_variable_get('@description_text').to_s
      end

      def all_databases
        @@migrations.map(&:database).uniq
      end

      def this_migration
        @@migrations.select { |m| m.name.to_s == migration_name }.first
      end

      def select(sql)
        this_migration.adapter.select(sql)
      end

      def table(table_name)
        this_migration.adapter.table(table_name)
      end

      Spec::Example::MigrationExampleGroup.register
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sbf-dm-migrations-1.4.0.beta.1 lib/spec/example/migration_example_group.rb
sbf-dm-migrations-1.3.0 lib/spec/example/migration_example_group.rb
sbf-dm-migrations-1.3.0.beta lib/spec/example/migration_example_group.rb