Skip to content

Commit

Permalink
first step towards removing the word database
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrobertson committed Feb 21, 2014
1 parent 39813f9 commit 2d8c3e1
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 60 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,18 +246,18 @@ This object should yield an array of string representing each database name. Ex

```ruby
# Dynamically get database names to migrate
config.database_names = lambda{ Customer.pluck(:database_name) }
config.tenant_names = lambda{ Customer.pluck(:database_name) }

# Use a static list of database names for migrate
config.database_names = ['db1', 'db2']
config.tenant_names = ['db1', 'db2']
```

You can then migrate your databases using the rake task:

rake apartment:migrate

This basically invokes `Apartment::Database.migrate(#{db_name})` for each database name supplied
from `Apartment.database_names`
from `Apartment.tenant_names`

### Handling Environments

Expand Down
16 changes: 13 additions & 3 deletions lib/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class << self
extend Forwardable

ACCESSOR_METHODS = [:use_schemas, :seed_after_create, :prepend_environment, :append_environment]
WRITER_METHODS = [:database_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length]
WRITER_METHODS = [:tenant_names, :database_schema_file, :excluded_models, :default_schema, :persistent_schemas, :connection_class, :tld_length]

attr_accessor(*ACCESSOR_METHODS)
attr_writer(*WRITER_METHODS)
Expand All @@ -24,8 +24,8 @@ def configure
end

# Be careful not to use `return` here so both Proc and lambda can be used without breaking
def database_names
@database_names.respond_to?(:call) ? @database_names.call : @database_names
def tenant_names
@tenant_names.respond_to?(:call) ? @tenant_names.call : @tenant_names
end

# Default to empty array
Expand Down Expand Up @@ -60,6 +60,16 @@ def reset
(ACCESSOR_METHODS + WRITER_METHODS).each{|method| remove_instance_variable(:"@#{method}") if instance_variable_defined?(:"@#{method}") }
end

def database_names
warn "[Deprecation Warning] `database_names` is now deprecated, please use `tenant_names`"
tenant_names
end

def database_names=(names)
warn "[Deprecation Warning] `database_names=` is now deprecated, please use `tenant_names=`"
self.tenant_names=(names)
end

def use_postgres_schemas
warn "[Deprecation Warning] `use_postgresql_schemas` is now deprecated, please use `use_schemas`"
use_schemas
Expand Down
2 changes: 1 addition & 1 deletion lib/apartment/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Railtie < Rails::Railtie
Apartment.configure do |config|
config.excluded_models = []
config.use_schemas = true
config.database_names = []
config.tenant_names = []
config.seed_after_create = false
config.prepend_environment = false
config.append_environment = false
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/apartment/install/templates/apartment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# config.append_environment = true

# supply list of database names for migrations to run on
config.database_names = lambda{ ToDo_Tenant_Or_User_Model.pluck :database }
config.tenant_names = lambda{ ToDo_Tenant_Or_User_Model.pluck :database }
end

##
Expand Down
16 changes: 8 additions & 8 deletions lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apartment_namespace = namespace :apartment do

desc "Create all multi-tenant databases"
task create: 'db:migrate' do
database_names.each do |db|
tenants.each do |db|
begin
puts("Creating #{db} database")
quietly { Apartment::Database.create(db) }
Expand All @@ -16,7 +16,7 @@ apartment_namespace = namespace :apartment do

desc "Migrate all multi-tenant databases"
task :migrate do
database_names.each do |db|
tenants.each do |db|
begin
puts("Migrating #{db} database")
Apartment::Migrator.migrate db
Expand All @@ -28,7 +28,7 @@ apartment_namespace = namespace :apartment do

desc "Seed all multi-tenant databases"
task :seed do
database_names.each do |db|
tenants.each do |db|
begin
puts("Seeding #{db} database")
Apartment::Database.process(db) do
Expand All @@ -44,7 +44,7 @@ apartment_namespace = namespace :apartment do
task :rollback do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1

database_names.each do |db|
tenants.each do |db|
begin
puts("Rolling back #{db} database")
Apartment::Migrator.rollback db, step
Expand All @@ -60,7 +60,7 @@ apartment_namespace = namespace :apartment do
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required' unless version

database_names.each do |db|
tenants.each do |db|
begin
puts("Migrating #{db} database up")
Apartment::Migrator.run :up, db, version
Expand All @@ -75,7 +75,7 @@ apartment_namespace = namespace :apartment do
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
raise 'VERSION is required' unless version

database_names.each do |db|
tenants.each do |db|
begin
puts("Migrating #{db} database down")
Apartment::Migrator.run :down, db, version
Expand All @@ -97,7 +97,7 @@ apartment_namespace = namespace :apartment do
end
end

def database_names
ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.database_names
def tenants
ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names
end
end
4 changes: 1 addition & 3 deletions spec/adapters/jdbc_mysql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

subject { Apartment::Database.jdbc_mysql_adapter config.symbolize_keys }

def database_names
def tenant_names
ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row['schema_name'] }
end

let(:default_database) { subject.process { ActiveRecord::Base.connection.current_database } }

it_should_behave_like "a generic apartment adapter"
it_should_behave_like "a connection based apartment adapter"

end

end
8 changes: 4 additions & 4 deletions spec/adapters/jdbc_postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

before { Apartment.use_schemas = true }

# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
def database_names
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
def tenant_names
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] }
end

Expand All @@ -26,8 +26,8 @@ def database_names

before { Apartment.use_schemas = false }

# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
def database_names
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
def tenant_names
connection.execute("select datname from pg_database;").collect { |row| row['datname'] }
end

Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/mysql2_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

subject(:adapter){ Apartment::Database.mysql2_adapter config }

def database_names
def tenant_names
ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row[0] }
end

Expand Down
8 changes: 4 additions & 4 deletions spec/adapters/postgresql_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

before{ Apartment.use_schemas = true }

# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
def database_names
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
def tenant_names
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] }
end

Expand All @@ -25,8 +25,8 @@ def database_names

before{ Apartment.use_schemas = false }

# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
def database_names
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
def tenant_names
connection.execute("select datname from pg_database;").collect { |row| row['datname'] }
end

Expand Down
2 changes: 1 addition & 1 deletion spec/adapters/sqlite3_adapter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
subject{ Apartment::Database.sqlite3_adapter config }

context "using connections" do
def database_names
def tenant_names
db_dir = File.expand_path("../../dummy/db", __FILE__)
Dir.glob("#{db_dir}/*.sqlite3").map { |file| File.basename(file, '.sqlite3') }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/dummy/config/initializers/apartment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Apartment.configure do |config|
config.excluded_models = ["Company"]
config.database_names = lambda{ Company.pluck(:database) }
end
config.tenant_names = lambda{ Company.pluck(:database) }
end
8 changes: 4 additions & 4 deletions spec/examples/generic_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
describe "#create" do

it "should create the new databases" do
database_names.should include(db1)
database_names.should include(db2)
tenant_names.should include(db1)
tenant_names.should include(db2)
end

it "should load schema.rb to new schema" do
Expand Down Expand Up @@ -44,7 +44,7 @@
describe "#drop" do
it "should remove the db" do
subject.drop db1
database_names.should_not include(db1)
tenant_names.should_not include(db1)
end
end

Expand Down Expand Up @@ -104,4 +104,4 @@
subject.current.should == db1
end
end
end
end
4 changes: 2 additions & 2 deletions spec/examples/schema_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
expect {
subject.create(db)
}.to_not raise_error
database_names.should include(db.to_s)
tenant_names.should include(db.to_s)
end

after{ subject.drop(db) }
Expand All @@ -99,7 +99,7 @@
expect {
subject.drop(db)
}.to_not raise_error
database_names.should_not include(db.to_s)
tenant_names.should_not include(db.to_s)
end

after { subject.drop(db) rescue nil }
Expand Down
2 changes: 1 addition & 1 deletion spec/integration/apartment_rake_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Apartment.configure do |config|
config.use_schemas = true
config.excluded_models = ["Company"]
config.database_names = lambda{ Company.pluck(:database) }
config.tenant_names = lambda{ Company.pluck(:database) }
end
Apartment::Database.reload!(config)

Expand Down
4 changes: 2 additions & 2 deletions spec/integration/query_caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
before do
Apartment.configure do |config|
config.excluded_models = ["Company"]
config.database_names = lambda{ Company.pluck(:database) }
config.tenant_names = lambda{ Company.pluck(:database) }
config.use_schemas = true
end

Expand Down Expand Up @@ -38,4 +38,4 @@
Apartment::Database.switch db_names.last
User.find_by_name(db_names.first).should be_nil
end
end
end
4 changes: 2 additions & 2 deletions spec/support/requirements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ module AdapterRequirements
end
end

%w{subject database_names default_database}.each do |method|
%w{subject tenant_names default_database}.each do |method|
define_method method do
raise "You must define a `#{method}` method in your host group"
end unless defined?(method)
end

end
end
end
end
17 changes: 8 additions & 9 deletions spec/tasks/apartment_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

context 'database migration' do

let(:database_names){ 3.times.map{ Apartment::Test.next_db } }
let(:db_count){ database_names.length }
let(:tenant_names){ 3.times.map{ Apartment::Test.next_db } }
let(:tenant_count){ tenant_names.length }

before do
Apartment.stub(:database_names).and_return database_names
Apartment.stub(:tenant_names).and_return tenant_names
end

describe "apartment:migrate" do
Expand All @@ -43,7 +43,7 @@
end

it "should migrate public and all multi-tenant dbs" do
Apartment::Migrator.should_receive(:migrate).exactly(db_count).times
Apartment::Migrator.should_receive(:migrate).exactly(tenant_count).times
@rake['apartment:migrate'].invoke
end
end
Expand All @@ -69,7 +69,7 @@
end

it "migrates up to a specific version" do
Apartment::Migrator.should_receive(:run).with(:up, anything, version.to_i).exactly(db_count).times
Apartment::Migrator.should_receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
@rake['apartment:migrate:up'].invoke
end
end
Expand All @@ -96,23 +96,22 @@
end

it "migrates up to a specific version" do
Apartment::Migrator.should_receive(:run).with(:down, anything, version.to_i).exactly(db_count).times
Apartment::Migrator.should_receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
@rake['apartment:migrate:down'].invoke
end
end
end

describe "apartment:rollback" do

let(:step){ '3' }

it "should rollback dbs" do
Apartment::Migrator.should_receive(:rollback).exactly(db_count).times
Apartment::Migrator.should_receive(:rollback).exactly(tenant_count).times
@rake['apartment:rollback'].invoke
end

it "should rollback dbs STEP amt" do
Apartment::Migrator.should_receive(:rollback).with(anything, step.to_i).exactly(db_count).times
Apartment::Migrator.should_receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
ENV['STEP'] = step
@rake['apartment:rollback'].invoke
end
Expand Down
Loading

0 comments on commit 2d8c3e1

Please sign in to comment.