Sha256: b17437b6f424bcb6d8a2c62757ba1b82d1f2e6661b45fb6360319050d0436833

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module ActiveRecord
  class Migration
    class << self

      # Extend maintain_test_schema! to include migrations of the current wagon to test
      # or to make sure no wagon migrations are loaded when testing the main application.
      def maintain_test_schema_with_wagons!
        if Base.maintain_test_schema
          # set migrations paths to core only, wagon migrations are loaded separately
          Migrator.migrations_paths = Rails.application.paths['db/migrate'].to_a
          if app_needs_migration?
            suppress_messages { load_wagon_schema! }
          end
        end
      end
      alias_method_chain :maintain_test_schema!, :wagons

      private

      def load_wagon_schema!
        config = Base.configurations['test']
        # Contrary to the original rails approach (#load_schema_if_pending!),
        # purge the database first to get rid of all wagon tables.
        Tasks::DatabaseTasks.purge(config)

        Base.establish_connection(config)
        load_base_schema

        Wagons.current_wagon.prepare_test_db if Wagons.current_wagon
      end

      def load_base_schema
        if Tasks::DatabaseTasks.respond_to?(:load_schema_current)
          Tasks::DatabaseTasks.load_schema_current
        else
          Tasks::DatabaseTask.load_schema
        end
        check_pending!
      end

      def app_needs_migration?
        Wagons.current_wagon ||
        defined_app_migration_versions != migration_versions_in_db
      end

      def defined_app_migration_versions
        Migrator.migrations(Migrator.migrations_paths).collect(&:version).to_set
      end

      def migration_versions_in_db
        if Base.connection.table_exists?(SchemaMigration.table_name)
          Migrator.get_all_versions.to_set
        else
          [].to_set
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wagons-0.4.5 lib/wagons/extensions/migration.rb