ros-apartment 2.3.0 → 2.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +78 -0
- data/.github/workflows/changelog.yml +63 -0
- data/.github/workflows/reviewdog.yml +22 -0
- data/.pryrc +5 -3
- data/.rubocop.yml +32 -0
- data/.rubocop_todo.yml +233 -0
- data/.ruby-version +1 -0
- data/.story_branch.yml +5 -0
- data/Appraisals +29 -42
- data/CHANGELOG.md +963 -0
- data/Gemfile +2 -7
- data/Guardfile +3 -16
- data/HISTORY.md +159 -68
- data/README.md +101 -21
- data/Rakefile +39 -22
- data/TODO.md +0 -1
- data/gemfiles/rails_5_2.gemfile +0 -5
- data/gemfiles/rails_6_0.gemfile +4 -9
- data/gemfiles/rails_6_1.gemfile +17 -0
- data/gemfiles/rails_7_0.gemfile +17 -0
- data/gemfiles/rails_master.gemfile +3 -8
- data/lib/apartment/active_record/connection_handling.rb +20 -0
- data/lib/apartment/active_record/internal_metadata.rb +9 -0
- data/lib/apartment/active_record/postgresql_adapter.rb +39 -0
- data/lib/apartment/active_record/schema_migration.rb +11 -0
- data/lib/apartment/adapters/abstract_adapter.rb +52 -46
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +5 -3
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +3 -3
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +19 -13
- data/lib/apartment/adapters/mysql2_adapter.rb +15 -9
- data/lib/apartment/adapters/postgis_adapter.rb +3 -2
- data/lib/apartment/adapters/postgresql_adapter.rb +79 -31
- data/lib/apartment/adapters/sqlite3_adapter.rb +18 -8
- data/lib/apartment/console.rb +23 -11
- data/lib/apartment/custom_console.rb +42 -0
- data/lib/apartment/deprecation.rb +2 -1
- data/lib/apartment/elevators/domain.rb +4 -3
- data/lib/apartment/elevators/first_subdomain.rb +3 -2
- data/lib/apartment/elevators/generic.rb +4 -3
- data/lib/apartment/elevators/host.rb +6 -1
- data/lib/apartment/elevators/host_hash.rb +6 -2
- data/lib/apartment/elevators/subdomain.rb +9 -5
- data/lib/apartment/log_subscriber.rb +33 -0
- data/lib/apartment/migrator.rb +4 -3
- data/lib/apartment/model.rb +29 -0
- data/lib/apartment/railtie.rb +16 -20
- data/lib/apartment/tasks/enhancements.rb +4 -6
- data/lib/apartment/tasks/task_helper.rb +52 -0
- data/lib/apartment/tenant.rb +7 -10
- data/lib/apartment/version.rb +3 -1
- data/lib/apartment.rb +56 -15
- data/lib/generators/apartment/install/install_generator.rb +4 -3
- data/lib/generators/apartment/install/templates/apartment.rb +10 -3
- data/lib/tasks/apartment.rake +48 -87
- data/ros-apartment.gemspec +59 -0
- metadata +148 -240
- data/.travis.yml +0 -65
- data/apartment.gemspec +0 -47
- data/gemfiles/rails_4_2.gemfile +0 -23
- data/gemfiles/rails_5_0.gemfile +0 -22
- data/gemfiles/rails_5_1.gemfile +0 -22
- data/lib/apartment/reloader.rb +0 -21
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -19
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -41
- data/spec/adapters/mysql2_adapter_spec.rb +0 -59
- data/spec/adapters/postgresql_adapter_spec.rb +0 -61
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -83
- data/spec/apartment_spec.rb +0 -11
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -6
- data/spec/dummy/app/helpers/application_helper.rb +0 -2
- data/spec/dummy/app/models/company.rb +0 -3
- data/spec/dummy/app/models/user.rb +0 -3
- data/spec/dummy/app/views/application/index.html.erb +0 -1
- data/spec/dummy/app/views/layouts/application.html.erb +0 -14
- data/spec/dummy/config/application.rb +0 -49
- data/spec/dummy/config/boot.rb +0 -11
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -5
- data/spec/dummy/config/environments/development.rb +0 -28
- data/spec/dummy/config/environments/production.rb +0 -51
- data/spec/dummy/config/environments/test.rb +0 -34
- data/spec/dummy/config/initializers/apartment.rb +0 -4
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy/config/initializers/inflections.rb +0 -10
- data/spec/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy/config/initializers/secret_token.rb +0 -7
- data/spec/dummy/config/initializers/session_store.rb +0 -8
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -3
- data/spec/dummy/config.ru +0 -4
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +0 -39
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +0 -14
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +0 -13
- data/spec/dummy/db/schema.rb +0 -55
- data/spec/dummy/db/seeds/import.rb +0 -5
- data/spec/dummy/db/seeds.rb +0 -5
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/public/404.html +0 -26
- data/spec/dummy/public/422.html +0 -26
- data/spec/dummy/public/500.html +0 -26
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +0 -6
- data/spec/dummy_engine/.gitignore +0 -8
- data/spec/dummy_engine/Gemfile +0 -15
- data/spec/dummy_engine/Rakefile +0 -34
- data/spec/dummy_engine/bin/rails +0 -12
- data/spec/dummy_engine/config/initializers/apartment.rb +0 -51
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -4
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -3
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -4
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -22
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -37
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -78
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -4
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -3
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -14
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -56
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/dummy_engine/test/dummy/config.ru +0 -4
- data/spec/examples/connection_adapter_examples.rb +0 -42
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -95
- data/spec/examples/generic_adapter_examples.rb +0 -163
- data/spec/examples/schema_adapter_examples.rb +0 -234
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -81
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -16
- data/spec/schemas/v2.rb +0 -43
- data/spec/schemas/v3.rb +0 -49
- data/spec/spec_helper.rb +0 -61
- data/spec/support/apartment_helpers.rb +0 -43
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -10
- data/spec/support/contexts.rb +0 -52
- data/spec/support/requirements.rb +0 -35
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -129
- data/spec/tenant_spec.rb +0 -190
- data/spec/unit/config_spec.rb +0 -112
- data/spec/unit/elevators/domain_spec.rb +0 -32
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -24
- data/spec/unit/elevators/generic_spec.rb +0 -54
- data/spec/unit/elevators/host_hash_spec.rb +0 -32
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -76
- data/spec/unit/migrator_spec.rb +0 -77
- data/spec/unit/reloader_spec.rb +0 -24
data/spec/support/contexts.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
# Some shared contexts for specs
|
2
|
-
|
3
|
-
shared_context "with default schema", :default_schema => true do
|
4
|
-
let(:default_schema){ Apartment::Test.next_db }
|
5
|
-
|
6
|
-
before do
|
7
|
-
Apartment::Test.create_schema(default_schema)
|
8
|
-
Apartment.default_schema = default_schema
|
9
|
-
end
|
10
|
-
|
11
|
-
after do
|
12
|
-
# resetting default_schema so we can drop and any further resets won't try to access droppped schema
|
13
|
-
Apartment.default_schema = nil
|
14
|
-
Apartment::Test.drop_schema(default_schema)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
# Some default setup for elevator specs
|
19
|
-
shared_context "elevators", elevator: true do
|
20
|
-
let(:company1) { mock_model(Company, database: db1).as_null_object }
|
21
|
-
let(:company2) { mock_model(Company, database: db2).as_null_object }
|
22
|
-
|
23
|
-
let(:api) { Apartment::Tenant }
|
24
|
-
|
25
|
-
before do
|
26
|
-
Apartment.reset # reset all config
|
27
|
-
Apartment.seed_after_create = false
|
28
|
-
Apartment.use_schemas = true
|
29
|
-
api.reload!(config)
|
30
|
-
api.create(db1)
|
31
|
-
api.create(db2)
|
32
|
-
end
|
33
|
-
|
34
|
-
after do
|
35
|
-
api.drop(db1)
|
36
|
-
api.drop(db2)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
shared_context "persistent_schemas", :persistent_schemas => true do
|
41
|
-
let(:persistent_schemas){ ['hstore', 'postgis'] }
|
42
|
-
|
43
|
-
before do
|
44
|
-
persistent_schemas.map{|schema| subject.create(schema) }
|
45
|
-
Apartment.persistent_schemas = persistent_schemas
|
46
|
-
end
|
47
|
-
|
48
|
-
after do
|
49
|
-
Apartment.persistent_schemas = []
|
50
|
-
persistent_schemas.map{|schema| subject.drop(schema) }
|
51
|
-
end
|
52
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Apartment
|
2
|
-
module Spec
|
3
|
-
|
4
|
-
#
|
5
|
-
# Define the interface methods required to
|
6
|
-
# use an adapter shared example
|
7
|
-
#
|
8
|
-
#
|
9
|
-
module AdapterRequirements
|
10
|
-
extend ActiveSupport::Concern
|
11
|
-
|
12
|
-
included do
|
13
|
-
before do
|
14
|
-
subject.create(db1)
|
15
|
-
subject.create(db2)
|
16
|
-
end
|
17
|
-
|
18
|
-
after do
|
19
|
-
# Reset before dropping (can't drop a db you're connected to)
|
20
|
-
subject.reset
|
21
|
-
|
22
|
-
# sometimes we manually drop these schemas in testing, don't care if we can't drop, hence rescue
|
23
|
-
subject.drop(db1) rescue true
|
24
|
-
subject.drop(db2) rescue true
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
%w{subject tenant_names default_tenant}.each do |method|
|
29
|
-
define_method method do
|
30
|
-
raise "You must define a `#{method}` method in your host group"
|
31
|
-
end unless defined?(method)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/spec/support/setup.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
module Apartment
|
2
|
-
module Spec
|
3
|
-
module Setup
|
4
|
-
|
5
|
-
def self.included(base)
|
6
|
-
base.instance_eval do
|
7
|
-
let(:db1){ Apartment::Test.next_db }
|
8
|
-
let(:db2){ Apartment::Test.next_db }
|
9
|
-
let(:connection){ ActiveRecord::Base.connection }
|
10
|
-
|
11
|
-
# This around ensures that we run these hooks before and after
|
12
|
-
# any before/after hooks defined in individual tests
|
13
|
-
# Otherwise these actually get run after test defined hooks
|
14
|
-
around(:each) do |example|
|
15
|
-
|
16
|
-
def config
|
17
|
-
db = RSpec.current_example.metadata.fetch(:database, :postgresql)
|
18
|
-
|
19
|
-
Apartment::Test.config['connections'][db.to_s].symbolize_keys
|
20
|
-
end
|
21
|
-
|
22
|
-
# before
|
23
|
-
Apartment::Tenant.reload!(config)
|
24
|
-
ActiveRecord::Base.establish_connection config
|
25
|
-
|
26
|
-
example.run
|
27
|
-
|
28
|
-
# after
|
29
|
-
Rails.configuration.database_configuration = {}
|
30
|
-
ActiveRecord::Base.clear_all_connections!
|
31
|
-
|
32
|
-
Apartment.excluded_models.each do |model|
|
33
|
-
klass = model.constantize
|
34
|
-
|
35
|
-
Apartment.connection_class.remove_connection(klass)
|
36
|
-
klass.clear_all_connections!
|
37
|
-
klass.reset_table_name
|
38
|
-
end
|
39
|
-
Apartment.reset
|
40
|
-
Apartment::Tenant.reload!
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,129 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'rake'
|
3
|
-
require 'apartment/migrator'
|
4
|
-
require 'apartment/tenant'
|
5
|
-
|
6
|
-
describe "apartment rake tasks" do
|
7
|
-
|
8
|
-
before do
|
9
|
-
@rake = Rake::Application.new
|
10
|
-
Rake.application = @rake
|
11
|
-
load 'tasks/apartment.rake'
|
12
|
-
# stub out rails tasks
|
13
|
-
Rake::Task.define_task('db:migrate')
|
14
|
-
Rake::Task.define_task('db:seed')
|
15
|
-
Rake::Task.define_task('db:rollback')
|
16
|
-
Rake::Task.define_task('db:migrate:up')
|
17
|
-
Rake::Task.define_task('db:migrate:down')
|
18
|
-
Rake::Task.define_task('db:migrate:redo')
|
19
|
-
end
|
20
|
-
|
21
|
-
after do
|
22
|
-
Rake.application = nil
|
23
|
-
ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
|
24
|
-
end
|
25
|
-
|
26
|
-
after(:all) do
|
27
|
-
Apartment::Test.load_schema
|
28
|
-
end
|
29
|
-
|
30
|
-
let(:version){ '1234' }
|
31
|
-
|
32
|
-
context 'database migration' do
|
33
|
-
|
34
|
-
let(:tenant_names){ 3.times.map{ Apartment::Test.next_db } }
|
35
|
-
let(:tenant_count){ tenant_names.length }
|
36
|
-
|
37
|
-
before do
|
38
|
-
allow(Apartment).to receive(:tenant_names).and_return tenant_names
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "apartment:migrate" do
|
42
|
-
before do
|
43
|
-
allow(ActiveRecord::Migrator).to receive(:migrate) # don't care about this
|
44
|
-
end
|
45
|
-
|
46
|
-
it "should migrate public and all multi-tenant dbs" do
|
47
|
-
expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times
|
48
|
-
@rake['apartment:migrate'].invoke
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "apartment:migrate:up" do
|
53
|
-
|
54
|
-
context "without a version" do
|
55
|
-
before do
|
56
|
-
ENV['VERSION'] = nil
|
57
|
-
end
|
58
|
-
|
59
|
-
it "requires a version to migrate to" do
|
60
|
-
expect{
|
61
|
-
@rake['apartment:migrate:up'].invoke
|
62
|
-
}.to raise_error("VERSION is required")
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "with version" do
|
67
|
-
|
68
|
-
before do
|
69
|
-
ENV['VERSION'] = version
|
70
|
-
end
|
71
|
-
|
72
|
-
it "migrates up to a specific version" do
|
73
|
-
expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
|
74
|
-
@rake['apartment:migrate:up'].invoke
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
describe "apartment:migrate:down" do
|
80
|
-
|
81
|
-
context "without a version" do
|
82
|
-
before do
|
83
|
-
ENV['VERSION'] = nil
|
84
|
-
end
|
85
|
-
|
86
|
-
it "requires a version to migrate to" do
|
87
|
-
expect{
|
88
|
-
@rake['apartment:migrate:down'].invoke
|
89
|
-
}.to raise_error("VERSION is required")
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
context "with version" do
|
94
|
-
|
95
|
-
before do
|
96
|
-
ENV['VERSION'] = version
|
97
|
-
end
|
98
|
-
|
99
|
-
it "migrates up to a specific version" do
|
100
|
-
expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
|
101
|
-
@rake['apartment:migrate:down'].invoke
|
102
|
-
end
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe "apartment:rollback" do
|
107
|
-
let(:step){ '3' }
|
108
|
-
|
109
|
-
it "should rollback dbs" do
|
110
|
-
expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
|
111
|
-
@rake['apartment:rollback'].invoke
|
112
|
-
end
|
113
|
-
|
114
|
-
it "should rollback dbs STEP amt" do
|
115
|
-
expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
|
116
|
-
ENV['STEP'] = step
|
117
|
-
@rake['apartment:rollback'].invoke
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
describe "apartment:drop" do
|
122
|
-
it "should migrate public and all multi-tenant dbs" do
|
123
|
-
expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
|
124
|
-
@rake['apartment:drop'].invoke
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
end
|
data/spec/tenant_spec.rb
DELETED
@@ -1,190 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Apartment::Tenant do
|
4
|
-
context "using mysql", database: :mysql do
|
5
|
-
|
6
|
-
before { subject.reload!(config) }
|
7
|
-
|
8
|
-
describe "#adapter" do
|
9
|
-
it "should load mysql adapter" do
|
10
|
-
subject.adapter
|
11
|
-
expect(Apartment::Adapters::Mysql2Adapter).to be_a(Class)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
# TODO this doesn't belong here, but there aren't integration tests currently for mysql
|
16
|
-
# where to put???
|
17
|
-
describe "exception recovery", :type => :request do
|
18
|
-
before do
|
19
|
-
subject.create db1
|
20
|
-
end
|
21
|
-
after{ subject.drop db1 }
|
22
|
-
|
23
|
-
# it "should recover from incorrect database" do
|
24
|
-
# session = Capybara::Session.new(:rack_test, Capybara.app)
|
25
|
-
# session.visit("http://#{db1}.com")
|
26
|
-
# expect {
|
27
|
-
# session.visit("http://this-database-should-not-exist.com")
|
28
|
-
# }.to raise_error
|
29
|
-
# session.visit("http://#{db1}.com")
|
30
|
-
# end
|
31
|
-
end
|
32
|
-
|
33
|
-
# TODO re-organize these tests
|
34
|
-
context "with prefix and schemas" do
|
35
|
-
describe "#create" do
|
36
|
-
before do
|
37
|
-
Apartment.configure do |config|
|
38
|
-
config.prepend_environment = true
|
39
|
-
config.use_schemas = true
|
40
|
-
end
|
41
|
-
|
42
|
-
subject.reload!(config)
|
43
|
-
end
|
44
|
-
|
45
|
-
after { subject.drop "db_with_prefix" rescue nil }
|
46
|
-
|
47
|
-
it "should create a new database" do
|
48
|
-
subject.create "db_with_prefix"
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "using postgresql", database: :postgresql do
|
55
|
-
before do
|
56
|
-
Apartment.use_schemas = true
|
57
|
-
subject.reload!(config)
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "#adapter" do
|
61
|
-
it "should load postgresql adapter" do
|
62
|
-
expect(subject.adapter).to be_a(Apartment::Adapters::PostgresqlSchemaAdapter)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "raises exception with invalid adapter specified" do
|
66
|
-
subject.reload!(config.merge(adapter: 'unknown'))
|
67
|
-
|
68
|
-
expect {
|
69
|
-
Apartment::Tenant.adapter
|
70
|
-
}.to raise_error(RuntimeError)
|
71
|
-
end
|
72
|
-
|
73
|
-
context "threadsafety" do
|
74
|
-
before { subject.create db1 }
|
75
|
-
after { subject.drop db1 }
|
76
|
-
|
77
|
-
it 'has a threadsafe adapter' do
|
78
|
-
subject.switch!(db1)
|
79
|
-
thread = Thread.new { expect(subject.current).to eq(Apartment.default_tenant) }
|
80
|
-
thread.join
|
81
|
-
expect(subject.current).to eq(db1)
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
# TODO above spec are also with use_schemas=true
|
87
|
-
context "with schemas" do
|
88
|
-
before do
|
89
|
-
Apartment.configure do |config|
|
90
|
-
config.excluded_models = []
|
91
|
-
config.use_schemas = true
|
92
|
-
config.seed_after_create = true
|
93
|
-
end
|
94
|
-
subject.create db1
|
95
|
-
end
|
96
|
-
|
97
|
-
after{ subject.drop db1 }
|
98
|
-
|
99
|
-
describe "#create" do
|
100
|
-
it "should seed data" do
|
101
|
-
subject.switch! db1
|
102
|
-
expect(User.count).to be > 0
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
describe "#switch!" do
|
107
|
-
|
108
|
-
let(:x){ rand(3) }
|
109
|
-
|
110
|
-
context "creating models" do
|
111
|
-
|
112
|
-
before{ subject.create db2 }
|
113
|
-
after{ subject.drop db2 }
|
114
|
-
|
115
|
-
it "should create a model instance in the current schema" do
|
116
|
-
subject.switch! db2
|
117
|
-
db2_count = User.count + x.times{ User.create }
|
118
|
-
|
119
|
-
subject.switch! db1
|
120
|
-
db_count = User.count + x.times{ User.create }
|
121
|
-
|
122
|
-
subject.switch! db2
|
123
|
-
expect(User.count).to eq(db2_count)
|
124
|
-
|
125
|
-
subject.switch! db1
|
126
|
-
expect(User.count).to eq(db_count)
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
context "with excluded models" do
|
131
|
-
|
132
|
-
before do
|
133
|
-
Apartment.configure do |config|
|
134
|
-
config.excluded_models = ["Company"]
|
135
|
-
end
|
136
|
-
subject.init
|
137
|
-
end
|
138
|
-
|
139
|
-
after do
|
140
|
-
# Apartment::Tenant.init creates per model connection.
|
141
|
-
# Remove the connection after testing not to unintentionally keep the connection across tests.
|
142
|
-
Apartment.excluded_models.each do |excluded_model|
|
143
|
-
excluded_model.constantize.remove_connection
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
it "should create excluded models in public schema" do
|
148
|
-
subject.reset # ensure we're on public schema
|
149
|
-
count = Company.count + x.times{ Company.create }
|
150
|
-
|
151
|
-
subject.switch! db1
|
152
|
-
x.times{ Company.create }
|
153
|
-
expect(Company.count).to eq(count + x)
|
154
|
-
subject.reset
|
155
|
-
expect(Company.count).to eq(count + x)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
context "seed paths" do
|
162
|
-
before do
|
163
|
-
Apartment.configure do |config|
|
164
|
-
config.excluded_models = []
|
165
|
-
config.use_schemas = true
|
166
|
-
config.seed_after_create = true
|
167
|
-
end
|
168
|
-
end
|
169
|
-
|
170
|
-
after{ subject.drop db1 }
|
171
|
-
|
172
|
-
it 'should seed from default path' do
|
173
|
-
subject.create db1
|
174
|
-
subject.switch! db1
|
175
|
-
expect(User.count).to eq(3)
|
176
|
-
expect(User.first.name).to eq('Some User 0')
|
177
|
-
end
|
178
|
-
|
179
|
-
it 'should seed from custom path' do
|
180
|
-
Apartment.configure do |config|
|
181
|
-
config.seed_data_file = "#{Rails.root}/db/seeds/import.rb"
|
182
|
-
end
|
183
|
-
subject.create db1
|
184
|
-
subject.switch! db1
|
185
|
-
expect(User.count).to eq(6)
|
186
|
-
expect(User.first.name).to eq('Different User 0')
|
187
|
-
end
|
188
|
-
end
|
189
|
-
end
|
190
|
-
end
|
data/spec/unit/config_spec.rb
DELETED
@@ -1,112 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Apartment do
|
4
|
-
|
5
|
-
describe "#config" do
|
6
|
-
|
7
|
-
let(:excluded_models){ ["Company"] }
|
8
|
-
let(:seed_data_file_path){ "#{Rails.root}/db/seeds/import.rb" }
|
9
|
-
|
10
|
-
def tenant_names_from_array(names)
|
11
|
-
names.each_with_object({}) do |tenant, hash|
|
12
|
-
hash[tenant] = Apartment.connection_config
|
13
|
-
end.with_indifferent_access
|
14
|
-
end
|
15
|
-
|
16
|
-
it "should yield the Apartment object" do
|
17
|
-
Apartment.configure do |config|
|
18
|
-
config.excluded_models = []
|
19
|
-
expect(config).to eq(Apartment)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
it "should set excluded models" do
|
24
|
-
Apartment.configure do |config|
|
25
|
-
config.excluded_models = excluded_models
|
26
|
-
end
|
27
|
-
expect(Apartment.excluded_models).to eq(excluded_models)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should set use_schemas" do
|
31
|
-
Apartment.configure do |config|
|
32
|
-
config.excluded_models = []
|
33
|
-
config.use_schemas = false
|
34
|
-
end
|
35
|
-
expect(Apartment.use_schemas).to be false
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should set seed_data_file" do
|
39
|
-
Apartment.configure do |config|
|
40
|
-
config.seed_data_file = seed_data_file_path
|
41
|
-
end
|
42
|
-
expect(Apartment.seed_data_file).to eq(seed_data_file_path)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "should set seed_after_create" do
|
46
|
-
Apartment.configure do |config|
|
47
|
-
config.excluded_models = []
|
48
|
-
config.seed_after_create = true
|
49
|
-
end
|
50
|
-
expect(Apartment.seed_after_create).to be true
|
51
|
-
end
|
52
|
-
|
53
|
-
context "databases" do
|
54
|
-
let(:users_conf_hash) { { port: 5444 } }
|
55
|
-
|
56
|
-
before do
|
57
|
-
Apartment.configure do |config|
|
58
|
-
config.tenant_names = tenant_names
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "tenant_names as string array" do
|
63
|
-
let(:tenant_names) { ['users', 'companies'] }
|
64
|
-
|
65
|
-
it "should return object if it doesnt respond_to call" do
|
66
|
-
expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names).keys)
|
67
|
-
end
|
68
|
-
|
69
|
-
it "should set tenants_with_config" do
|
70
|
-
expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names))
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
context "tenant_names as proc returning an array" do
|
75
|
-
let(:tenant_names) { lambda { ['users', 'companies'] } }
|
76
|
-
|
77
|
-
it "should return object if it doesnt respond_to call" do
|
78
|
-
expect(Apartment.tenant_names).to eq(tenant_names_from_array(tenant_names.call).keys)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "should set tenants_with_config" do
|
82
|
-
expect(Apartment.tenants_with_config).to eq(tenant_names_from_array(tenant_names.call))
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
context "tenant_names as Hash" do
|
87
|
-
let(:tenant_names) { { users: users_conf_hash }.with_indifferent_access }
|
88
|
-
|
89
|
-
it "should return object if it doesnt respond_to call" do
|
90
|
-
expect(Apartment.tenant_names).to eq(tenant_names.keys)
|
91
|
-
end
|
92
|
-
|
93
|
-
it "should set tenants_with_config" do
|
94
|
-
expect(Apartment.tenants_with_config).to eq(tenant_names)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
context "tenant_names as proc returning a Hash" do
|
99
|
-
let(:tenant_names) { lambda { { users: users_conf_hash }.with_indifferent_access } }
|
100
|
-
|
101
|
-
it "should return object if it doesnt respond_to call" do
|
102
|
-
expect(Apartment.tenant_names).to eq(tenant_names.call.keys)
|
103
|
-
end
|
104
|
-
|
105
|
-
it "should set tenants_with_config" do
|
106
|
-
expect(Apartment.tenants_with_config).to eq(tenant_names.call)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/elevators/domain'
|
3
|
-
|
4
|
-
describe Apartment::Elevators::Domain do
|
5
|
-
|
6
|
-
subject(:elevator){ described_class.new(Proc.new{}) }
|
7
|
-
|
8
|
-
describe "#parse_tenant_name" do
|
9
|
-
it "parses the host for a domain name" do
|
10
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'example.com')
|
11
|
-
expect(elevator.parse_tenant_name(request)).to eq('example')
|
12
|
-
end
|
13
|
-
|
14
|
-
it "ignores a www prefix and domain suffix" do
|
15
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => 'www.example.bc.ca')
|
16
|
-
expect(elevator.parse_tenant_name(request)).to eq('example')
|
17
|
-
end
|
18
|
-
|
19
|
-
it "returns nil if there is no host" do
|
20
|
-
request = ActionDispatch::Request.new('HTTP_HOST' => '')
|
21
|
-
expect(elevator.parse_tenant_name(request)).to be_nil
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "#call" do
|
26
|
-
it "switches to the proper tenant" do
|
27
|
-
expect(Apartment::Tenant).to receive(:switch).with('example')
|
28
|
-
|
29
|
-
elevator.call('HTTP_HOST' => 'www.example.com')
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/elevators/first_subdomain'
|
3
|
-
|
4
|
-
describe Apartment::Elevators::FirstSubdomain do
|
5
|
-
describe "subdomain" do
|
6
|
-
subject { described_class.new("test").parse_tenant_name(request) }
|
7
|
-
let(:request) { double(:request, :host => "#{subdomain}.example.com") }
|
8
|
-
|
9
|
-
context "one subdomain" do
|
10
|
-
let(:subdomain) { "test" }
|
11
|
-
it { is_expected.to eq("test") }
|
12
|
-
end
|
13
|
-
|
14
|
-
context "nested subdomains" do
|
15
|
-
let(:subdomain) { "test1.test2" }
|
16
|
-
it { is_expected.to eq("test1") }
|
17
|
-
end
|
18
|
-
|
19
|
-
context "no subdomain" do
|
20
|
-
let(:subdomain) { nil }
|
21
|
-
it { is_expected.to eq(nil) }
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'apartment/elevators/generic'
|
3
|
-
|
4
|
-
describe Apartment::Elevators::Generic do
|
5
|
-
|
6
|
-
class MyElevator < described_class
|
7
|
-
def parse_tenant_name(*)
|
8
|
-
'tenant2'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
subject(:elevator){ described_class.new(Proc.new{}) }
|
13
|
-
|
14
|
-
describe "#call" do
|
15
|
-
it "calls the processor if given" do
|
16
|
-
elevator = described_class.new(Proc.new{}, Proc.new{'tenant1'})
|
17
|
-
|
18
|
-
expect(Apartment::Tenant).to receive(:switch).with('tenant1')
|
19
|
-
|
20
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
21
|
-
end
|
22
|
-
|
23
|
-
it "raises if parse_tenant_name not implemented" do
|
24
|
-
expect {
|
25
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
26
|
-
}.to raise_error(RuntimeError)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "switches to the parsed db_name" do
|
30
|
-
elevator = MyElevator.new(Proc.new{})
|
31
|
-
|
32
|
-
expect(Apartment::Tenant).to receive(:switch).with('tenant2')
|
33
|
-
|
34
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
35
|
-
end
|
36
|
-
|
37
|
-
it "calls the block implementation of `switch`" do
|
38
|
-
elevator = MyElevator.new(Proc.new{}, Proc.new{'tenant2'})
|
39
|
-
|
40
|
-
expect(Apartment::Tenant).to receive(:switch).with('tenant2').and_yield
|
41
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
42
|
-
end
|
43
|
-
|
44
|
-
it "does not call `switch` if no database given" do
|
45
|
-
app = Proc.new{}
|
46
|
-
elevator = MyElevator.new(app, Proc.new{})
|
47
|
-
|
48
|
-
expect(Apartment::Tenant).not_to receive(:switch)
|
49
|
-
expect(app).to receive :call
|
50
|
-
|
51
|
-
elevator.call('HTTP_HOST' => 'foo.bar.com')
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|