apartment 0.17.1 → 0.17.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -44,7 +44,7 @@ namespace :postgres do
44
44
 
45
45
  desc 'Build the PostgreSQL test databases'
46
46
  task :build_db do
47
- %x{ createdb -E UTF8 #{pg_config['database']} -Upostgres } rescue "test db already exists"
47
+ %x{ createdb -E UTF8 #{pg_config['database']} -U#{pg_config['username']} } rescue "test db already exists"
48
48
  ActiveRecord::Base.establish_connection pg_config
49
49
  ActiveRecord::Migrator.migrate('spec/dummy/db/migrate')
50
50
  end
@@ -52,7 +52,7 @@ namespace :postgres do
52
52
  desc "drop the PostgreSQL test database"
53
53
  task :drop_db do
54
54
  puts "dropping database #{pg_config['database']}"
55
- %x{ dropdb #{pg_config['database']} -Upostgres }
55
+ %x{ dropdb #{pg_config['database']} -U#{pg_config['username']} }
56
56
  end
57
57
 
58
58
  end
@@ -63,7 +63,7 @@ namespace :mysql do
63
63
 
64
64
  desc 'Build the MySQL test databases'
65
65
  task :build_db do
66
- %x{ mysqladmin -u root create #{my_config['database']} } rescue "test db already exists"
66
+ %x{ mysqladmin -u #{my_config['username']} create #{my_config['database']} } rescue "test db already exists"
67
67
  ActiveRecord::Base.establish_connection my_config
68
68
  ActiveRecord::Migrator.migrate('spec/dummy/db/migrate')
69
69
  end
@@ -71,7 +71,7 @@ namespace :mysql do
71
71
  desc "drop the MySQL test database"
72
72
  task :drop_db do
73
73
  puts "dropping database #{my_config['database']}"
74
- %x{ mysqladmin -u root drop #{my_config['database']} --force}
74
+ %x{ mysqladmin -u #{my_config['username']} drop #{my_config['database']} --force}
75
75
  end
76
76
 
77
77
  end
data/lib/apartment.rb CHANGED
@@ -14,6 +14,11 @@ module Apartment
14
14
  yield self if block_given?
15
15
  end
16
16
 
17
+ # Default switch schema to public
18
+ def schema_to_switch
19
+ @schema_to_switch || "public"
20
+ end
21
+
17
22
  # Be careful not to use `return` here so both Proc and lambda can be used without breaking
18
23
  def database_names
19
24
  @database_names.respond_to?(:call) ? @database_names.call : @database_names
@@ -50,7 +55,8 @@ module Apartment
50
55
 
51
56
  module Elevators
52
57
  autoload :Generic, 'apartment/elevators/generic'
53
- autoload :Subdomain, 'apartment/elevators/subdomain'
58
+ autoload :Subdomain, 'apartment/elevators/subdomain'
59
+ autoload :FirstSubdomain, 'apartment/elevators/first_subdomain'
54
60
  autoload :Domain, 'apartment/elevators/domain'
55
61
  end
56
62
 
@@ -92,7 +92,7 @@ module Apartment
92
92
  ActiveRecord::Base.connection.schema_search_path = full_search_path
93
93
 
94
94
  rescue ActiveRecord::StatementInvalid
95
- raise SchemaNotFound, "The schema #{database.inspect} cannot be found."
95
+ raise SchemaNotFound, "One of the following schema(s) is invalid: #{new_search_path}"
96
96
  end
97
97
 
98
98
  # Create the new schema
@@ -0,0 +1,13 @@
1
+ module Apartment
2
+ module Elevators
3
+ # Provides a rack based db switching solution based on subdomains
4
+ # Assumes that database name should match subdomain
5
+ class FirstSubdomain < Subdomain
6
+
7
+ def parse_database_name(request)
8
+ super(request).match(/(\w+)(\.\w+)?/)[1]
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Apartment
2
- VERSION = "0.17.1"
2
+ VERSION = "0.17.2"
3
3
  end
@@ -148,7 +148,50 @@ shared_examples_for "a schema based apartment adapter" do
148
148
 
149
149
  it "should reset connection if database is nil" do
150
150
  subject.switch
151
- connection.schema_search_path.should == public_schema
151
+ connection.schema_search_path.should include public_schema
152
+ end
153
+
154
+ describe "other schemas in search path" do
155
+ let(:other_schema) { "other_schema" }
156
+ before do
157
+ subject.create(other_schema)
158
+ @old_schema = subject.instance_variable_get(:@defaults)[:schema_search_path]
159
+ subject.instance_variable_get(:@defaults)[:schema_search_path] += "," + other_schema
160
+ end
161
+
162
+ it "should maintain other schemas on switch" do
163
+ subject.switch(schema1)
164
+ connection.schema_search_path.should include other_schema
165
+ end
166
+
167
+ after do
168
+ subject.instance_variable_get(:@defaults)[:schema_search_path] = @old_schema
169
+ subject.drop(other_schema)
170
+ end
171
+
172
+ describe "with schema_to_switch specified" do
173
+ before do
174
+ Apartment.schema_to_switch = other_schema
175
+ subject.switch(schema1)
176
+ end
177
+
178
+ after do
179
+ # Reset the switch schema.
180
+ Apartment.schema_to_switch = nil
181
+ end
182
+
183
+ it "should switch out the schema to switch rather than public" do
184
+ connection.schema_search_path.should_not include other_schema
185
+ end
186
+
187
+ it "should retain the public schema" do
188
+ connection.schema_search_path.should include "public"
189
+ end
190
+
191
+ it "should still switch to the switched schema" do
192
+ connection.schema_search_path.should include schema1
193
+ end
194
+ end
152
195
  end
153
196
 
154
197
  it "should raise an error if schema is invalid" do
@@ -214,4 +257,6 @@ shared_examples_for "a schema based apartment adapter" do
214
257
  end
215
258
  end
216
259
 
260
+
261
+
217
262
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apartment::Elevators::FirstSubdomain do
4
+ describe "subdomain" do
5
+ subject { described_class.new("test").parse_database_name(request) }
6
+ let(:request) { double(:request, :subdomain => subdomain)}
7
+
8
+ context "one subdomain" do
9
+ let(:subdomain) { "test" }
10
+ it { should == "test" }
11
+ end
12
+
13
+ context "nested subdomains" do
14
+ let(:subdomain) { "test1.test2" }
15
+ it { should == "test1" }
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apartment
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.1
4
+ version: 0.17.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-30 00:00:00.000000000 Z
13
+ date: 2012-11-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -251,6 +251,7 @@ files:
251
251
  - lib/apartment/delayed_job/hooks.rb
252
252
  - lib/apartment/delayed_job/requirements.rb
253
253
  - lib/apartment/elevators/domain.rb
254
+ - lib/apartment/elevators/first_subdomain.rb
254
255
  - lib/apartment/elevators/generic.rb
255
256
  - lib/apartment/elevators/subdomain.rb
256
257
  - lib/apartment/migrator.rb
@@ -310,6 +311,7 @@ files:
310
311
  - spec/integration/apartment_rake_integration_spec.rb
311
312
  - spec/integration/delayed_job_integration_spec.rb
312
313
  - spec/integration/middleware/domain_elevator_spec.rb
314
+ - spec/integration/middleware/first_subdomain_elevator_spec.rb
313
315
  - spec/integration/middleware/generic_elevator_spec.rb
314
316
  - spec/integration/middleware/subdomain_elevator_spec.rb
315
317
  - spec/spec_helper.rb
@@ -339,7 +341,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
339
341
  version: '0'
340
342
  segments:
341
343
  - 0
342
- hash: -4170403971247658822
344
+ hash: 3706391324035318111
343
345
  required_rubygems_version: !ruby/object:Gem::Requirement
344
346
  none: false
345
347
  requirements:
@@ -348,7 +350,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
348
350
  version: '0'
349
351
  segments:
350
352
  - 0
351
- hash: -4170403971247658822
353
+ hash: 3706391324035318111
352
354
  requirements: []
353
355
  rubyforge_project:
354
356
  rubygems_version: 1.8.24