Sha256: e5f1a7a21d5e5d5f59460ec5ffa7f104434e04a78a394eb89f880c9a22d38b4d

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

module FourthBase

  mattr_accessor :is_on_base, instance_accessor: false
  self.is_on_base = false

  def self.on_base
    already_on_base = is_on_base
    if already_on_base
      yield
      return
    end
    original_config = ActiveRecord::Tasks::DatabaseTasks.current_config
    original_configurations = ActiveRecord::Base.configurations
    original_migrations_path = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
    original_db_dir = ActiveRecord::Tasks::DatabaseTasks.db_dir
    ActiveRecord::Tasks::DatabaseTasks.current_config = config
    ActiveRecord::Base.configurations = original_configurations[Railtie.config_key]
    ActiveRecord::Base.establish_connection(config)
    ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [FourthBase::Railtie.fullpath('migrate')]
    ActiveRecord::Tasks::DatabaseTasks.db_dir = FourthBase::Railtie.fullpath
    ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
    self.is_on_base = true
    yield
  ensure
    unless already_on_base
      ActiveRecord::Base.configurations = original_configurations
      ActiveRecord::Tasks::DatabaseTasks.migrations_paths = original_migrations_path
      ActiveRecord::Tasks::DatabaseTasks.db_dir = original_db_dir
      ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
      ActiveRecord::Tasks::DatabaseTasks.current_config = original_config
      ActiveRecord::Base.establish_connection(original_config)
      self.is_on_base = false
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fourthbase-2.1.2 lib/fourth_base/on_base.rb