Class: ActiveRecord::Tenanted::DatabaseTasks
- Inherits:
-
Object
- Object
- ActiveRecord::Tenanted::DatabaseTasks
- Includes:
- Rake::DSL
- Defined in:
- lib/active_record/tenanted/database_tasks.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #drop_all ⇒ Object
- #drop_tenant(tenant = set_current_tenant) ⇒ Object
- #get_default_tenant ⇒ Object
-
#initialize(config) ⇒ DatabaseTasks
constructor
A new instance of DatabaseTasks.
-
#migrate(config) ⇒ Object
This is essentially a simplified implementation of ActiveRecord::Tasks::DatabaseTasks.migrate.
- #migrate_all ⇒ Object
- #migrate_tenant(tenant = set_current_tenant) ⇒ Object
- #register_rake_tasks ⇒ Object
- #set_current_tenant ⇒ Object
- #tenants ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize(config) ⇒ DatabaseTasks
Returns a new instance of DatabaseTasks.
18 19 20 21 22 23 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 18 def initialize(config) unless config.is_a?(ActiveRecord::Tenanted::DatabaseConfigurations::BaseConfig) raise TypeError, "Argument must be an instance of ActiveRecord::Tenanted::DatabaseConfigurations::BaseConfig" end @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
16 17 18 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 16 def config @config end |
Class Method Details
.verbose? ⇒ Boolean
11 12 13 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 11 def verbose? ActiveRecord::Tasks::DatabaseTasks.send(:verbose?) end |
Instance Method Details
#drop_all ⇒ Object
36 37 38 39 40 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 36 def drop_all tenants.each do |tenant| drop_tenant(tenant) end end |
#drop_tenant(tenant = set_current_tenant) ⇒ Object
42 43 44 45 46 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 42 def drop_tenant(tenant = set_current_tenant) db_config = config.new_tenant_config(tenant) db_config.config_adapter.drop_database $stdout.puts "Dropped database '#{db_config.database}'" if verbose? end |
#get_default_tenant ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 52 def get_default_tenant # TODO: needs to work with multiple tenanted configs, maybe using ENV["ARTENANT_#{config.name}"] tenant = ENV["ARTENANT"] if tenant.present? $stdout.puts "Setting current tenant to #{tenant.inspect}" if verbose? elsif Rails.env.local? tenant = Rails.application.config.active_record_tenanted.default_tenant $stdout.puts "Defaulting current tenant for #{config.name.inspect} to #{tenant.inspect}" if verbose? else tenant = nil $stdout.puts "Cannot determine an implicit tenant: ARTENANT not set, and Rails.env is not local." if verbose? end tenant end |
#migrate(config) ⇒ Object
This is essentially a simplified implementation of ActiveRecord::Tasks::DatabaseTasks.migrate
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 83 def migrate(config) ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection(config) do |conn| pool = conn.pool # initialize_database unless pool.schema_migration.table_exists? schema_dump_path = ActiveRecord::Tasks::DatabaseTasks.schema_dump_path(config) if schema_dump_path && File.exist?(schema_dump_path) ActiveRecord::Tasks::DatabaseTasks.load_schema(config) end # TODO: emit a "Created database" message once we sort out implicit creation end # migrate migrated = false if pool.migration_context.pending_migration_versions.present? pool.migration_context.migrate(nil) pool.schema_cache.clear! migrated = true end # dump the schema and schema cache if Rails.env.development? || ENV["ARTENANT_SCHEMA_DUMP"].present? if migrated ActiveRecord::Tasks::DatabaseTasks.dump_schema(config) end cache_dump = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config) if migrated || !File.exist?(cache_dump) ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(pool, cache_dump) end end end end |
#migrate_all ⇒ Object
25 26 27 28 29 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 25 def migrate_all tenants.each do |tenant| migrate_tenant(tenant) end end |
#migrate_tenant(tenant = set_current_tenant) ⇒ Object
31 32 33 34 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 31 def migrate_tenant(tenant = set_current_tenant) db_config = config.new_tenant_config(tenant) migrate(db_config) end |
#register_rake_tasks ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 122 def register_rake_tasks name = config.name desc "Migrate tenanted #{name} databases for current environment" task "db:migrate:#{name}" => "load_config" do verbose_was = ActiveRecord::Migration.verbose ActiveRecord::Migration.verbose = ActiveRecord::Tenanted::DatabaseTasks.verbose? tenant = ENV["ARTENANT"] if tenant.present? migrate_tenant(tenant) else migrate_all end ensure ActiveRecord::Migration.verbose = verbose_was end task "db:migrate" => "db:migrate:#{name}" task "db:prepare" => "db:migrate:#{name}" desc "Drop tenanted #{name} databases for current environment" task "db:drop:#{name}" => "load_config" do verbose_was = ActiveRecord::Migration.verbose ActiveRecord::Migration.verbose = ActiveRecord::Tenanted::DatabaseTasks.verbose? tenant = ENV["ARTENANT"] if tenant.present? drop_tenant(tenant) else drop_all end ensure ActiveRecord::Migration.verbose = verbose_was end task "db:drop" => "db:drop:#{name}" # TODO: Rails' database tasks include "db:seed" in the tasks that "db:reset" runs. desc "Drop and recreate tenanted #{name} database from its schema for the current environment" task "db:reset:#{name}" => [ "db:drop:#{name}", "db:migrate:#{name}" ] task "db:reset" => "db:reset:#{name}" end |
#set_current_tenant ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 69 def set_current_tenant unless (connection_class = ActiveRecord::Tenanted.connection_class) raise ActiveRecord::Tenanted::IntegrationNotConfiguredError, "ActiveRecord::Tenanted integration is not configured via connection_class" end if connection_class.current_tenant.nil? connection_class.current_tenant = get_default_tenant else connection_class.current_tenant end end |
#tenants ⇒ Object
48 49 50 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 48 def tenants config.tenants.presence || [ get_default_tenant ].compact end |
#verbose? ⇒ Boolean
118 119 120 |
# File 'lib/active_record/tenanted/database_tasks.rb', line 118 def verbose? self.class.verbose? end |