Sha256: 302fe6e6510eb51602ada0326f944a52d5d3528c6ea5114e2f462295460a06cd

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

require 'apartment/tenant'

module Apartment
  module Migrator
    extend self

    # Migrate to latest
    def migrate(database)
      Tenant.switch(database) do
        version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil

        migration_scope_block = ->(migration) { ENV['SCOPE'].blank? || (ENV['SCOPE'] == migration.scope) }

        if activerecord_below_5_2?
          ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, version, &migration_scope_block)
        else
          ActiveRecord::Base.connection.migration_context.migrate(version, &migration_scope_block)
        end
      end
    end

    # Migrate up/down to a specific version
    def run(direction, database, version)
      Tenant.switch(database) do
        if activerecord_below_5_2?
          ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_paths, version)
        else
          ActiveRecord::Base.connection.migration_context.run(direction, version)
        end
      end
    end

    # rollback latest migration `step` number of times
    def rollback(database, step = 1)
      Tenant.switch(database) do
        if activerecord_below_5_2?
          ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
        else
          ActiveRecord::Base.connection.migration_context.rollback(step)
        end
      end
    end

    private

    def activerecord_below_5_2?
      ActiveRecord.version.release < Gem::Version.new('5.2.0')
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
puzzle-apartment-2.12.0 lib/apartment/migrator.rb
ros-apartment-2.11.0 lib/apartment/migrator.rb
ros-apartment-2.10.0 lib/apartment/migrator.rb
ros-apartment-2.9.0 lib/apartment/migrator.rb
ros-apartment-2.8.1 lib/apartment/migrator.rb
ros-apartment-2.8.1.rc2 lib/apartment/migrator.rb
ros-apartment-2.8.1.rc1 lib/apartment/migrator.rb
ros-apartment-2.8.0 lib/apartment/migrator.rb
ros-apartment-2.7.2 lib/apartment/migrator.rb
ros-apartment-2.7.1 lib/apartment/migrator.rb
ros-apartment-2.7.0 lib/apartment/migrator.rb
ros-apartment-2.6.1 lib/apartment/migrator.rb
ros-apartment-2.6.0 lib/apartment/migrator.rb
ros-apartment-2.5.0 lib/apartment/migrator.rb
ros-apartment-2.4.0 lib/apartment/migrator.rb