Class: MultiAR::MultiAR

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_ar.rb

Overview

Base of MultiAR gem.

Must be initialized before most actions works, that relies on MultiAR#app for getting configuration.

Constant Summary collapse

@@migration_dirs =

This classvariable is part of a private API. You should avoid using this classvariable if possible, as it may be removed or be changed in the future.

This will always be overridden, when MultiAR is initialized. Don’t try to do any funny logic with this.

[]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(databases:, environment: "development", config: "config/multi_ar.yaml", db_config: "config/database.yaml", migration_dirs: []) ⇒ MultiAR

TODO:

config file is overriding parameters passed here… I think it should be other way around, but need more custom logic for that :/

Returns a new instance of MultiAR.

Parameters:

  • databases

    array of available databases



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/multi_ar.rb', line 32

def initialize databases:, environment: "development", config: "config/multi_ar.yaml", db_config: "config/database.yaml", migration_dirs: []

  # first load config
  if not config.nil? and File.exist? config
    require "psych"
    config = Psych.load_file config
    b = binding
    config.each do |key, value|
      b.local_variable_set key.to_sym, value
    end
  end

  # then check that we have data in format we want it to be
  raise "#{db_config} is not valid path to a file. Try specifying --db-config <path> or configuring it in the configuration file." if db_config.nil? or !File.exist?(db_config)
  raise "databases is not responding to :each. Try passing passing --databases <database> or configuring it in the configuration file." unless databases.respond_to? :each

  @databases = databases
  @db_config = db_config
  @environment = environment
  @@migration_dirs = migration_dirs unless migration_dirs.empty? # This takes care of that it will only be overridden if there is any given values, making default configs work

  Database.initialize db_config: db_config

  ActiveRecord::Tasks::DatabaseTasks.class_eval { attr_accessor :sub_db_dir }
  ActiveRecord::Tasks::DatabaseTasks.sub_db_dir = databases.first # TODO: I don’t think this is how it should work

  @rake = ::Rake::Application.new
  ::Rake.application = @rake
  @rake.init
  ::Rake::TaskManager. = true

  Rake::Tasks.databases = databases
  Rake::Tasks.environment = environment
  Rake::Tasks.define

  MultiAR.app = self
end

Class Attribute Details

.appObject

Instance of MultiAR::MultiAR, automatically assigned by MultiAR::MultiAR#new. Used internally in the gem, to access configuration and other internal parts.



27
28
29
# File 'lib/multi_ar.rb', line 27

def app
  @app
end

Instance Attribute Details

#databasesObject (readonly)

Returns the value of attribute databases.



16
17
18
# File 'lib/multi_ar.rb', line 16

def databases
  @databases
end

#db_configObject (readonly)

Returns the value of attribute db_config.



17
18
19
# File 'lib/multi_ar.rb', line 17

def db_config
  @db_config
end

#environmentObject (readonly)

Returns the value of attribute environment.



18
19
20
# File 'lib/multi_ar.rb', line 18

def environment
  @environment
end

Class Method Details

.add_migration_dir(path) ⇒ Object

Note:

often you want to add full path to this dir, ‘__dir__` is useful for this.

Add a path to a directory where migrations resides. For standard Rails setup, this would be “db/migrate”.

The directory structure of how MultiAR uses the path is a bit different from traditional way: for each database, there is directory inside the migration dir.

For example, if project uses database named “messy_database” and migration dir is “my/migration/dir”, migrations would be looked from path “my/migration/dir/messy_database”.



85
86
87
88
# File 'lib/multi_ar.rb', line 85

def self.add_migration_dir path
  raise "Migration dir #{path} does not exist." unless Dir.exist? path
  @@migration_dirs << path
end

.migration_dirsObject

Array of paths to directories where migrations resides.

See Also:



72
73
74
# File 'lib/multi_ar.rb', line 72

def self.migration_dirs
  return @@migration_dirs
end

Instance Method Details

#list_tasks(all_rake_tasks: false) ⇒ Object



91
92
93
94
95
96
# File 'lib/multi_ar.rb', line 91

def list_tasks all_rake_tasks: false
  @rake.options.show_all_tasks = true if all_rake_tasks
  @rake.options.show_tasks = :tasks
  @rake.options.show_task_pattern = // # all tasks; we don’t have support for string-matching tasks
  @rake.display_tasks_and_comments
end

#rake_task(task_name) ⇒ Object

Invokes Rake task from ‘task_name`



99
100
101
# File 'lib/multi_ar.rb', line 99

def rake_task task_name
  @rake.invoke_task task_name
end