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
data/spec/schemas/v1.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: UTF-8
|
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
|
+
|
16
|
+
end
|
data/spec/schemas/v2.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# encoding: UTF-8
|
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
|
+
|
16
|
+
create_table "companies", :force => true do |t|
|
17
|
+
t.boolean "dummy"
|
18
|
+
t.string "database"
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table "delayed_jobs", :force => true do |t|
|
22
|
+
t.integer "priority", :default => 0
|
23
|
+
t.integer "attempts", :default => 0
|
24
|
+
t.text "handler"
|
25
|
+
t.text "last_error"
|
26
|
+
t.datetime "run_at"
|
27
|
+
t.datetime "locked_at"
|
28
|
+
t.datetime "failed_at"
|
29
|
+
t.string "locked_by"
|
30
|
+
t.datetime "created_at"
|
31
|
+
t.datetime "updated_at"
|
32
|
+
t.string "queue"
|
33
|
+
end
|
34
|
+
|
35
|
+
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
|
36
|
+
|
37
|
+
create_table "users", :force => true do |t|
|
38
|
+
t.string "name"
|
39
|
+
t.datetime "birthdate"
|
40
|
+
t.string "sex"
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/spec/schemas/v3.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: UTF-8
|
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
|
+
|
16
|
+
create_table "books", :force => true do |t|
|
17
|
+
t.string "name"
|
18
|
+
t.integer "pages"
|
19
|
+
t.datetime "published"
|
20
|
+
end
|
21
|
+
|
22
|
+
create_table "companies", :force => true do |t|
|
23
|
+
t.boolean "dummy"
|
24
|
+
t.string "database"
|
25
|
+
end
|
26
|
+
|
27
|
+
create_table "delayed_jobs", :force => true do |t|
|
28
|
+
t.integer "priority", :default => 0
|
29
|
+
t.integer "attempts", :default => 0
|
30
|
+
t.text "handler"
|
31
|
+
t.text "last_error"
|
32
|
+
t.datetime "run_at"
|
33
|
+
t.datetime "locked_at"
|
34
|
+
t.datetime "failed_at"
|
35
|
+
t.string "locked_by"
|
36
|
+
t.datetime "created_at"
|
37
|
+
t.datetime "updated_at"
|
38
|
+
t.string "queue"
|
39
|
+
end
|
40
|
+
|
41
|
+
add_index "delayed_jobs", ["priority", "run_at"], :name => "delayed_jobs_priority"
|
42
|
+
|
43
|
+
create_table "users", :force => true do |t|
|
44
|
+
t.string "name"
|
45
|
+
t.datetime "birthdate"
|
46
|
+
t.string "sex"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
# Configure Rails Environment
|
4
|
+
ENV["RAILS_ENV"] = "test"
|
5
|
+
|
6
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
7
|
+
|
8
|
+
# Loading dummy applications affects table_name of each excluded models
|
9
|
+
# defined in `spec/dummy/config/initializers/apartment.rb`.
|
10
|
+
# To make them pristine, we need to execute below lines.
|
11
|
+
Apartment.excluded_models.each do |model|
|
12
|
+
klass = model.constantize
|
13
|
+
|
14
|
+
Apartment.connection_class.remove_connection(klass)
|
15
|
+
klass.clear_all_connections!
|
16
|
+
klass.reset_table_name
|
17
|
+
end
|
18
|
+
|
19
|
+
require "rspec/rails"
|
20
|
+
require 'capybara/rspec'
|
21
|
+
require 'capybara/rails'
|
22
|
+
|
23
|
+
begin
|
24
|
+
require 'pry'
|
25
|
+
silence_warnings{ IRB = Pry }
|
26
|
+
rescue LoadError
|
27
|
+
end
|
28
|
+
|
29
|
+
ActionMailer::Base.delivery_method = :test
|
30
|
+
ActionMailer::Base.perform_deliveries = true
|
31
|
+
ActionMailer::Base.default_url_options[:host] = "test.com"
|
32
|
+
|
33
|
+
Rails.backtrace_cleaner.remove_silencers!
|
34
|
+
|
35
|
+
# Load support files
|
36
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
37
|
+
|
38
|
+
RSpec.configure do |config|
|
39
|
+
|
40
|
+
config.include RSpec::Integration::CapybaraSessions, type: :request
|
41
|
+
config.include Apartment::Spec::Setup
|
42
|
+
|
43
|
+
# Somewhat brutal hack so that rails 4 postgres extensions don't modify this file
|
44
|
+
config.after(:all) do
|
45
|
+
`git checkout -- spec/dummy/db/schema.rb`
|
46
|
+
end
|
47
|
+
|
48
|
+
# rspec-rails 3 will no longer automatically infer an example group's spec type
|
49
|
+
# from the file location. You can explicitly opt-in to the feature using this
|
50
|
+
# config option.
|
51
|
+
# To explicitly tag specs without using automatic inference, set the `:type`
|
52
|
+
# metadata manually:
|
53
|
+
#
|
54
|
+
# describe ThingsController, :type => :controller do
|
55
|
+
# # Equivalent to being in spec/controllers
|
56
|
+
# end
|
57
|
+
config.infer_spec_type_from_file_location!
|
58
|
+
end
|
59
|
+
|
60
|
+
# Load shared examples, must happen after configure for RSpec 3
|
61
|
+
Dir["#{File.dirname(__FILE__)}/examples/**/*.rb"].each { |f| require f }
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Apartment
|
2
|
+
module Test
|
3
|
+
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def reset
|
7
|
+
Apartment.excluded_models = nil
|
8
|
+
Apartment.use_schemas = nil
|
9
|
+
Apartment.seed_after_create = nil
|
10
|
+
Apartment.default_schema = nil
|
11
|
+
end
|
12
|
+
|
13
|
+
def next_db
|
14
|
+
@x ||= 0
|
15
|
+
"db%d" % @x += 1
|
16
|
+
end
|
17
|
+
|
18
|
+
def drop_schema(schema)
|
19
|
+
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE") rescue true
|
20
|
+
end
|
21
|
+
|
22
|
+
# Use this if you don't want to import schema.rb etc... but need the postgres schema to exist
|
23
|
+
# basically for speed purposes
|
24
|
+
def create_schema(schema)
|
25
|
+
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{schema}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def load_schema(version = 3)
|
29
|
+
file = File.expand_path("../../schemas/v#{version}.rb", __FILE__)
|
30
|
+
|
31
|
+
silence_warnings{ load(file) }
|
32
|
+
end
|
33
|
+
|
34
|
+
def migrate
|
35
|
+
ActiveRecord::Migrator.migrate(Rails.root + ActiveRecord::Migrator.migrations_path)
|
36
|
+
end
|
37
|
+
|
38
|
+
def rollback
|
39
|
+
ActiveRecord::Migrator.rollback(Rails.root + ActiveRecord::Migrator.migrations_path)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,52 @@
|
|
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
|
@@ -0,0 +1,35 @@
|
|
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
|
@@ -0,0 +1,46 @@
|
|
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
|
@@ -0,0 +1,129 @@
|
|
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
|