ros-apartment 2.4.0 → 2.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -0
- data/.travis.yml +8 -30
- data/Appraisals +4 -17
- data/Gemfile +1 -1
- data/Guardfile +3 -1
- data/README.md +15 -14
- data/Rakefile +34 -22
- data/apartment.gemspec +11 -3
- data/gemfiles/rails_5_0.gemfile +10 -11
- data/gemfiles/rails_5_1.gemfile +10 -11
- data/gemfiles/rails_5_2.gemfile +9 -10
- data/gemfiles/rails_6_0.gemfile +10 -11
- data/gemfiles/rails_master.gemfile +10 -11
- data/lib/apartment/active_record/connection_handling.rb +17 -0
- data/lib/apartment/active_record/internal_metadata.rb +11 -0
- data/lib/apartment/active_record/schema_migration.rb +13 -0
- data/lib/apartment/adapters/abstract_adapter.rb +3 -2
- data/lib/apartment/model.rb +27 -0
- data/lib/apartment/tenant.rb +3 -1
- data/lib/apartment/version.rb +1 -1
- data/lib/apartment.rb +22 -3
- metadata +11 -209
- data/spec/adapters/jdbc_mysql_adapter_spec.rb +0 -20
- data/spec/adapters/jdbc_postgresql_adapter_spec.rb +0 -39
- data/spec/adapters/mysql2_adapter_spec.rb +0 -61
- data/spec/adapters/postgresql_adapter_spec.rb +0 -63
- data/spec/adapters/sqlite3_adapter_spec.rb +0 -101
- data/spec/apartment_spec.rb +0 -13
- data/spec/config/database.yml.sample +0 -49
- data/spec/dummy/Rakefile +0 -7
- data/spec/dummy/app/controllers/application_controller.rb +0 -7
- data/spec/dummy/app/helpers/application_helper.rb +0 -4
- data/spec/dummy/app/models/application_record.rb +0 -6
- data/spec/dummy/app/models/company.rb +0 -5
- data/spec/dummy/app/models/user.rb +0 -5
- 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 -51
- data/spec/dummy/config/boot.rb +0 -13
- data/spec/dummy/config/database.yml.sample +0 -44
- data/spec/dummy/config/environment.rb +0 -7
- data/spec/dummy/config/environments/development.rb +0 -29
- data/spec/dummy/config/environments/production.rb +0 -53
- data/spec/dummy/config/environments/test.rb +0 -36
- data/spec/dummy/config/initializers/apartment.rb +0 -6
- data/spec/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy/config/initializers/inflections.rb +0 -11
- data/spec/dummy/config/initializers/mime_types.rb +0 -6
- data/spec/dummy/config/initializers/secret_token.rb +0 -9
- data/spec/dummy/config/initializers/session_store.rb +0 -10
- data/spec/dummy/config/locales/en.yml +0 -5
- data/spec/dummy/config/routes.rb +0 -5
- data/spec/dummy/config.ru +0 -6
- 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 -7
- 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 -8
- 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 -52
- data/spec/dummy_engine/dummy_engine.gemspec +0 -24
- data/spec/dummy_engine/lib/dummy_engine/engine.rb +0 -6
- data/spec/dummy_engine/lib/dummy_engine/version.rb +0 -5
- data/spec/dummy_engine/lib/dummy_engine.rb +0 -6
- data/spec/dummy_engine/test/dummy/Rakefile +0 -6
- data/spec/dummy_engine/test/dummy/config/application.rb +0 -24
- data/spec/dummy_engine/test/dummy/config/boot.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/database.yml +0 -25
- data/spec/dummy_engine/test/dummy/config/environment.rb +0 -7
- data/spec/dummy_engine/test/dummy/config/environments/development.rb +0 -39
- data/spec/dummy_engine/test/dummy/config/environments/production.rb +0 -80
- data/spec/dummy_engine/test/dummy/config/environments/test.rb +0 -41
- data/spec/dummy_engine/test/dummy/config/initializers/assets.rb +0 -10
- data/spec/dummy_engine/test/dummy/config/initializers/backtrace_silencers.rb +0 -8
- data/spec/dummy_engine/test/dummy/config/initializers/cookies_serializer.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/filter_parameter_logging.rb +0 -6
- data/spec/dummy_engine/test/dummy/config/initializers/inflections.rb +0 -17
- data/spec/dummy_engine/test/dummy/config/initializers/mime_types.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/session_store.rb +0 -5
- data/spec/dummy_engine/test/dummy/config/initializers/wrap_parameters.rb +0 -16
- data/spec/dummy_engine/test/dummy/config/locales/en.yml +0 -23
- data/spec/dummy_engine/test/dummy/config/routes.rb +0 -58
- data/spec/dummy_engine/test/dummy/config/secrets.yml +0 -22
- data/spec/dummy_engine/test/dummy/config.ru +0 -6
- data/spec/examples/connection_adapter_examples.rb +0 -44
- data/spec/examples/generic_adapter_custom_configuration_example.rb +0 -93
- data/spec/examples/generic_adapter_examples.rb +0 -164
- data/spec/examples/schema_adapter_examples.rb +0 -239
- data/spec/integration/apartment_rake_integration_spec.rb +0 -107
- data/spec/integration/query_caching_spec.rb +0 -83
- data/spec/integration/use_within_an_engine_spec.rb +0 -28
- data/spec/schemas/v1.rb +0 -15
- data/spec/schemas/v2.rb +0 -41
- data/spec/schemas/v3.rb +0 -47
- data/spec/spec_helper.rb +0 -63
- data/spec/support/apartment_helpers.rb +0 -47
- data/spec/support/capybara_sessions.rb +0 -15
- data/spec/support/config.rb +0 -13
- data/spec/support/contexts.rb +0 -54
- data/spec/support/requirements.rb +0 -48
- data/spec/support/setup.rb +0 -46
- data/spec/tasks/apartment_rake_spec.rb +0 -124
- data/spec/tenant_spec.rb +0 -194
- data/spec/unit/config_spec.rb +0 -111
- data/spec/unit/elevators/domain_spec.rb +0 -33
- data/spec/unit/elevators/first_subdomain_spec.rb +0 -26
- data/spec/unit/elevators/generic_spec.rb +0 -55
- data/spec/unit/elevators/host_hash_spec.rb +0 -33
- data/spec/unit/elevators/host_spec.rb +0 -89
- data/spec/unit/elevators/subdomain_spec.rb +0 -77
- data/spec/unit/migrator_spec.rb +0 -78
- data/spec/unit/reloader_spec.rb +0 -24
@@ -1,83 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
|
5
|
-
describe 'query caching' do
|
6
|
-
describe 'when use_schemas = true' do
|
7
|
-
let(:db_names) { [db1, db2] }
|
8
|
-
|
9
|
-
before do
|
10
|
-
Apartment.configure do |config|
|
11
|
-
config.excluded_models = ['Company']
|
12
|
-
config.tenant_names = -> { Company.pluck(:database) }
|
13
|
-
config.use_schemas = true
|
14
|
-
end
|
15
|
-
|
16
|
-
Apartment::Tenant.reload!(config)
|
17
|
-
|
18
|
-
db_names.each do |db_name|
|
19
|
-
Apartment::Tenant.create(db_name)
|
20
|
-
Company.create database: db_name
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
after do
|
25
|
-
db_names.each { |db| Apartment::Tenant.drop(db) }
|
26
|
-
Apartment::Tenant.reset
|
27
|
-
Company.delete_all
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'clears the ActiveRecord::QueryCache after switching databases' do
|
31
|
-
db_names.each do |db_name|
|
32
|
-
Apartment::Tenant.switch! db_name
|
33
|
-
User.create! name: db_name
|
34
|
-
end
|
35
|
-
|
36
|
-
ActiveRecord::Base.connection.enable_query_cache!
|
37
|
-
|
38
|
-
Apartment::Tenant.switch! db_names.first
|
39
|
-
expect(User.find_by(name: db_names.first).name).to eq(db_names.first)
|
40
|
-
|
41
|
-
Apartment::Tenant.switch! db_names.last
|
42
|
-
expect(User.find_by(name: db_names.first)).to be_nil
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
describe 'when use_schemas = false' do
|
47
|
-
let(:db_name) { db1 }
|
48
|
-
|
49
|
-
before do
|
50
|
-
Apartment.configure do |config|
|
51
|
-
config.excluded_models = ['Company']
|
52
|
-
config.tenant_names = -> { Company.pluck(:database) }
|
53
|
-
config.use_schemas = false
|
54
|
-
end
|
55
|
-
|
56
|
-
Apartment::Tenant.reload!(config)
|
57
|
-
|
58
|
-
Apartment::Tenant.create(db_name)
|
59
|
-
Company.create database: db_name
|
60
|
-
end
|
61
|
-
|
62
|
-
after do
|
63
|
-
# Avoid cannot drop the currently open database. Maybe there is a better way to handle this.
|
64
|
-
Apartment::Tenant.switch! 'template1'
|
65
|
-
|
66
|
-
Apartment::Tenant.drop(db_name)
|
67
|
-
Apartment::Tenant.reset
|
68
|
-
Company.delete_all
|
69
|
-
end
|
70
|
-
|
71
|
-
it 'configuration value is kept after switching databases' do
|
72
|
-
ActiveRecord::Base.connection.enable_query_cache!
|
73
|
-
|
74
|
-
Apartment::Tenant.switch! db_name
|
75
|
-
expect(Apartment.connection.query_cache_enabled).to be true
|
76
|
-
|
77
|
-
ActiveRecord::Base.connection.disable_query_cache!
|
78
|
-
|
79
|
-
Apartment::Tenant.switch! db_name
|
80
|
-
expect(Apartment.connection.query_cache_enabled).to be false
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
@@ -1,28 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
describe 'using apartment within an engine' do
|
4
|
-
before do
|
5
|
-
engine_path = Pathname.new(File.expand_path('../dummy_engine', __dir__))
|
6
|
-
require engine_path.join('test/dummy/config/application')
|
7
|
-
@rake = Rake::Application.new
|
8
|
-
Rake.application = @rake
|
9
|
-
stub_const 'APP_RAKEFILE', engine_path.join('test/dummy/Rakefile')
|
10
|
-
load 'rails/tasks/engine.rake'
|
11
|
-
end
|
12
|
-
|
13
|
-
it 'sucessfully runs rake db:migrate in the engine root' do
|
14
|
-
expect { Rake::Task['db:migrate'].invoke }.to_not raise_error
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'sucessfully runs rake app:db:migrate in the engine root' do
|
18
|
-
expect { Rake::Task['app:db:migrate'].invoke }.to_not raise_error
|
19
|
-
end
|
20
|
-
|
21
|
-
context 'when Apartment.db_migrate_tenants is false' do
|
22
|
-
it 'should not enhance tasks' do
|
23
|
-
Apartment.db_migrate_tenants = false
|
24
|
-
expect(Apartment::RakeTaskEnhancer).to_not receive(:enhance_task).with('db:migrate')
|
25
|
-
Rake::Task['db:migrate'].invoke
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
data/spec/schemas/v1.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# This file is auto-generated from the current state of the database. Instead
|
3
|
-
# of editing this file, please use the migrations feature of Active Record to
|
4
|
-
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
-
#
|
6
|
-
# Note that this schema.rb definition is the authoritative source for your
|
7
|
-
# database schema. If you need to create the application database on another
|
8
|
-
# system, you should be using db:schema:load, not running all the migrations
|
9
|
-
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
-
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
-
#
|
12
|
-
# It's strongly recommended to check this file into your version control system.
|
13
|
-
|
14
|
-
ActiveRecord::Schema.define(version: 0) do
|
15
|
-
end
|
data/spec/schemas/v2.rb
DELETED
@@ -1,41 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# This file is auto-generated from the current state of the database. Instead
|
3
|
-
# of editing this file, please use the migrations feature of Active Record to
|
4
|
-
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
-
#
|
6
|
-
# Note that this schema.rb definition is the authoritative source for your
|
7
|
-
# database schema. If you need to create the application database on another
|
8
|
-
# system, you should be using db:schema:load, not running all the migrations
|
9
|
-
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
-
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
-
#
|
12
|
-
# It's strongly recommended to check this file into your version control system.
|
13
|
-
|
14
|
-
ActiveRecord::Schema.define(version: 20110613152810) do
|
15
|
-
create_table 'companies', force: true do |t|
|
16
|
-
t.boolean 'dummy'
|
17
|
-
t.string 'database'
|
18
|
-
end
|
19
|
-
|
20
|
-
create_table 'delayed_jobs', force: true do |t|
|
21
|
-
t.integer 'priority', default: 0
|
22
|
-
t.integer 'attempts', default: 0
|
23
|
-
t.text 'handler'
|
24
|
-
t.text 'last_error'
|
25
|
-
t.datetime 'run_at'
|
26
|
-
t.datetime 'locked_at'
|
27
|
-
t.datetime 'failed_at'
|
28
|
-
t.string 'locked_by'
|
29
|
-
t.datetime 'created_at'
|
30
|
-
t.datetime 'updated_at'
|
31
|
-
t.string 'queue'
|
32
|
-
end
|
33
|
-
|
34
|
-
add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority'
|
35
|
-
|
36
|
-
create_table 'users', force: true do |t|
|
37
|
-
t.string 'name'
|
38
|
-
t.datetime 'birthdate'
|
39
|
-
t.string 'sex'
|
40
|
-
end
|
41
|
-
end
|
data/spec/schemas/v3.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
# This file is auto-generated from the current state of the database. Instead
|
3
|
-
# of editing this file, please use the migrations feature of Active Record to
|
4
|
-
# incrementally modify your database, and then regenerate this schema definition.
|
5
|
-
#
|
6
|
-
# Note that this schema.rb definition is the authoritative source for your
|
7
|
-
# database schema. If you need to create the application database on another
|
8
|
-
# system, you should be using db:schema:load, not running all the migrations
|
9
|
-
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
10
|
-
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
11
|
-
#
|
12
|
-
# It's strongly recommended to check this file into your version control system.
|
13
|
-
|
14
|
-
ActiveRecord::Schema.define(version: 20111202022214) do
|
15
|
-
create_table 'books', force: true do |t|
|
16
|
-
t.string 'name'
|
17
|
-
t.integer 'pages'
|
18
|
-
t.datetime 'published'
|
19
|
-
end
|
20
|
-
|
21
|
-
create_table 'companies', force: true do |t|
|
22
|
-
t.boolean 'dummy'
|
23
|
-
t.string 'database'
|
24
|
-
end
|
25
|
-
|
26
|
-
create_table 'delayed_jobs', force: true do |t|
|
27
|
-
t.integer 'priority', default: 0
|
28
|
-
t.integer 'attempts', default: 0
|
29
|
-
t.text 'handler'
|
30
|
-
t.text 'last_error'
|
31
|
-
t.datetime 'run_at'
|
32
|
-
t.datetime 'locked_at'
|
33
|
-
t.datetime 'failed_at'
|
34
|
-
t.string 'locked_by'
|
35
|
-
t.datetime 'created_at'
|
36
|
-
t.datetime 'updated_at'
|
37
|
-
t.string 'queue'
|
38
|
-
end
|
39
|
-
|
40
|
-
add_index 'delayed_jobs', ['priority', 'run_at'], name: 'delayed_jobs_priority'
|
41
|
-
|
42
|
-
create_table 'users', force: true do |t|
|
43
|
-
t.string 'name'
|
44
|
-
t.datetime 'birthdate'
|
45
|
-
t.string 'sex'
|
46
|
-
end
|
47
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
-
|
5
|
-
# Configure Rails Environment
|
6
|
-
ENV['RAILS_ENV'] = 'test'
|
7
|
-
|
8
|
-
require File.expand_path('dummy/config/environment.rb', __dir__)
|
9
|
-
|
10
|
-
# Loading dummy applications affects table_name of each excluded models
|
11
|
-
# defined in `spec/dummy/config/initializers/apartment.rb`.
|
12
|
-
# To make them pristine, we need to execute below lines.
|
13
|
-
Apartment.excluded_models.each do |model|
|
14
|
-
klass = model.constantize
|
15
|
-
|
16
|
-
Apartment.connection_class.remove_connection(klass)
|
17
|
-
klass.clear_all_connections!
|
18
|
-
klass.reset_table_name
|
19
|
-
end
|
20
|
-
|
21
|
-
require 'rspec/rails'
|
22
|
-
require 'capybara/rspec'
|
23
|
-
require 'capybara/rails'
|
24
|
-
|
25
|
-
begin
|
26
|
-
require 'pry'
|
27
|
-
silence_warnings { IRB = Pry }
|
28
|
-
rescue LoadError
|
29
|
-
nil
|
30
|
-
end
|
31
|
-
|
32
|
-
ActionMailer::Base.delivery_method = :test
|
33
|
-
ActionMailer::Base.perform_deliveries = true
|
34
|
-
ActionMailer::Base.default_url_options[:host] = 'test.com'
|
35
|
-
|
36
|
-
Rails.backtrace_cleaner.remove_silencers!
|
37
|
-
|
38
|
-
# Load support files
|
39
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
40
|
-
|
41
|
-
RSpec.configure do |config|
|
42
|
-
config.include RSpec::Integration::CapybaraSessions, type: :request
|
43
|
-
config.include Apartment::Spec::Setup
|
44
|
-
|
45
|
-
# Somewhat brutal hack so that rails 4 postgres extensions don't modify this file
|
46
|
-
config.after(:all) do
|
47
|
-
`git checkout -- spec/dummy/db/schema.rb`
|
48
|
-
end
|
49
|
-
|
50
|
-
# rspec-rails 3 will no longer automatically infer an example group's spec type
|
51
|
-
# from the file location. You can explicitly opt-in to the feature using this
|
52
|
-
# config option.
|
53
|
-
# To explicitly tag specs without using automatic inference, set the `:type`
|
54
|
-
# metadata manually:
|
55
|
-
#
|
56
|
-
# describe ThingsController, :type => :controller do
|
57
|
-
# # Equivalent to being in spec/controllers
|
58
|
-
# end
|
59
|
-
config.infer_spec_type_from_file_location!
|
60
|
-
end
|
61
|
-
|
62
|
-
# Load shared examples, must happen after configure for RSpec 3
|
63
|
-
Dir["#{File.dirname(__FILE__)}/examples/**/*.rb"].each { |f| require f }
|
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Apartment
|
4
|
-
module Test
|
5
|
-
# rubocop:disable Style/ModuleFunction
|
6
|
-
extend self
|
7
|
-
# rubocop:enable Style/ModuleFunction
|
8
|
-
|
9
|
-
def reset
|
10
|
-
Apartment.excluded_models = nil
|
11
|
-
Apartment.use_schemas = nil
|
12
|
-
Apartment.seed_after_create = nil
|
13
|
-
Apartment.default_schema = nil
|
14
|
-
end
|
15
|
-
|
16
|
-
def next_db
|
17
|
-
@x ||= 0
|
18
|
-
format('db%<db_idx>d', db_idx: @x += 1)
|
19
|
-
end
|
20
|
-
|
21
|
-
def drop_schema(schema)
|
22
|
-
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE")
|
23
|
-
rescue StandardError => _e
|
24
|
-
true
|
25
|
-
end
|
26
|
-
|
27
|
-
# Use this if you don't want to import schema.rb etc... but need the postgres schema to exist
|
28
|
-
# basically for speed purposes
|
29
|
-
def create_schema(schema)
|
30
|
-
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{schema}")
|
31
|
-
end
|
32
|
-
|
33
|
-
def load_schema(version = 3)
|
34
|
-
file = File.expand_path("../../schemas/v#{version}.rb", __FILE__)
|
35
|
-
|
36
|
-
silence_warnings { load(file) }
|
37
|
-
end
|
38
|
-
|
39
|
-
def migrate
|
40
|
-
ActiveRecord::Migrator.migrate(Rails.root + ActiveRecord::Migrator.migrations_path)
|
41
|
-
end
|
42
|
-
|
43
|
-
def rollback
|
44
|
-
ActiveRecord::Migrator.rollback(Rails.root + ActiveRecord::Migrator.migrations_path)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Integration
|
5
|
-
module CapybaraSessions
|
6
|
-
def in_new_session(&_block)
|
7
|
-
yield new_session
|
8
|
-
end
|
9
|
-
|
10
|
-
def new_session
|
11
|
-
Capybara::Session.new(Capybara.current_driver, Capybara.app)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
data/spec/support/config.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'yaml'
|
4
|
-
|
5
|
-
module Apartment
|
6
|
-
module Test
|
7
|
-
def self.config
|
8
|
-
# rubocop:disable Security/YAMLLoad
|
9
|
-
@config ||= YAML.load(ERB.new(IO.read('spec/config/database.yml')).result)
|
10
|
-
# rubocop:enable Security/YAMLLoad
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
data/spec/support/contexts.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Some shared contexts for specs
|
4
|
-
|
5
|
-
shared_context 'with default schema', default_schema: true do
|
6
|
-
let(:default_schema) { Apartment::Test.next_db }
|
7
|
-
|
8
|
-
before do
|
9
|
-
Apartment::Test.create_schema(default_schema)
|
10
|
-
Apartment.default_schema = default_schema
|
11
|
-
end
|
12
|
-
|
13
|
-
after do
|
14
|
-
# resetting default_schema so we can drop and any further resets won't try to access droppped schema
|
15
|
-
Apartment.default_schema = nil
|
16
|
-
Apartment::Test.drop_schema(default_schema)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
# Some default setup for elevator specs
|
21
|
-
shared_context 'elevators', elevator: true do
|
22
|
-
let(:company1) { mock_model(Company, database: db1).as_null_object }
|
23
|
-
let(:company2) { mock_model(Company, database: db2).as_null_object }
|
24
|
-
|
25
|
-
let(:api) { Apartment::Tenant }
|
26
|
-
|
27
|
-
before do
|
28
|
-
Apartment.reset # reset all config
|
29
|
-
Apartment.seed_after_create = false
|
30
|
-
Apartment.use_schemas = true
|
31
|
-
api.reload!(config)
|
32
|
-
api.create(db1)
|
33
|
-
api.create(db2)
|
34
|
-
end
|
35
|
-
|
36
|
-
after do
|
37
|
-
api.drop(db1)
|
38
|
-
api.drop(db2)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
shared_context 'persistent_schemas', persistent_schemas: true do
|
43
|
-
let(:persistent_schemas) { %w[hstore postgis] }
|
44
|
-
|
45
|
-
before do
|
46
|
-
persistent_schemas.map { |schema| subject.create(schema) }
|
47
|
-
Apartment.persistent_schemas = persistent_schemas
|
48
|
-
end
|
49
|
-
|
50
|
-
after do
|
51
|
-
Apartment.persistent_schemas = []
|
52
|
-
persistent_schemas.map { |schema| subject.drop(schema) }
|
53
|
-
end
|
54
|
-
end
|
@@ -1,48 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Apartment
|
4
|
-
module Spec
|
5
|
-
#
|
6
|
-
# Define the interface methods required to
|
7
|
-
# use an adapter shared example
|
8
|
-
#
|
9
|
-
#
|
10
|
-
module AdapterRequirements
|
11
|
-
extend ActiveSupport::Concern
|
12
|
-
|
13
|
-
included do
|
14
|
-
before do
|
15
|
-
subject.create(db1)
|
16
|
-
subject.create(db2)
|
17
|
-
end
|
18
|
-
|
19
|
-
after do
|
20
|
-
# Reset before dropping (can't drop a db you're connected to)
|
21
|
-
subject.reset
|
22
|
-
|
23
|
-
# sometimes we manually drop these schemas in testing, don't care if
|
24
|
-
# we can't drop, hence rescue
|
25
|
-
begin
|
26
|
-
subject.drop(db1)
|
27
|
-
rescue StandardError => _e
|
28
|
-
true
|
29
|
-
end
|
30
|
-
|
31
|
-
begin
|
32
|
-
subject.drop(db2)
|
33
|
-
rescue StandardError => _e
|
34
|
-
true
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
%w[subject tenant_names default_tenant].each do |method|
|
40
|
-
next if defined?(method)
|
41
|
-
|
42
|
-
define_method method do
|
43
|
-
raise "You must define a `#{method}` method in your host group"
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
data/spec/support/setup.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Apartment
|
4
|
-
module Spec
|
5
|
-
module Setup
|
6
|
-
def self.included(base)
|
7
|
-
base.instance_eval do
|
8
|
-
let(:db1) { Apartment::Test.next_db }
|
9
|
-
let(:db2) { Apartment::Test.next_db }
|
10
|
-
let(:connection) { ActiveRecord::Base.connection }
|
11
|
-
|
12
|
-
# This around ensures that we run these hooks before and after
|
13
|
-
# any before/after hooks defined in individual tests
|
14
|
-
# Otherwise these actually get run after test defined hooks
|
15
|
-
around(:each) do |example|
|
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,124 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'spec_helper'
|
4
|
-
require 'rake'
|
5
|
-
require 'apartment/migrator'
|
6
|
-
require 'apartment/tenant'
|
7
|
-
|
8
|
-
describe 'apartment rake tasks' do
|
9
|
-
before do
|
10
|
-
@rake = Rake::Application.new
|
11
|
-
Rake.application = @rake
|
12
|
-
load 'tasks/apartment.rake'
|
13
|
-
# stub out rails tasks
|
14
|
-
Rake::Task.define_task('db:migrate')
|
15
|
-
Rake::Task.define_task('db:seed')
|
16
|
-
Rake::Task.define_task('db:rollback')
|
17
|
-
Rake::Task.define_task('db:migrate:up')
|
18
|
-
Rake::Task.define_task('db:migrate:down')
|
19
|
-
Rake::Task.define_task('db:migrate:redo')
|
20
|
-
end
|
21
|
-
|
22
|
-
after do
|
23
|
-
Rake.application = nil
|
24
|
-
ENV['VERSION'] = nil # linux users reported env variable carrying on between tests
|
25
|
-
end
|
26
|
-
|
27
|
-
after(:all) do
|
28
|
-
Apartment::Test.load_schema
|
29
|
-
end
|
30
|
-
|
31
|
-
let(:version) { '1234' }
|
32
|
-
|
33
|
-
context 'database migration' do
|
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
|
-
context 'without a version' do
|
54
|
-
before do
|
55
|
-
ENV['VERSION'] = nil
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'requires a version to migrate to' do
|
59
|
-
expect do
|
60
|
-
@rake['apartment:migrate:up'].invoke
|
61
|
-
end.to raise_error('VERSION is required')
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
context 'with version' do
|
66
|
-
before do
|
67
|
-
ENV['VERSION'] = version
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'migrates up to a specific version' do
|
71
|
-
expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
|
72
|
-
@rake['apartment:migrate:up'].invoke
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
describe 'apartment:migrate:down' do
|
78
|
-
context 'without a version' do
|
79
|
-
before do
|
80
|
-
ENV['VERSION'] = nil
|
81
|
-
end
|
82
|
-
|
83
|
-
it 'requires a version to migrate to' do
|
84
|
-
expect do
|
85
|
-
@rake['apartment:migrate:down'].invoke
|
86
|
-
end.to raise_error('VERSION is required')
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
context 'with version' do
|
91
|
-
before do
|
92
|
-
ENV['VERSION'] = version
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'migrates up to a specific version' do
|
96
|
-
expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
|
97
|
-
@rake['apartment:migrate:down'].invoke
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe 'apartment:rollback' do
|
103
|
-
let(:step) { '3' }
|
104
|
-
|
105
|
-
it 'should rollback dbs' do
|
106
|
-
expect(Apartment::Migrator).to receive(:rollback).exactly(tenant_count).times
|
107
|
-
@rake['apartment:rollback'].invoke
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'should rollback dbs STEP amt' do
|
111
|
-
expect(Apartment::Migrator).to receive(:rollback).with(anything, step.to_i).exactly(tenant_count).times
|
112
|
-
ENV['STEP'] = step
|
113
|
-
@rake['apartment:rollback'].invoke
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe 'apartment:drop' do
|
118
|
-
it 'should migrate public and all multi-tenant dbs' do
|
119
|
-
expect(Apartment::Tenant).to receive(:drop).exactly(tenant_count).times
|
120
|
-
@rake['apartment:drop'].invoke
|
121
|
-
end
|
122
|
-
end
|
123
|
-
end
|
124
|
-
end
|