active_tenant 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -18
- data/.travis.yml +5 -0
- data/Gemfile +4 -4
- data/LICENSE +21 -21
- data/README.md +41 -39
- data/Rakefile +2 -2
- data/active_tenant.gemspec +23 -23
- data/lib/active_tenant/active_record_extensions.rb +59 -55
- data/lib/active_tenant/adapters/postgres_adapter.rb +72 -69
- data/lib/active_tenant/adapters/sqlite_adapter.rb +75 -71
- data/lib/active_tenant/base.rb +40 -40
- data/lib/active_tenant/configuration.rb +4 -4
- data/lib/active_tenant/engine.rb +3 -3
- data/lib/active_tenant/version.rb +3 -3
- data/lib/active_tenant.rb +29 -29
- data/lib/tasks/migration.rake +21 -21
- data/spec/adapters_spec.rb +249 -217
- data/spec/migrations/20120823132512_create_globals.rb +10 -10
- data/spec/migrations/20120823132854_create_tenants.rb +12 -12
- data/spec/migrations/20120823132856_create_other_tenants.rb +12 -12
- data/spec/migrations/20120823132902_create_customs.rb +12 -12
- data/spec/models.rb +12 -0
- data/spec/spec_helper.rb +8 -10
- data/spec/support/adapter_test_helper.rb +69 -58
- metadata +40 -11
data/spec/adapters_spec.rb
CHANGED
@@ -1,218 +1,250 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
ActiveTenant::Base::ADAPTERS.
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
tenants.should
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
tenant_adapter.
|
37
|
-
|
38
|
-
|
39
|
-
tenants.should
|
40
|
-
tenants.should
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
ActiveTenant.current.
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
ActiveTenant.current.
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
ActiveRecord::Base.connection.table_exists?('
|
98
|
-
ActiveRecord::Base.connection.table_exists?('
|
99
|
-
ActiveRecord::Base.connection.table_exists?('
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
ActiveTenant.current.create '
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
ActiveRecord::Base.connection.table_exists?('
|
113
|
-
ActiveRecord::Base.connection.table_exists?('
|
114
|
-
ActiveRecord::Base.connection.table_exists?('
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
ActiveRecord::Base.connection.table_exists?('
|
120
|
-
ActiveRecord::Base.connection.table_exists?('
|
121
|
-
ActiveRecord::Base.connection.table_exists?('
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
ActiveTenant.current.remove '
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
ActiveTenant.current.create '
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
ActiveRecord::Base.connection.table_exists?('
|
137
|
-
ActiveRecord::Base.connection.table_exists?('
|
138
|
-
ActiveRecord::Base.connection.table_exists?('
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
ActiveRecord::Base.connection.table_exists?('
|
144
|
-
ActiveRecord::Base.connection.table_exists?('
|
145
|
-
ActiveRecord::Base.connection.table_exists?('
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
ActiveTenant.current.remove '
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
ActiveRecord::Base.connection.table_exists?('
|
160
|
-
ActiveRecord::Base.connection.table_exists?('
|
161
|
-
ActiveRecord::Base.connection.table_exists?('
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
ActiveRecord::Base.connection.table_exists?('
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
ActiveRecord::Base.
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
ActiveRecord::Base.connection.table_exists?('
|
194
|
-
ActiveRecord::Base.connection.table_exists?('
|
195
|
-
ActiveRecord::Base.connection.table_exists?('
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
ActiveRecord::Base.
|
200
|
-
|
201
|
-
|
202
|
-
ActiveRecord::Base.connection.table_exists?('
|
203
|
-
ActiveRecord::Base.connection.table_exists?('
|
204
|
-
ActiveRecord::Base.connection.table_exists?('
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
ActiveRecord::Base.
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
ActiveTenant::Base::ADAPTERS.each do |adapter_name, adapter_class|
|
4
|
+
|
5
|
+
describe adapter_class do
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
AdapterTestHelper.before_all adapter_name
|
9
|
+
end
|
10
|
+
|
11
|
+
after :all do
|
12
|
+
AdapterTestHelper.after_all adapter_name
|
13
|
+
end
|
14
|
+
|
15
|
+
before :each do
|
16
|
+
AdapterTestHelper.before_each adapter_name
|
17
|
+
end
|
18
|
+
|
19
|
+
after :each do
|
20
|
+
AdapterTestHelper.after_each adapter_name
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'Specific adapter' do
|
24
|
+
|
25
|
+
let(:tenant_adapter) { adapter_class.new }
|
26
|
+
|
27
|
+
it 'List all tenants' do
|
28
|
+
tenants = tenant_adapter.all
|
29
|
+
|
30
|
+
tenants.should be_a Array
|
31
|
+
tenants.should have(:no).items
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'Create a new tenant' do
|
35
|
+
tenant_adapter.create 'new_tenant'
|
36
|
+
tenants = tenant_adapter.all
|
37
|
+
|
38
|
+
tenants.should be_a Array
|
39
|
+
tenants.should have(1).items
|
40
|
+
tenants.should include 'new_tenant'
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'Remove an existing tenant' do
|
44
|
+
tenant_adapter.create 'tenant_to_remove'
|
45
|
+
|
46
|
+
tenant_adapter.all.should include 'tenant_to_remove'
|
47
|
+
|
48
|
+
tenant_adapter.remove 'tenant_to_remove'
|
49
|
+
|
50
|
+
tenant_adapter.all.should_not include 'tenant_to_remove'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'Evaluate a block into a tenant' do
|
54
|
+
tenant_adapter.name.should eq tenant_adapter.global
|
55
|
+
|
56
|
+
tenant_adapter.create 'dummy'
|
57
|
+
|
58
|
+
tenant_adapter.name.should eq tenant_adapter.global
|
59
|
+
|
60
|
+
tenant_adapter.with 'dummy' do
|
61
|
+
tenant_adapter.name.should eq 'dummy'
|
62
|
+
end
|
63
|
+
|
64
|
+
tenant_adapter.name.should eq tenant_adapter.global
|
65
|
+
|
66
|
+
tenant_adapter.remove 'dummy'
|
67
|
+
|
68
|
+
tenant_adapter.name.should eq tenant_adapter.global
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'Global adapter' do
|
74
|
+
|
75
|
+
it 'Adapter operations' do
|
76
|
+
ActiveTenant.current.create 'dummy'
|
77
|
+
ActiveTenant.current.all.should include 'dummy'
|
78
|
+
|
79
|
+
ActiveTenant.current.name.should eq ActiveTenant.current.global
|
80
|
+
|
81
|
+
ActiveTenant.current.with 'dummy' do
|
82
|
+
ActiveTenant.current.name.should eq 'dummy'
|
83
|
+
end
|
84
|
+
|
85
|
+
ActiveTenant.current.name.should eq ActiveTenant.current.global
|
86
|
+
|
87
|
+
ActiveTenant.current.remove 'dummy'
|
88
|
+
ActiveTenant.current.all.should_not include 'dummy'
|
89
|
+
end
|
90
|
+
|
91
|
+
it 'Migrate global' do
|
92
|
+
ActiveTenant.current.create 'dummy'
|
93
|
+
|
94
|
+
ActiveTenant.current.migrate_global
|
95
|
+
|
96
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_true
|
97
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_false
|
98
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
|
99
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
100
|
+
|
101
|
+
ActiveTenant.current.remove 'dummy'
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'Migrate one tenant' do
|
105
|
+
ActiveTenant.current.create 'dummy_1'
|
106
|
+
ActiveTenant.current.create 'dummy_2'
|
107
|
+
|
108
|
+
ActiveTenant.current.migrate 'dummy_1'
|
109
|
+
|
110
|
+
ActiveTenant.current.with 'dummy_1' do
|
111
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
112
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
113
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
|
114
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
115
|
+
end
|
116
|
+
|
117
|
+
ActiveTenant.current.with 'dummy_2' do
|
118
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
119
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_false
|
120
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
|
121
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
122
|
+
end
|
123
|
+
|
124
|
+
ActiveTenant.current.remove 'dummy_1'
|
125
|
+
ActiveTenant.current.remove 'dummy_2'
|
126
|
+
end
|
127
|
+
|
128
|
+
it 'Migrate all tenants' do
|
129
|
+
ActiveTenant.current.create 'dummy_1'
|
130
|
+
ActiveTenant.current.create 'dummy_2'
|
131
|
+
|
132
|
+
ActiveTenant.current.migrate_all
|
133
|
+
|
134
|
+
ActiveTenant.current.with 'dummy_1' do
|
135
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
136
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
137
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
|
138
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
139
|
+
end
|
140
|
+
|
141
|
+
ActiveTenant.current.with 'dummy_2' do
|
142
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
143
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
144
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
|
145
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
146
|
+
end
|
147
|
+
|
148
|
+
ActiveTenant.current.remove 'dummy_1'
|
149
|
+
ActiveTenant.current.remove 'dummy_2'
|
150
|
+
end
|
151
|
+
|
152
|
+
it 'Migrate custom tenant' do
|
153
|
+
ActiveTenant.current.create 'custom'
|
154
|
+
|
155
|
+
ActiveTenant.current.migrate 'custom'
|
156
|
+
|
157
|
+
ActiveTenant.current.with 'custom' do
|
158
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
159
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
160
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
|
161
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_true
|
162
|
+
end
|
163
|
+
|
164
|
+
ActiveTenant.current.remove 'custom'
|
165
|
+
end
|
166
|
+
|
167
|
+
it 'Migrate to specific version' do
|
168
|
+
ActiveTenant.current.create 'dummy'
|
169
|
+
|
170
|
+
ActiveTenant.current.migrate_all 20120823132854
|
171
|
+
|
172
|
+
ActiveTenant.current.with 'dummy' do
|
173
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
174
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
|
175
|
+
end
|
176
|
+
|
177
|
+
ActiveTenant.current.remove 'dummy'
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
181
|
+
|
182
|
+
context 'ActiveRecord extensions' do
|
183
|
+
|
184
|
+
it 'Create, migrate and remove' do
|
185
|
+
ActiveRecord::Base.tenant?.should be_false
|
186
|
+
ActiveRecord::Base.tenant_name.should be_nil
|
187
|
+
|
188
|
+
ActiveRecord::Base.create_tenant 'dummy'
|
189
|
+
|
190
|
+
ActiveRecord::Migration.migrate_all
|
191
|
+
|
192
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_true
|
193
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_false
|
194
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_false
|
195
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
196
|
+
|
197
|
+
ActiveRecord::Base.with_tenant 'dummy' do
|
198
|
+
ActiveRecord::Base.tenant?.should be_true
|
199
|
+
ActiveRecord::Base.tenant_name.should eq 'dummy'
|
200
|
+
|
201
|
+
ActiveRecord::Base.connection.table_exists?('globals').should be_false
|
202
|
+
ActiveRecord::Base.connection.table_exists?('tenants').should be_true
|
203
|
+
ActiveRecord::Base.connection.table_exists?('other_tenants').should be_true
|
204
|
+
ActiveRecord::Base.connection.table_exists?('customs').should be_false
|
205
|
+
end
|
206
|
+
|
207
|
+
ActiveRecord::Base.remove_tenant 'dummy'
|
208
|
+
|
209
|
+
ActiveRecord::Base.tenant?.should be_false
|
210
|
+
ActiveRecord::Base.tenant_name.should be_nil
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
context 'Models' do
|
216
|
+
|
217
|
+
it 'Globals always visible' do
|
218
|
+
ActiveTenant.current.create 'dummy_1'
|
219
|
+
ActiveTenant.current.create 'dummy_2'
|
220
|
+
|
221
|
+
ActiveTenant.current.migrate_global
|
222
|
+
ActiveTenant.current.migrate_all
|
223
|
+
|
224
|
+
Global.count.should eq 0
|
225
|
+
|
226
|
+
ActiveTenant.current.with('dummy_1') do
|
227
|
+
Tenant.count.should eq 0
|
228
|
+
end
|
229
|
+
|
230
|
+
ActiveTenant.current.with('dummy_2') do
|
231
|
+
Tenant.create! key: '1', value: 'dummy_2'
|
232
|
+
Global.create! key: '2', value: 'global'
|
233
|
+
|
234
|
+
Tenant.count.should eq 1
|
235
|
+
Global.count.should eq 1
|
236
|
+
end
|
237
|
+
|
238
|
+
Global.count.should eq 1
|
239
|
+
|
240
|
+
ActiveTenant.current.with('dummy_1') do
|
241
|
+
Tenant.count.should eq 0
|
242
|
+
Global.count.should eq 1
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
end
|
247
|
+
|
248
|
+
end
|
249
|
+
|
218
250
|
end
|
@@ -1,10 +1,10 @@
|
|
1
|
-
class CreateGlobals < ActiveRecord::Migration
|
2
|
-
def change
|
3
|
-
create_table :globals do |t|
|
4
|
-
t.string :key, null: false
|
5
|
-
t.string :value, null: false
|
6
|
-
|
7
|
-
t.timestamps
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
1
|
+
class CreateGlobals < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :globals do |t|
|
4
|
+
t.string :key, null: false
|
5
|
+
t.string :value, null: false
|
6
|
+
|
7
|
+
t.timestamps
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
class CreateTenants < ActiveRecord::Migration
|
2
|
-
tenant :all
|
3
|
-
|
4
|
-
def change
|
5
|
-
create_table :tenants do |t|
|
6
|
-
t.string :key, null: false
|
7
|
-
t.string :value, null: false
|
8
|
-
|
9
|
-
t.timestamps
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
class CreateTenants < ActiveRecord::Migration
|
2
|
+
tenant :all
|
3
|
+
|
4
|
+
def change
|
5
|
+
create_table :tenants do |t|
|
6
|
+
t.string :key, null: false
|
7
|
+
t.string :value, null: false
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
class CreateOtherTenants < ActiveRecord::Migration
|
2
|
-
tenant :all
|
3
|
-
|
4
|
-
def change
|
5
|
-
create_table :other_tenants do |t|
|
6
|
-
t.string :key, null: false
|
7
|
-
t.string :value, null: false
|
8
|
-
|
9
|
-
t.timestamps
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
class CreateOtherTenants < ActiveRecord::Migration
|
2
|
+
tenant :all
|
3
|
+
|
4
|
+
def change
|
5
|
+
create_table :other_tenants do |t|
|
6
|
+
t.string :key, null: false
|
7
|
+
t.string :value, null: false
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,12 +1,12 @@
|
|
1
|
-
class CreateCustoms < ActiveRecord::Migration
|
2
|
-
tenant :custom
|
3
|
-
|
4
|
-
def change
|
5
|
-
create_table :customs do |t|
|
6
|
-
t.string :key, null: false
|
7
|
-
t.string :value, null: false
|
8
|
-
|
9
|
-
t.timestamps
|
10
|
-
end
|
11
|
-
end
|
12
|
-
end
|
1
|
+
class CreateCustoms < ActiveRecord::Migration
|
2
|
+
tenant :custom
|
3
|
+
|
4
|
+
def change
|
5
|
+
create_table :customs do |t|
|
6
|
+
t.string :key, null: false
|
7
|
+
t.string :value, null: false
|
8
|
+
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/spec/models.rb
ADDED
data/spec/spec_helper.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require 'active_tenant'
|
2
|
-
|
3
|
-
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
RSpec.configure do |config|
|
1
|
+
require 'active_tenant'
|
2
|
+
|
3
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
4
|
+
|
5
|
+
ActiveRecord::Base.logger = Logger.new($stdout)
|
6
|
+
ActiveRecord::Migrator.migrations_path = "#{File.dirname(__FILE__)}/migrations"
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
11
9
|
end
|