apartment 0.13.0 → 0.13.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +30 -23
- data/apartment.gemspec +15 -12
- data/lib/apartment.rb +34 -21
- data/lib/apartment/adapters/abstract_adapter.rb +59 -59
- data/lib/apartment/adapters/postgresql_adapter.rb +38 -38
- data/lib/apartment/database.rb +22 -20
- data/lib/apartment/delayed_job/hooks.rb +5 -5
- data/lib/apartment/elevators/subdomain.rb +27 -11
- data/lib/apartment/version.rb +1 -1
- data/spec/adapters/postgresql_adapter_spec.rb +38 -29
- data/spec/apartment_spec.rb +1 -1
- data/spec/dummy/db/schema.rb +7 -1
- data/spec/integration/database_integration_spec.rb +44 -44
- data/spec/integration/middleware/subdomain_elevator_spec.rb +15 -15
- data/spec/spec_helper.rb +7 -3
- data/spec/support/apartment_helpers.rb +8 -8
- data/spec/unit/config_spec.rb +48 -18
- data/spec/unit/middleware/subdomain_elevator_spec.rb +11 -8
- data/spec/unit/reloader_spec.rb +6 -6
- metadata +106 -91
data/spec/apartment_spec.rb
CHANGED
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,13 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:version =>
|
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
|
15
21
|
|
16
22
|
create_table "companies", :force => true do |t|
|
17
23
|
t.boolean "dummy"
|
@@ -1,15 +1,15 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Apartment::Database do
|
4
|
-
|
4
|
+
|
5
5
|
context "using postgresql" do
|
6
|
-
|
6
|
+
|
7
7
|
# See apartment.yml file in dummy app config
|
8
|
-
|
8
|
+
|
9
9
|
let(:config){ Apartment::Test.config['connections']['postgresql'].symbolize_keys }
|
10
10
|
let(:database){ "some_new_database" }
|
11
11
|
let(:database2){ "yet_another_database" }
|
12
|
-
|
12
|
+
|
13
13
|
before do
|
14
14
|
Apartment.use_postgres_schemas = true
|
15
15
|
ActiveRecord::Base.establish_connection config
|
@@ -17,47 +17,47 @@ describe Apartment::Database do
|
|
17
17
|
Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
|
18
18
|
@schema_search_path = ActiveRecord::Base.connection.schema_search_path
|
19
19
|
end
|
20
|
-
|
20
|
+
|
21
21
|
after do
|
22
22
|
Apartment::Test.reset
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
describe "#init" do
|
26
|
-
|
26
|
+
|
27
27
|
it "should process model exclusions" do
|
28
28
|
Apartment.configure do |config|
|
29
29
|
config.excluded_models = ["Company"]
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
Apartment::Database.init
|
33
|
-
|
33
|
+
|
34
34
|
Company.table_name.should == "public.companies"
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
describe "#adapter" do
|
40
40
|
before do
|
41
41
|
Apartment::Database.reload!
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
it "should load postgresql adapter" do
|
45
45
|
Apartment::Database.adapter
|
46
46
|
Apartment::Adapters::PostgresqlAdapter.should be_a(Class)
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
it "should raise exception with invalid adapter specified" do
|
50
50
|
Apartment::Database.stub(:config).and_return config.merge(:adapter => 'unkown')
|
51
|
-
|
51
|
+
|
52
52
|
expect {
|
53
53
|
Apartment::Database.adapter
|
54
54
|
}.to raise_error
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
context "with schemas" do
|
60
|
-
|
60
|
+
|
61
61
|
before do
|
62
62
|
Apartment.configure do |config|
|
63
63
|
config.excluded_models = []
|
@@ -66,21 +66,21 @@ describe Apartment::Database do
|
|
66
66
|
end
|
67
67
|
Apartment::Database.create database
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
after do
|
71
71
|
Apartment::Test.drop_schema database
|
72
72
|
end
|
73
|
-
|
73
|
+
|
74
74
|
describe "#process" do
|
75
|
-
|
75
|
+
|
76
76
|
before do
|
77
77
|
Apartment::Database.create database2
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
after do
|
81
81
|
Apartment::Test.drop_schema database2
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
it "should connect to new schema" do
|
85
85
|
Apartment::Database.process(database) do
|
86
86
|
Apartment::Database.current_database.should == database
|
@@ -92,49 +92,49 @@ describe Apartment::Database do
|
|
92
92
|
Apartment::Database.process(database)
|
93
93
|
Apartment::Database.current_database.should == database2
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
it "should reset to previous schema if database is nil" do
|
97
97
|
Apartment::Database.switch(database)
|
98
98
|
Apartment::Database.process
|
99
99
|
Apartment::Database.current_database.should == database
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
it "should set to public schema if database is nil" do
|
103
103
|
Apartment::Database.process do
|
104
104
|
Apartment::Database.current_database.should == @schema_search_path
|
105
105
|
end
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
end
|
109
|
-
|
109
|
+
|
110
110
|
describe "#create" do
|
111
111
|
it "should create new postgres schema" do
|
112
112
|
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect{|row| row['nspname']}.should include(database)
|
113
113
|
end
|
114
|
-
|
114
|
+
|
115
115
|
it "should seed data" do
|
116
116
|
Apartment::Database.switch database
|
117
117
|
User.count.should be > 0
|
118
118
|
end
|
119
119
|
end
|
120
|
-
|
120
|
+
|
121
121
|
describe "#switch" do
|
122
|
-
|
122
|
+
|
123
123
|
let(:x){ rand(3) }
|
124
|
-
|
124
|
+
|
125
125
|
it "should connect to new schema" do
|
126
126
|
Apartment::Database.switch database
|
127
127
|
ActiveRecord::Base.connection.schema_search_path.should == database
|
128
128
|
end
|
129
|
-
|
129
|
+
|
130
130
|
it "should fail with invalid schema" do
|
131
131
|
expect {
|
132
132
|
Apartment::Database.switch('some_nonexistent_schema')
|
133
133
|
}.to raise_error Apartment::SchemaNotFound
|
134
134
|
end
|
135
|
-
|
135
|
+
|
136
136
|
context "creating models" do
|
137
|
-
|
137
|
+
|
138
138
|
before do
|
139
139
|
Apartment::Database.create database2
|
140
140
|
end
|
@@ -142,7 +142,7 @@ describe Apartment::Database do
|
|
142
142
|
after do
|
143
143
|
Apartment::Test.drop_schema database2
|
144
144
|
end
|
145
|
-
|
145
|
+
|
146
146
|
it "should create a model instance in the current schema" do
|
147
147
|
Apartment::Database.switch database2
|
148
148
|
db2_count = User.count + x.times{ User.create }
|
@@ -157,25 +157,25 @@ describe Apartment::Database do
|
|
157
157
|
User.count.should == db_count
|
158
158
|
end
|
159
159
|
end
|
160
|
-
|
160
|
+
|
161
161
|
context "with excluded models" do
|
162
|
-
|
162
|
+
|
163
163
|
before do
|
164
164
|
Apartment.configure do |config|
|
165
165
|
config.excluded_models = ["Company"]
|
166
166
|
end
|
167
167
|
Apartment::Database.init
|
168
168
|
end
|
169
|
-
|
169
|
+
|
170
170
|
it "should ignore excluded models" do
|
171
171
|
Apartment::Database.switch database
|
172
172
|
Company.table_name.should include('public')
|
173
173
|
end
|
174
|
-
|
174
|
+
|
175
175
|
it "should create excluded models in public schema" do
|
176
176
|
Apartment::Database.reset # ensure we're on public schema
|
177
177
|
count = Company.count + x.times{ Company.create }
|
178
|
-
|
178
|
+
|
179
179
|
Apartment::Database.switch database
|
180
180
|
x.times{ Company.create }
|
181
181
|
Company.count.should == count + x
|
@@ -183,18 +183,18 @@ describe Apartment::Database do
|
|
183
183
|
Company.count.should == count + x
|
184
184
|
end
|
185
185
|
end
|
186
|
-
|
186
|
+
|
187
187
|
end
|
188
|
-
|
188
|
+
|
189
189
|
describe "#current_database" do
|
190
|
-
|
190
|
+
|
191
191
|
it "should return the current schema search path" do
|
192
192
|
Apartment::Database.switch database
|
193
193
|
Apartment::Database.current_database.should == database
|
194
194
|
end
|
195
195
|
end
|
196
|
-
|
196
|
+
|
197
197
|
end
|
198
|
-
|
198
|
+
|
199
199
|
end
|
200
200
|
end
|
@@ -1,41 +1,41 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Apartment::Elevators::Subdomain do
|
4
|
-
|
4
|
+
|
5
5
|
let(:company){ mock_model(Company, :subdomain => 'foo').as_null_object }
|
6
6
|
let(:domain){ "http://#{company.subdomain}.domain.com" }
|
7
|
-
|
7
|
+
|
8
8
|
before do
|
9
9
|
Apartment.seed_after_create = false
|
10
10
|
Apartment.use_postgres_schemas = true
|
11
|
-
|
11
|
+
|
12
12
|
Apartment::Database.create(company.subdomain)
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
after do
|
16
16
|
Apartment::Test.drop_schema(company.subdomain)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
context "single request" do
|
20
20
|
it "should switch the db" do
|
21
21
|
ActiveRecord::Base.connection.schema_search_path.should_not == 'foo'
|
22
|
-
|
22
|
+
|
23
23
|
visit(domain)
|
24
24
|
ActiveRecord::Base.connection.schema_search_path.should == company.subdomain
|
25
25
|
end
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
context "simultaneous requests" do
|
29
29
|
let(:company2){ mock_model(Company, :subdomain => 'bar').as_null_object }
|
30
30
|
let(:domain2){ "http://#{company2.subdomain}.domain.com" }
|
31
|
-
|
31
|
+
|
32
32
|
before do
|
33
33
|
Apartment::Database.create(company2.subdomain)
|
34
34
|
# Create some users for each db
|
35
35
|
Apartment::Database.process(company.subdomain) do
|
36
36
|
@c1_user_count = (2 + rand(2)).times{ User.create }
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
Apartment::Database.process(company2.subdomain) do
|
40
40
|
@c2_user_count = (@c1_user_count + 2).times{ User.create }
|
41
41
|
end
|
@@ -44,20 +44,20 @@ describe Apartment::Elevators::Subdomain do
|
|
44
44
|
after do
|
45
45
|
Apartment::Test.drop_schema(company2.subdomain)
|
46
46
|
end
|
47
|
-
|
47
|
+
|
48
48
|
it "should fetch the correct user count for each session based on subdomain" do
|
49
49
|
visit(domain)
|
50
|
-
|
50
|
+
|
51
51
|
in_new_session do |session|
|
52
52
|
session.visit(domain2)
|
53
53
|
User.count.should == @c2_user_count
|
54
54
|
end
|
55
|
-
|
55
|
+
|
56
56
|
visit(domain)
|
57
57
|
User.count.should == @c1_user_count
|
58
58
|
end
|
59
|
-
|
60
|
-
|
59
|
+
|
60
|
+
|
61
61
|
end
|
62
|
-
|
62
|
+
|
63
63
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -19,13 +19,17 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
19
19
|
|
20
20
|
|
21
21
|
RSpec.configure do |config|
|
22
|
-
|
22
|
+
|
23
23
|
config.include RSpec::Integration::CapybaraSessions, :type => :request
|
24
|
-
|
24
|
+
|
25
25
|
config.before(:all) do
|
26
26
|
# Ensure that each test starts with a clean connection
|
27
27
|
# Necessary as some tests will leak things like current_schema into the next test
|
28
28
|
ActiveRecord::Base.clear_all_connections!
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
|
+
config.before(:each) do
|
32
|
+
Apartment.reset
|
33
|
+
end
|
34
|
+
|
31
35
|
end
|
@@ -1,32 +1,32 @@
|
|
1
1
|
module Apartment
|
2
2
|
module Test
|
3
|
-
|
3
|
+
|
4
4
|
extend self
|
5
|
-
|
5
|
+
|
6
6
|
def reset
|
7
7
|
Apartment.excluded_models = nil
|
8
8
|
Apartment.use_postgres_schemas = nil
|
9
9
|
end
|
10
|
-
|
10
|
+
|
11
11
|
def drop_schema(schema)
|
12
12
|
ActiveRecord::Base.silence{ ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE") } rescue true
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def create_schema(schema)
|
16
16
|
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{schema}")
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
def load_schema
|
20
20
|
silence_stream(STDOUT){ load("#{Rails.root}/db/schema.rb") }
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
def migrate
|
24
24
|
ActiveRecord::Migrator.migrate(Rails.root + ActiveRecord::Migrator.migrations_path)
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
def rollback
|
28
28
|
ActiveRecord::Migrator.rollback(Rails.root + ActiveRecord::Migrator.migrations_path)
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
end
|
32
32
|
end
|
data/spec/unit/config_spec.rb
CHANGED
@@ -1,29 +1,25 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Apartment do
|
4
|
-
|
4
|
+
|
5
5
|
describe "#config" do
|
6
|
-
|
6
|
+
|
7
7
|
let(:excluded_models){ [Company] }
|
8
|
-
|
9
|
-
after do
|
10
|
-
Apartment::Test.reset
|
11
|
-
end
|
12
|
-
|
8
|
+
|
13
9
|
it "should yield the Apartment object" do
|
14
10
|
Apartment.configure do |config|
|
15
11
|
config.excluded_models = []
|
16
12
|
config.should == Apartment
|
17
13
|
end
|
18
14
|
end
|
19
|
-
|
15
|
+
|
20
16
|
it "should set excluded models" do
|
21
17
|
Apartment.configure do |config|
|
22
18
|
config.excluded_models = excluded_models
|
23
19
|
end
|
24
20
|
Apartment.excluded_models.should == excluded_models
|
25
21
|
end
|
26
|
-
|
22
|
+
|
27
23
|
it "should set postgres_schemas" do
|
28
24
|
Apartment.configure do |config|
|
29
25
|
config.excluded_models = []
|
@@ -31,7 +27,7 @@ describe Apartment do
|
|
31
27
|
end
|
32
28
|
Apartment.use_postgres_schemas.should be_false
|
33
29
|
end
|
34
|
-
|
30
|
+
|
35
31
|
it "should set seed_after_create" do
|
36
32
|
Apartment.configure do |config|
|
37
33
|
config.excluded_models = []
|
@@ -39,40 +35,74 @@ describe Apartment do
|
|
39
35
|
end
|
40
36
|
Apartment.seed_after_create.should be_true
|
41
37
|
end
|
42
|
-
|
38
|
+
|
39
|
+
context "load schema" do
|
40
|
+
it "should default to true" do
|
41
|
+
Apartment.configure
|
42
|
+
Apartment.load_schema.should be_true
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should set load_schema" do
|
46
|
+
Apartment.configure do |config|
|
47
|
+
config.load_schema = false
|
48
|
+
end
|
49
|
+
Apartment.load_schema.should be_false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
43
53
|
context "databases" do
|
44
54
|
it "should return object if it doesnt respond_to call" do
|
45
55
|
database_names = ['users', 'companies']
|
46
|
-
|
56
|
+
|
47
57
|
Apartment.configure do |config|
|
48
58
|
config.excluded_models = []
|
49
59
|
config.database_names = database_names
|
50
60
|
end
|
51
61
|
Apartment.database_names.should == database_names
|
52
62
|
end
|
53
|
-
|
63
|
+
|
54
64
|
it "should invoke the proc if appropriate" do
|
55
65
|
database_names = lambda{ ['users', 'users'] }
|
56
66
|
database_names.should_receive(:call)
|
57
|
-
|
67
|
+
|
58
68
|
Apartment.configure do |config|
|
59
69
|
config.excluded_models = []
|
60
70
|
config.database_names = database_names
|
61
71
|
end
|
62
72
|
Apartment.database_names
|
63
73
|
end
|
64
|
-
|
74
|
+
|
65
75
|
it "should return the invoked proc if appropriate" do
|
66
76
|
dbs = lambda{ Company.scoped }
|
67
|
-
|
77
|
+
|
68
78
|
Apartment.configure do |config|
|
69
79
|
config.excluded_models = []
|
70
80
|
config.database_names = dbs
|
71
81
|
end
|
72
|
-
|
82
|
+
|
73
83
|
Apartment.database_names.should == Company.scoped
|
74
84
|
end
|
75
85
|
end
|
76
|
-
|
86
|
+
|
87
|
+
describe "#reset" do
|
88
|
+
|
89
|
+
before do
|
90
|
+
Apartment.configure do |config|
|
91
|
+
config.excluded_models = ["One", "Two"]
|
92
|
+
config.database_names = ["one", "two"]
|
93
|
+
config.seed_after_create = true
|
94
|
+
config.load_schema = false
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
it "should reset all the config options" do
|
99
|
+
Apartment.reset
|
100
|
+
Apartment.excluded_models.should == []
|
101
|
+
Apartment.database_names.should be_nil
|
102
|
+
Apartment.seed_after_create.should be_false
|
103
|
+
Apartment.load_schema.should be_true
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
77
107
|
end
|
78
108
|
end
|