apartment 0.13.0.1 → 0.13.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/HISTORY.md +28 -29
- data/apartment.gemspec +12 -15
- data/lib/apartment.rb +21 -34
- data/lib/apartment/adapters/abstract_adapter.rb +64 -64
- data/lib/apartment/adapters/postgresql_adapter.rb +52 -51
- data/lib/apartment/database.rb +20 -22
- data/lib/apartment/delayed_job/hooks.rb +5 -5
- data/lib/apartment/elevators/subdomain.rb +11 -27
- data/lib/apartment/version.rb +1 -1
- data/spec/adapters/postgresql_adapter_spec.rb +29 -38
- data/spec/apartment_spec.rb +1 -1
- data/spec/dummy/db/schema.rb +1 -7
- data/spec/integration/database_integration_spec.rb +44 -44
- data/spec/integration/middleware/subdomain_elevator_spec.rb +15 -15
- data/spec/spec_helper.rb +3 -7
- data/spec/support/apartment_helpers.rb +8 -8
- data/spec/tasks/apartment_rake_spec.rb +1 -0
- data/spec/unit/config_spec.rb +18 -48
- data/spec/unit/middleware/subdomain_elevator_spec.rb +8 -11
- data/spec/unit/reloader_spec.rb +6 -6
- metadata +92 -107
data/spec/apartment_spec.rb
CHANGED
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,13 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended to check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(:version =>
|
15
|
-
|
16
|
-
create_table "books", :force => true do |t|
|
17
|
-
t.string "name"
|
18
|
-
t.integer "pages"
|
19
|
-
t.datetime "published"
|
20
|
-
end
|
14
|
+
ActiveRecord::Schema.define(:version => 20110613152810) do
|
21
15
|
|
22
16
|
create_table "companies", :force => true do |t|
|
23
17
|
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,17 +19,13 @@ 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
|
-
|
31
|
-
config.before(:each) do
|
32
|
-
Apartment.reset
|
33
|
-
end
|
34
|
-
|
30
|
+
|
35
31
|
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,25 +1,29 @@
|
|
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
|
-
|
8
|
+
|
9
|
+
after do
|
10
|
+
Apartment::Test.reset
|
11
|
+
end
|
12
|
+
|
9
13
|
it "should yield the Apartment object" do
|
10
14
|
Apartment.configure do |config|
|
11
15
|
config.excluded_models = []
|
12
16
|
config.should == Apartment
|
13
17
|
end
|
14
18
|
end
|
15
|
-
|
19
|
+
|
16
20
|
it "should set excluded models" do
|
17
21
|
Apartment.configure do |config|
|
18
22
|
config.excluded_models = excluded_models
|
19
23
|
end
|
20
24
|
Apartment.excluded_models.should == excluded_models
|
21
25
|
end
|
22
|
-
|
26
|
+
|
23
27
|
it "should set postgres_schemas" do
|
24
28
|
Apartment.configure do |config|
|
25
29
|
config.excluded_models = []
|
@@ -27,7 +31,7 @@ describe Apartment do
|
|
27
31
|
end
|
28
32
|
Apartment.use_postgres_schemas.should be_false
|
29
33
|
end
|
30
|
-
|
34
|
+
|
31
35
|
it "should set seed_after_create" do
|
32
36
|
Apartment.configure do |config|
|
33
37
|
config.excluded_models = []
|
@@ -35,74 +39,40 @@ describe Apartment do
|
|
35
39
|
end
|
36
40
|
Apartment.seed_after_create.should be_true
|
37
41
|
end
|
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
|
-
|
42
|
+
|
53
43
|
context "databases" do
|
54
44
|
it "should return object if it doesnt respond_to call" do
|
55
45
|
database_names = ['users', 'companies']
|
56
|
-
|
46
|
+
|
57
47
|
Apartment.configure do |config|
|
58
48
|
config.excluded_models = []
|
59
49
|
config.database_names = database_names
|
60
50
|
end
|
61
51
|
Apartment.database_names.should == database_names
|
62
52
|
end
|
63
|
-
|
53
|
+
|
64
54
|
it "should invoke the proc if appropriate" do
|
65
55
|
database_names = lambda{ ['users', 'users'] }
|
66
56
|
database_names.should_receive(:call)
|
67
|
-
|
57
|
+
|
68
58
|
Apartment.configure do |config|
|
69
59
|
config.excluded_models = []
|
70
60
|
config.database_names = database_names
|
71
61
|
end
|
72
62
|
Apartment.database_names
|
73
63
|
end
|
74
|
-
|
64
|
+
|
75
65
|
it "should return the invoked proc if appropriate" do
|
76
66
|
dbs = lambda{ Company.scoped }
|
77
|
-
|
67
|
+
|
78
68
|
Apartment.configure do |config|
|
79
69
|
config.excluded_models = []
|
80
70
|
config.database_names = dbs
|
81
71
|
end
|
82
|
-
|
72
|
+
|
83
73
|
Apartment.database_names.should == Company.scoped
|
84
74
|
end
|
85
75
|
end
|
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
|
-
|
76
|
+
|
107
77
|
end
|
108
78
|
end
|