Sha256: 933e8b5bdfad7eadbf3778bc23a59842c2ebcad76f17e5ba17a320ae7afa9bf7

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 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.version >= Gem::Version.new('7.2.0')
          ActiveRecord::Base.connection_pool.migration_context.migrate(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.version >= Gem::Version.new('7.2.0')
          ActiveRecord::Base.connection_pool.migration_context.run(direction, 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.version >= Gem::Version.new('7.2.0')
          ActiveRecord::Base.connection_pool.migration_context.rollback(step)
        else
          ActiveRecord::Base.connection.migration_context.rollback(step)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ros-apartment-3.2.0 lib/apartment/migrator.rb
synerma-apartment-3.1.0 lib/apartment/migrator.rb