ros-apartment 2.3.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE.md +21 -0
- data/.gitignore +15 -0
- data/.pryrc +3 -0
- data/.rspec +4 -0
- data/.travis.yml +65 -0
- data/Appraisals +71 -0
- data/Gemfile +10 -0
- data/Guardfile +24 -0
- data/HISTORY.md +398 -0
- data/README.md +576 -0
- data/Rakefile +128 -0
- data/TODO.md +51 -0
- data/apartment.gemspec +46 -0
- data/docker-compose.yml +33 -0
- data/gemfiles/rails_4_2.gemfile +23 -0
- data/gemfiles/rails_5_0.gemfile +22 -0
- data/gemfiles/rails_5_1.gemfile +22 -0
- data/gemfiles/rails_5_2.gemfile +18 -0
- data/gemfiles/rails_6_0.gemfile +22 -0
- data/gemfiles/rails_master.gemfile +22 -0
- data/lib/apartment.rb +118 -0
- data/lib/apartment/adapters/abstract_adapter.rb +269 -0
- data/lib/apartment/adapters/abstract_jdbc_adapter.rb +18 -0
- data/lib/apartment/adapters/jdbc_mysql_adapter.rb +19 -0
- data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +56 -0
- data/lib/apartment/adapters/mysql2_adapter.rb +71 -0
- data/lib/apartment/adapters/postgis_adapter.rb +12 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +236 -0
- data/lib/apartment/adapters/sqlite3_adapter.rb +56 -0
- data/lib/apartment/console.rb +12 -0
- data/lib/apartment/deprecation.rb +10 -0
- data/lib/apartment/elevators/domain.rb +22 -0
- data/lib/apartment/elevators/first_subdomain.rb +17 -0
- data/lib/apartment/elevators/generic.rb +32 -0
- data/lib/apartment/elevators/host.rb +30 -0
- data/lib/apartment/elevators/host_hash.rb +22 -0
- data/lib/apartment/elevators/subdomain.rb +62 -0
- data/lib/apartment/migrator.rb +51 -0
- data/lib/apartment/railtie.rb +67 -0
- data/lib/apartment/reloader.rb +21 -0
- data/lib/apartment/tasks/enhancements.rb +57 -0
- data/lib/apartment/tenant.rb +66 -0
- data/lib/apartment/version.rb +3 -0
- data/lib/generators/apartment/install/USAGE +5 -0
- data/lib/generators/apartment/install/install_generator.rb +10 -0
- data/lib/generators/apartment/install/templates/apartment.rb +109 -0
- data/lib/tasks/apartment.rake +145 -0
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +19 -0
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +41 -0
- data/spec/adapters/mysql2_adapter_spec.rb +59 -0
- data/spec/adapters/postgresql_adapter_spec.rb +61 -0
- data/spec/adapters/sqlite3_adapter_spec.rb +83 -0
- data/spec/apartment_spec.rb +11 -0
- data/spec/config/database.yml.sample +49 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +6 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/company.rb +3 -0
- data/spec/dummy/app/models/user.rb +3 -0
- data/spec/dummy/app/views/application/index.html.erb +1 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +49 -0
- data/spec/dummy/config/boot.rb +11 -0
- data/spec/dummy/config/database.yml.sample +44 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +28 -0
- data/spec/dummy/config/environments/production.rb +51 -0
- data/spec/dummy/config/environments/test.rb +34 -0
- data/spec/dummy/config/initializers/apartment.rb +4 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +39 -0
- data/spec/dummy/db/migrate/20111202022214_create_table_books.rb +14 -0
- data/spec/dummy/db/migrate/20180415260934_create_public_tokens.rb +13 -0
- data/spec/dummy/db/schema.rb +55 -0
- data/spec/dummy/db/seeds.rb +5 -0
- data/spec/dummy/db/seeds/import.rb +5 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy_engine/.gitignore +8 -0
- data/spec/dummy_engine/Gemfile +15 -0
- data/spec/dummy_engine/Rakefile +34 -0
- data/spec/dummy_engine/bin/rails +12 -0
- data/spec/dummy_engine/config/initializers/apartment.rb +51 -0
- data/spec/dummy_engine/dummy_engine.gemspec +24 -0
- data/spec/dummy_engine/lib/dummy_engine.rb +4 -0
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +4 -0
- data/spec/dummy_engine/lib/dummy_engine/version.rb +3 -0
- data/spec/dummy_engine/test/dummy/Rakefile +6 -0
- data/spec/dummy_engine/test/dummy/config.ru +4 -0
- data/spec/dummy_engine/test/dummy/config/application.rb +22 -0
- data/spec/dummy_engine/test/dummy/config/boot.rb +5 -0
- data/spec/dummy_engine/test/dummy/config/database.yml +25 -0
- data/spec/dummy_engine/test/dummy/config/environment.rb +5 -0
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +37 -0
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +78 -0
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +39 -0
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +8 -0
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +3 -0
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +23 -0
- data/spec/dummy_engine/test/dummy/config/routes.rb +56 -0
- data/spec/dummy_engine/test/dummy/config/secrets.yml +22 -0
- data/spec/examples/connection_adapter_examples.rb +42 -0
- data/spec/examples/generic_adapter_custom_configuration_example.rb +95 -0
- data/spec/examples/generic_adapter_examples.rb +163 -0
- data/spec/examples/schema_adapter_examples.rb +234 -0
- data/spec/integration/apartment_rake_integration_spec.rb +107 -0
- data/spec/integration/query_caching_spec.rb +81 -0
- data/spec/integration/use_within_an_engine_spec.rb +28 -0
- data/spec/schemas/v1.rb +16 -0
- data/spec/schemas/v2.rb +43 -0
- data/spec/schemas/v3.rb +49 -0
- data/spec/spec_helper.rb +61 -0
- data/spec/support/apartment_helpers.rb +43 -0
- data/spec/support/capybara_sessions.rb +15 -0
- data/spec/support/config.rb +10 -0
- data/spec/support/contexts.rb +52 -0
- data/spec/support/requirements.rb +35 -0
- data/spec/support/setup.rb +46 -0
- data/spec/tasks/apartment_rake_spec.rb +129 -0
- data/spec/tenant_spec.rb +190 -0
- data/spec/unit/config_spec.rb +112 -0
- data/spec/unit/elevators/domain_spec.rb +32 -0
- data/spec/unit/elevators/first_subdomain_spec.rb +24 -0
- data/spec/unit/elevators/generic_spec.rb +54 -0
- data/spec/unit/elevators/host_hash_spec.rb +32 -0
- data/spec/unit/elevators/host_spec.rb +89 -0
- data/spec/unit/elevators/subdomain_spec.rb +76 -0
- data/spec/unit/migrator_spec.rb +77 -0
- data/spec/unit/reloader_spec.rb +24 -0
- metadata +487 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'apartment/migrator'
|
2
|
+
require 'parallel'
|
3
|
+
|
4
|
+
apartment_namespace = namespace :apartment do
|
5
|
+
|
6
|
+
desc "Create all tenants"
|
7
|
+
task :create do
|
8
|
+
tenants.each do |tenant|
|
9
|
+
begin
|
10
|
+
puts("Creating #{tenant} tenant")
|
11
|
+
Apartment::Tenant.create(tenant)
|
12
|
+
rescue Apartment::TenantExists => e
|
13
|
+
puts e.message
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Drop all tenants"
|
19
|
+
task :drop do
|
20
|
+
tenants.each do |tenant|
|
21
|
+
begin
|
22
|
+
puts("Dropping #{tenant} tenant")
|
23
|
+
Apartment::Tenant.drop(tenant)
|
24
|
+
rescue Apartment::TenantNotFound => e
|
25
|
+
puts e.message
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
desc "Migrate all tenants"
|
31
|
+
task :migrate do
|
32
|
+
warn_if_tenants_empty
|
33
|
+
each_tenant do |tenant|
|
34
|
+
begin
|
35
|
+
puts("Migrating #{tenant} tenant")
|
36
|
+
Apartment::Migrator.migrate tenant
|
37
|
+
rescue Apartment::TenantNotFound => e
|
38
|
+
puts e.message
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
desc "Seed all tenants"
|
44
|
+
task :seed do
|
45
|
+
warn_if_tenants_empty
|
46
|
+
|
47
|
+
each_tenant do |tenant|
|
48
|
+
begin
|
49
|
+
puts("Seeding #{tenant} tenant")
|
50
|
+
Apartment::Tenant.switch(tenant) do
|
51
|
+
Apartment::Tenant.seed
|
52
|
+
end
|
53
|
+
rescue Apartment::TenantNotFound => e
|
54
|
+
puts e.message
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
desc "Rolls the migration back to the previous version (specify steps w/ STEP=n) across all tenants."
|
60
|
+
task :rollback do
|
61
|
+
warn_if_tenants_empty
|
62
|
+
|
63
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
64
|
+
|
65
|
+
each_tenant do |tenant|
|
66
|
+
begin
|
67
|
+
puts("Rolling back #{tenant} tenant")
|
68
|
+
Apartment::Migrator.rollback tenant, step
|
69
|
+
rescue Apartment::TenantNotFound => e
|
70
|
+
puts e.message
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
namespace :migrate do
|
76
|
+
desc 'Runs the "up" for a given migration VERSION across all tenants.'
|
77
|
+
task :up do
|
78
|
+
warn_if_tenants_empty
|
79
|
+
|
80
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
81
|
+
raise 'VERSION is required' unless version
|
82
|
+
|
83
|
+
each_tenant do |tenant|
|
84
|
+
begin
|
85
|
+
puts("Migrating #{tenant} tenant up")
|
86
|
+
Apartment::Migrator.run :up, tenant, version
|
87
|
+
rescue Apartment::TenantNotFound => e
|
88
|
+
puts e.message
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
desc 'Runs the "down" for a given migration VERSION across all tenants.'
|
94
|
+
task :down do
|
95
|
+
warn_if_tenants_empty
|
96
|
+
|
97
|
+
version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
|
98
|
+
raise 'VERSION is required' unless version
|
99
|
+
|
100
|
+
each_tenant do |tenant|
|
101
|
+
begin
|
102
|
+
puts("Migrating #{tenant} tenant down")
|
103
|
+
Apartment::Migrator.run :down, tenant, version
|
104
|
+
rescue Apartment::TenantNotFound => e
|
105
|
+
puts e.message
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
desc 'Rolls back the tenant one migration and re migrate up (options: STEP=x, VERSION=x).'
|
111
|
+
task :redo do
|
112
|
+
if ENV['VERSION']
|
113
|
+
apartment_namespace['migrate:down'].invoke
|
114
|
+
apartment_namespace['migrate:up'].invoke
|
115
|
+
else
|
116
|
+
apartment_namespace['rollback'].invoke
|
117
|
+
apartment_namespace['migrate'].invoke
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def each_tenant(&block)
|
123
|
+
Parallel.each(tenants, in_threads: Apartment.parallel_migration_threads) do |tenant|
|
124
|
+
block.call(tenant)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def tenants
|
129
|
+
ENV['DB'] ? ENV['DB'].split(',').map { |s| s.strip } : Apartment.tenant_names || []
|
130
|
+
end
|
131
|
+
|
132
|
+
def warn_if_tenants_empty
|
133
|
+
if tenants.empty? && ENV['IGNORE_EMPTY_TENANTS'] != "true"
|
134
|
+
puts <<-WARNING
|
135
|
+
[WARNING] - The list of tenants to migrate appears to be empty. This could mean a few things:
|
136
|
+
|
137
|
+
1. You may not have created any, in which case you can ignore this message
|
138
|
+
2. You've run `apartment:migrate` directly without loading the Rails environment
|
139
|
+
* `apartment:migrate` is now deprecated. Tenants will automatically be migrated with `db:migrate`
|
140
|
+
|
141
|
+
Note that your tenants currently haven't been migrated. You'll need to run `db:migrate` to rectify this.
|
142
|
+
WARNING
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
if defined?(JRUBY_VERSION)
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'apartment/adapters/jdbc_mysql_adapter'
|
5
|
+
|
6
|
+
describe Apartment::Adapters::JDBCMysqlAdapter, database: :mysql do
|
7
|
+
|
8
|
+
subject { Apartment::Tenant.jdbc_mysql_adapter config.symbolize_keys }
|
9
|
+
|
10
|
+
def tenant_names
|
11
|
+
ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row['schema_name'] }
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
15
|
+
|
16
|
+
it_should_behave_like "a generic apartment adapter"
|
17
|
+
it_should_behave_like "a connection based apartment adapter"
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
if defined?(JRUBY_VERSION)
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'apartment/adapters/jdbc_postgresql_adapter'
|
5
|
+
|
6
|
+
describe Apartment::Adapters::JDBCPostgresqlAdapter, database: :postgresql do
|
7
|
+
|
8
|
+
subject { Apartment::Tenant.jdbc_postgresql_adapter config.symbolize_keys }
|
9
|
+
|
10
|
+
context "using schemas" do
|
11
|
+
|
12
|
+
before { Apartment.use_schemas = true }
|
13
|
+
|
14
|
+
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
15
|
+
def tenant_names
|
16
|
+
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] }
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } }
|
20
|
+
|
21
|
+
it_should_behave_like "a generic apartment adapter"
|
22
|
+
it_should_behave_like "a schema based apartment adapter"
|
23
|
+
end
|
24
|
+
|
25
|
+
context "using databases" do
|
26
|
+
|
27
|
+
before { Apartment.use_schemas = false }
|
28
|
+
|
29
|
+
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
30
|
+
def tenant_names
|
31
|
+
connection.execute("select datname from pg_database;").collect { |row| row['datname'] }
|
32
|
+
end
|
33
|
+
|
34
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
35
|
+
|
36
|
+
it_should_behave_like "a generic apartment adapter"
|
37
|
+
it_should_behave_like "a connection based apartment adapter"
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'apartment/adapters/mysql2_adapter'
|
3
|
+
|
4
|
+
describe Apartment::Adapters::Mysql2Adapter, database: :mysql do
|
5
|
+
unless defined?(JRUBY_VERSION)
|
6
|
+
|
7
|
+
subject(:adapter){ Apartment::Tenant.mysql2_adapter config }
|
8
|
+
|
9
|
+
def tenant_names
|
10
|
+
ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect { |row| row[0] }
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
14
|
+
|
15
|
+
context "using - the equivalent of - schemas" do
|
16
|
+
before { Apartment.use_schemas = true }
|
17
|
+
|
18
|
+
it_should_behave_like "a generic apartment adapter"
|
19
|
+
|
20
|
+
describe "#default_tenant" do
|
21
|
+
it "is set to the original db from config" do
|
22
|
+
expect(subject.default_tenant).to eq(config[:database])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#init" do
|
27
|
+
include Apartment::Spec::AdapterRequirements
|
28
|
+
|
29
|
+
before do
|
30
|
+
Apartment.configure do |config|
|
31
|
+
config.excluded_models = ["Company"]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
after do
|
36
|
+
# Apartment::Tenant.init creates per model connection.
|
37
|
+
# Remove the connection after testing not to unintentionally keep the connection across tests.
|
38
|
+
Apartment.excluded_models.each do |excluded_model|
|
39
|
+
excluded_model.constantize.remove_connection
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should process model exclusions" do
|
44
|
+
Apartment::Tenant.init
|
45
|
+
|
46
|
+
expect(Company.table_name).to eq("#{default_tenant}.companies")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "using connections" do
|
52
|
+
before { Apartment.use_schemas = false }
|
53
|
+
|
54
|
+
it_should_behave_like "a generic apartment adapter"
|
55
|
+
it_should_behave_like "a generic apartment adapter able to handle custom configuration"
|
56
|
+
it_should_behave_like "a connection based apartment adapter"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'apartment/adapters/postgresql_adapter'
|
3
|
+
|
4
|
+
describe Apartment::Adapters::PostgresqlAdapter, database: :postgresql do
|
5
|
+
unless defined?(JRUBY_VERSION)
|
6
|
+
|
7
|
+
subject{ Apartment::Tenant.postgresql_adapter config }
|
8
|
+
|
9
|
+
context "using schemas with schema.rb" do
|
10
|
+
|
11
|
+
before{ Apartment.use_schemas = true }
|
12
|
+
|
13
|
+
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
14
|
+
def tenant_names
|
15
|
+
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] }
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } }
|
19
|
+
|
20
|
+
it_should_behave_like "a generic apartment adapter"
|
21
|
+
it_should_behave_like "a schema based apartment adapter"
|
22
|
+
end
|
23
|
+
|
24
|
+
context "using schemas with SQL dump" do
|
25
|
+
|
26
|
+
before{ Apartment.use_schemas = true; Apartment.use_sql = true }
|
27
|
+
|
28
|
+
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
29
|
+
def tenant_names
|
30
|
+
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect { |row| row['nspname'] }
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.schema_search_path.gsub('"', '') } }
|
34
|
+
|
35
|
+
it_should_behave_like "a generic apartment adapter"
|
36
|
+
it_should_behave_like "a schema based apartment adapter"
|
37
|
+
|
38
|
+
it 'allows for dashes in the schema name' do
|
39
|
+
expect { Apartment::Tenant.create('has-dashes') }.to_not raise_error
|
40
|
+
end
|
41
|
+
|
42
|
+
after { Apartment::Tenant.drop('has-dashes') if Apartment.connection.schema_exists? 'has-dashes' }
|
43
|
+
end
|
44
|
+
|
45
|
+
context "using connections" do
|
46
|
+
|
47
|
+
before{ Apartment.use_schemas = false }
|
48
|
+
|
49
|
+
# Not sure why, but somehow using let(:tenant_names) memoizes for the whole example group, not just each test
|
50
|
+
def tenant_names
|
51
|
+
connection.execute("select datname from pg_database;").collect { |row| row['datname'] }
|
52
|
+
end
|
53
|
+
|
54
|
+
let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } }
|
55
|
+
|
56
|
+
it_should_behave_like "a generic apartment adapter"
|
57
|
+
it_should_behave_like "a generic apartment adapter able to handle custom configuration"
|
58
|
+
it_should_behave_like "a connection based apartment adapter"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'apartment/adapters/sqlite3_adapter'
|
3
|
+
|
4
|
+
describe Apartment::Adapters::Sqlite3Adapter, database: :sqlite do
|
5
|
+
unless defined?(JRUBY_VERSION)
|
6
|
+
|
7
|
+
subject{ Apartment::Tenant.sqlite3_adapter config }
|
8
|
+
|
9
|
+
context "using connections" do
|
10
|
+
def tenant_names
|
11
|
+
db_dir = File.expand_path("../../dummy/db", __FILE__)
|
12
|
+
Dir.glob("#{db_dir}/*.sqlite3").map { |file| File.basename(file, '.sqlite3') }
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:default_tenant) do
|
16
|
+
subject.switch { File.basename(Apartment::Test.config['connections']['sqlite']['database'], '.sqlite3') }
|
17
|
+
end
|
18
|
+
|
19
|
+
it_should_behave_like "a generic apartment adapter"
|
20
|
+
it_should_behave_like "a connection based apartment adapter"
|
21
|
+
|
22
|
+
after(:all) do
|
23
|
+
File.delete(Apartment::Test.config['connections']['sqlite']['database'])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "with prepend and append" do
|
28
|
+
let(:default_dir) { File.expand_path(File.dirname(config[:database])) }
|
29
|
+
describe "#prepend" do
|
30
|
+
let (:db_name) { "db_with_prefix" }
|
31
|
+
before do
|
32
|
+
Apartment.configure do |config|
|
33
|
+
config.prepend_environment = true
|
34
|
+
config.append_environment = false
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
after { subject.drop db_name rescue nil }
|
39
|
+
|
40
|
+
it "should create a new database" do
|
41
|
+
subject.create db_name
|
42
|
+
|
43
|
+
expect(File.exists?("#{default_dir}/#{Rails.env}_#{db_name}.sqlite3")).to eq true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#neither" do
|
48
|
+
let (:db_name) { "db_without_prefix_suffix" }
|
49
|
+
before do
|
50
|
+
Apartment.configure { |config| config.prepend_environment = config.append_environment = false }
|
51
|
+
end
|
52
|
+
|
53
|
+
after { subject.drop db_name rescue nil }
|
54
|
+
|
55
|
+
it "should create a new database" do
|
56
|
+
subject.create db_name
|
57
|
+
|
58
|
+
expect(File.exists?("#{default_dir}/#{db_name}.sqlite3")).to eq true
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "#append" do
|
63
|
+
let (:db_name) { "db_with_suffix" }
|
64
|
+
before do
|
65
|
+
Apartment.configure do |config|
|
66
|
+
config.prepend_environment = false
|
67
|
+
config.append_environment = true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
after { subject.drop db_name rescue nil }
|
72
|
+
|
73
|
+
it "should create a new database" do
|
74
|
+
subject.create db_name
|
75
|
+
|
76
|
+
expect(File.exists?("#{default_dir}/#{db_name}_#{Rails.env}.sqlite3")).to eq true
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
<% if defined?(JRUBY_VERSION) %>
|
2
|
+
connections:
|
3
|
+
postgresql:
|
4
|
+
adapter: postgresql
|
5
|
+
database: apartment_postgresql_test
|
6
|
+
username: postgres
|
7
|
+
min_messages: WARNING
|
8
|
+
driver: org.postgresql.Driver
|
9
|
+
url: jdbc:postgresql://localhost:5432/apartment_postgresql_test
|
10
|
+
timeout: 5000
|
11
|
+
pool: 5
|
12
|
+
host: localhost
|
13
|
+
port: 5432
|
14
|
+
|
15
|
+
mysql:
|
16
|
+
adapter: mysql
|
17
|
+
database: apartment_mysql_test
|
18
|
+
username: root
|
19
|
+
min_messages: WARNING
|
20
|
+
driver: com.mysql.jdbc.Driver
|
21
|
+
url: jdbc:mysql://localhost:3306/apartment_mysql_test
|
22
|
+
timeout: 5000
|
23
|
+
pool: 5
|
24
|
+
host: 127.0.0.1
|
25
|
+
port: 3306
|
26
|
+
<% else %>
|
27
|
+
connections:
|
28
|
+
postgresql:
|
29
|
+
adapter: postgresql
|
30
|
+
database: apartment_postgresql_test
|
31
|
+
min_messages: WARNING
|
32
|
+
username: postgres
|
33
|
+
schema_search_path: public
|
34
|
+
password:
|
35
|
+
host: localhost
|
36
|
+
port: 5432
|
37
|
+
|
38
|
+
mysql:
|
39
|
+
adapter: mysql2
|
40
|
+
database: apartment_mysql_test
|
41
|
+
username: root
|
42
|
+
password:
|
43
|
+
host: 127.0.0.1
|
44
|
+
port: 3306
|
45
|
+
|
46
|
+
sqlite:
|
47
|
+
adapter: sqlite3
|
48
|
+
database: <%= File.expand_path('../spec/dummy/db', __FILE__) %>/default.sqlite3
|
49
|
+
<% end %>
|