apartment 0.18.0 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY.md +7 -0
- data/README.md +2 -2
- data/apartment.gemspec +2 -2
- data/lib/apartment.rb +13 -6
- data/lib/apartment/adapters/abstract_adapter.rb +7 -4
- data/lib/apartment/adapters/postgis_adapter.rb +15 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +5 -3
- data/lib/apartment/database.rb +2 -2
- data/lib/apartment/elevators/generic.rb +1 -1
- data/lib/apartment/elevators/subdomain.rb +23 -1
- data/lib/apartment/railtie.rb +1 -1
- data/lib/apartment/version.rb +1 -1
- data/lib/generators/apartment/install/templates/apartment.rb +1 -1
- data/spec/adapters/postgresql_adapter_spec.rb +2 -2
- data/spec/database_spec.rb +12 -2
- data/spec/examples/generic_adapter_examples.rb +1 -0
- data/spec/examples/schema_adapter_examples.rb +2 -0
- data/spec/integration/delayed_job_integration_spec.rb +1 -1
- data/spec/integration/middleware/first_subdomain_elevator_spec.rb +1 -1
- data/spec/support/apartment_helpers.rb +1 -1
- data/spec/support/contexts.rb +1 -1
- data/spec/unit/config_spec.rb +3 -3
- data/spec/unit/migrator_spec.rb +1 -1
- metadata +9 -8
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -164,7 +164,7 @@ of dbs to Apartment. You can make this dynamic by providing a Proc object to be
|
|
164
164
|
This object should yield an array of string representing each database name. Example:
|
165
165
|
|
166
166
|
# Dynamically get database names to migrate
|
167
|
-
config.database_names = lambda{ Customer.
|
167
|
+
config.database_names = lambda{ Customer.pluck(:database_name) }
|
168
168
|
|
169
169
|
# Use a static list of database names for migrate
|
170
170
|
config.database_names = ['db1', 'db2']
|
@@ -234,4 +234,4 @@ All jobs *must* stored in the global (public) namespace, so add it to the list o
|
|
234
234
|
|
235
235
|
## License
|
236
236
|
|
237
|
-
Apartment is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
237
|
+
Apartment is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/apartment.gemspec
CHANGED
@@ -25,8 +25,8 @@ Gem::Specification.new do |s|
|
|
25
25
|
s.add_development_dependency 'rails', '>= 3.1.2'
|
26
26
|
s.add_development_dependency 'rake', '~> 0.9.2'
|
27
27
|
s.add_development_dependency 'sqlite3'
|
28
|
-
s.add_development_dependency 'rspec', '~> 2.
|
29
|
-
s.add_development_dependency 'rspec-rails', '~> 2.
|
28
|
+
s.add_development_dependency 'rspec', '~> 2.11'
|
29
|
+
s.add_development_dependency 'rspec-rails', '~> 2.11'
|
30
30
|
s.add_development_dependency 'capybara', '~> 1.0.0'
|
31
31
|
s.add_development_dependency 'pg', '>= 0.11.0'
|
32
32
|
s.add_development_dependency 'mysql2', '~> 0.3.10'
|
data/lib/apartment.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'apartment/railtie' if defined?(Rails)
|
2
|
+
require 'active_support/core_ext/module/delegation'
|
3
|
+
require 'active_support/core_ext/object/blank'
|
2
4
|
|
3
5
|
module Apartment
|
4
6
|
|
5
7
|
class << self
|
6
|
-
ACCESSOR_METHODS = [:
|
8
|
+
ACCESSOR_METHODS = [:use_schemas, :seed_after_create, :prepend_environment, :append_environment]
|
7
9
|
WRITER_METHODS = [:database_names, :excluded_models, :default_schema, :persistent_schemas, :connection_class]
|
8
10
|
|
9
11
|
attr_accessor(*ACCESSOR_METHODS)
|
@@ -16,11 +18,6 @@ module Apartment
|
|
16
18
|
yield self if block_given?
|
17
19
|
end
|
18
20
|
|
19
|
-
# Default switch schema to public
|
20
|
-
def schema_to_switch
|
21
|
-
@schema_to_switch || "public"
|
22
|
-
end
|
23
|
-
|
24
21
|
# Be careful not to use `return` here so both Proc and lambda can be used without breaking
|
25
22
|
def database_names
|
26
23
|
@database_names.respond_to?(:call) ? @database_names.call : @database_names
|
@@ -48,6 +45,16 @@ module Apartment
|
|
48
45
|
(ACCESSOR_METHODS + WRITER_METHODS).each{|method| instance_variable_set(:"@#{method}", nil) }
|
49
46
|
end
|
50
47
|
|
48
|
+
def use_postgres_schemas
|
49
|
+
warn "[Deprecation Warning] `use_postgresql_schemas` is now deprecated, please use `use_schemas`"
|
50
|
+
use_schemas
|
51
|
+
end
|
52
|
+
|
53
|
+
def use_postgres_schemas=(to_use_or_not_to_use)
|
54
|
+
warn "[Deprecation Warning] `use_postgresql_schemas=` is now deprecated, please use `use_schemas=`"
|
55
|
+
self.use_schemas = to_use_or_not_to_use
|
56
|
+
end
|
57
|
+
|
51
58
|
end
|
52
59
|
|
53
60
|
autoload :Database, 'apartment/database'
|
@@ -8,11 +8,9 @@ module Apartment
|
|
8
8
|
|
9
9
|
# @constructor
|
10
10
|
# @param {Hash} config Database config
|
11
|
-
# @param {Hash} defaults Some default options
|
12
11
|
#
|
13
|
-
def initialize(config
|
12
|
+
def initialize(config)
|
14
13
|
@config = config
|
15
|
-
@defaults = defaults
|
16
14
|
end
|
17
15
|
|
18
16
|
# Create a new database, import schema, seed if appropriate
|
@@ -39,7 +37,12 @@ module Apartment
|
|
39
37
|
def current_database
|
40
38
|
Apartment.connection.current_database
|
41
39
|
end
|
42
|
-
|
40
|
+
|
41
|
+
# Note alias_method here doesn't work with inheritence apparently ??
|
42
|
+
#
|
43
|
+
def current
|
44
|
+
current_database
|
45
|
+
end
|
43
46
|
|
44
47
|
# Drop the database
|
45
48
|
#
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# handle postgis adapter as if it were postgresql,
|
2
|
+
# only override the adapter_method used for initialization
|
3
|
+
require "apartment/adapters/postgresql_adapter"
|
4
|
+
|
5
|
+
module Apartment
|
6
|
+
|
7
|
+
module Database
|
8
|
+
|
9
|
+
def self.postgis_adapter(config)
|
10
|
+
Apartment.use_schemas ?
|
11
|
+
Adapters::PostgresqlSchemaAdapter.new(config) :
|
12
|
+
Adapters::PostgresqlAdapter.new(config)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -3,7 +3,7 @@ module Apartment
|
|
3
3
|
module Database
|
4
4
|
|
5
5
|
def self.postgresql_adapter(config)
|
6
|
-
Apartment.
|
6
|
+
Apartment.use_schemas ?
|
7
7
|
Adapters::PostgresqlSchemaAdapter.new(config) :
|
8
8
|
Adapters::PostgresqlAdapter.new(config)
|
9
9
|
end
|
@@ -33,8 +33,6 @@ module Apartment
|
|
33
33
|
# Separate Adapter for Postgresql when using schemas
|
34
34
|
class PostgresqlSchemaAdapter < AbstractAdapter
|
35
35
|
|
36
|
-
attr_reader :current_database
|
37
|
-
|
38
36
|
# Drop the database schema
|
39
37
|
#
|
40
38
|
# @param {String} database Database (schema) to drop
|
@@ -81,6 +79,10 @@ module Apartment
|
|
81
79
|
Apartment.connection.schema_search_path = full_search_path
|
82
80
|
end
|
83
81
|
|
82
|
+
def current_database
|
83
|
+
@current_database || Apartment.default_schema
|
84
|
+
end
|
85
|
+
|
84
86
|
protected
|
85
87
|
|
86
88
|
# Set schema search path to new schema
|
data/lib/apartment/database.rb
CHANGED
@@ -23,7 +23,7 @@ module Apartment
|
|
23
23
|
# @return {subclass of Apartment::AbstractAdapter}
|
24
24
|
#
|
25
25
|
def adapter
|
26
|
-
|
26
|
+
Thread.current[:apartment_adapter] ||= begin
|
27
27
|
adapter_method = "#{config[:adapter]}_adapter"
|
28
28
|
|
29
29
|
begin
|
@@ -43,7 +43,7 @@ module Apartment
|
|
43
43
|
# Reset config and adapter so they are regenerated
|
44
44
|
#
|
45
45
|
def reload!
|
46
|
-
|
46
|
+
Thread.current[:apartment_adapter] = nil
|
47
47
|
@config = nil
|
48
48
|
end
|
49
49
|
|
@@ -6,7 +6,29 @@ module Apartment
|
|
6
6
|
class Subdomain < Generic
|
7
7
|
|
8
8
|
def parse_database_name(request)
|
9
|
-
|
9
|
+
database = subdomain(request.host)
|
10
|
+
|
11
|
+
database.present? && database || nil
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
# *Almost* a direct ripoff of ActionDispatch::Request subdomain methods
|
17
|
+
|
18
|
+
# Only care about the first subdomain for the database name
|
19
|
+
def subdomain(host)
|
20
|
+
subdomains(host).first
|
21
|
+
end
|
22
|
+
|
23
|
+
# Assuming tld_length of 1, might need to make this configurable in Apartment in the future for things like .co.uk
|
24
|
+
def subdomains(host, tld_length = 1)
|
25
|
+
return [] unless named_host?(host)
|
26
|
+
|
27
|
+
host.split('.')[0..-(tld_length + 2)]
|
28
|
+
end
|
29
|
+
|
30
|
+
def named_host?(host)
|
31
|
+
!(host.nil? || /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.match(host))
|
10
32
|
end
|
11
33
|
end
|
12
34
|
end
|
data/lib/apartment/railtie.rb
CHANGED
@@ -10,7 +10,7 @@ module Apartment
|
|
10
10
|
config.before_initialize do
|
11
11
|
Apartment.configure do |config|
|
12
12
|
config.excluded_models = []
|
13
|
-
config.
|
13
|
+
config.use_schemas = true
|
14
14
|
config.database_names = []
|
15
15
|
config.seed_after_create = false
|
16
16
|
config.prepend_environment = false
|
data/lib/apartment/version.rb
CHANGED
@@ -8,7 +8,7 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
8
8
|
|
9
9
|
context "using schemas" do
|
10
10
|
|
11
|
-
before{ Apartment.
|
11
|
+
before{ Apartment.use_schemas = true }
|
12
12
|
|
13
13
|
# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
|
14
14
|
def database_names
|
@@ -23,7 +23,7 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
23
23
|
|
24
24
|
context "using databases" do
|
25
25
|
|
26
|
-
before{ Apartment.
|
26
|
+
before{ Apartment.use_schemas = false }
|
27
27
|
|
28
28
|
# Not sure why, but somehow using let(:database_names) memoizes for the whole example group, not just each test
|
29
29
|
def database_names
|
data/spec/database_spec.rb
CHANGED
@@ -55,7 +55,7 @@ describe Apartment::Database do
|
|
55
55
|
let(:database2){ Apartment::Test.next_db }
|
56
56
|
|
57
57
|
before do
|
58
|
-
Apartment.
|
58
|
+
Apartment.use_schemas = true
|
59
59
|
ActiveRecord::Base.establish_connection config
|
60
60
|
Apartment::Test.load_schema # load the Rails schema in the public db schema
|
61
61
|
subject.stub(:config).and_return config # Use postgresql database config for this test
|
@@ -79,6 +79,16 @@ describe Apartment::Database do
|
|
79
79
|
}.to raise_error
|
80
80
|
end
|
81
81
|
|
82
|
+
context "threadsafety" do
|
83
|
+
before { subject.create database }
|
84
|
+
|
85
|
+
it 'has a threadsafe adapter' do
|
86
|
+
subject.switch(database)
|
87
|
+
thread = Thread.new { subject.current_database.should == Apartment.default_schema }
|
88
|
+
thread.join
|
89
|
+
subject.current_database.should == database
|
90
|
+
end
|
91
|
+
end
|
82
92
|
end
|
83
93
|
|
84
94
|
context "with schemas" do
|
@@ -86,7 +96,7 @@ describe Apartment::Database do
|
|
86
96
|
before do
|
87
97
|
Apartment.configure do |config|
|
88
98
|
config.excluded_models = []
|
89
|
-
config.
|
99
|
+
config.use_schemas = true
|
90
100
|
config.seed_after_create = true
|
91
101
|
end
|
92
102
|
subject.create database
|
@@ -204,12 +204,14 @@ shared_examples_for "a schema based apartment adapter" do
|
|
204
204
|
it "should return the current schema name" do
|
205
205
|
subject.switch(schema1)
|
206
206
|
subject.current_database.should == schema1
|
207
|
+
subject.current.should == schema1
|
207
208
|
end
|
208
209
|
|
209
210
|
context "persistent_schemas", :persistent_schemas => true do
|
210
211
|
it "should exlude persistent_schemas" do
|
211
212
|
subject.switch(schema1)
|
212
213
|
subject.current_database.should == schema1
|
214
|
+
subject.current.should == schema1
|
213
215
|
end
|
214
216
|
end
|
215
217
|
end
|
@@ -16,7 +16,7 @@ describe Apartment::Delayed do
|
|
16
16
|
Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
|
17
17
|
|
18
18
|
Apartment.configure do |config|
|
19
|
-
config.
|
19
|
+
config.use_schemas = true
|
20
20
|
end
|
21
21
|
|
22
22
|
Apartment::Database.create database
|
@@ -3,7 +3,7 @@ require 'spec_helper'
|
|
3
3
|
describe Apartment::Elevators::FirstSubdomain do
|
4
4
|
describe "subdomain" do
|
5
5
|
subject { described_class.new("test").parse_database_name(request) }
|
6
|
-
let(:request) { double(:request, :
|
6
|
+
let(:request) { double(:request, :host => "#{subdomain}.example.com") }
|
7
7
|
|
8
8
|
context "one subdomain" do
|
9
9
|
let(:subdomain) { "test" }
|
data/spec/support/contexts.rb
CHANGED
@@ -28,7 +28,7 @@ shared_context "elevators", :elevator => true do
|
|
28
28
|
before do
|
29
29
|
Apartment.reset # reset all config
|
30
30
|
Apartment.seed_after_create = false
|
31
|
-
Apartment.
|
31
|
+
Apartment.use_schemas = true
|
32
32
|
api.reload! # reload adapter
|
33
33
|
|
34
34
|
api.create(database1)
|
data/spec/unit/config_spec.rb
CHANGED
@@ -20,12 +20,12 @@ describe Apartment do
|
|
20
20
|
Apartment.excluded_models.should == excluded_models
|
21
21
|
end
|
22
22
|
|
23
|
-
it "should set
|
23
|
+
it "should set use_schemas" do
|
24
24
|
Apartment.configure do |config|
|
25
25
|
config.excluded_models = []
|
26
|
-
config.
|
26
|
+
config.use_schemas = false
|
27
27
|
end
|
28
|
-
Apartment.
|
28
|
+
Apartment.use_schemas.should be_false
|
29
29
|
end
|
30
30
|
|
31
31
|
it "should set seed_after_create" do
|
data/spec/unit/migrator_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe Apartment::Migrator do
|
|
12
12
|
@original_schema = ActiveRecord::Base.connection.schema_search_path
|
13
13
|
|
14
14
|
Apartment.configure do |config|
|
15
|
-
config.
|
15
|
+
config.use_schemas = true
|
16
16
|
config.excluded_models = []
|
17
17
|
config.database_names = [schema_name]
|
18
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.
|
4
|
+
version: 0.19.0
|
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-
|
13
|
+
date: 2012-12-30 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -115,7 +115,7 @@ dependencies:
|
|
115
115
|
requirements:
|
116
116
|
- - ~>
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
-
version: 2.
|
118
|
+
version: '2.11'
|
119
119
|
type: :development
|
120
120
|
prerelease: false
|
121
121
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -123,7 +123,7 @@ dependencies:
|
|
123
123
|
requirements:
|
124
124
|
- - ~>
|
125
125
|
- !ruby/object:Gem::Version
|
126
|
-
version: 2.
|
126
|
+
version: '2.11'
|
127
127
|
- !ruby/object:Gem::Dependency
|
128
128
|
name: rspec-rails
|
129
129
|
requirement: !ruby/object:Gem::Requirement
|
@@ -131,7 +131,7 @@ dependencies:
|
|
131
131
|
requirements:
|
132
132
|
- - ~>
|
133
133
|
- !ruby/object:Gem::Version
|
134
|
-
version: 2.
|
134
|
+
version: '2.11'
|
135
135
|
type: :development
|
136
136
|
prerelease: false
|
137
137
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -139,7 +139,7 @@ dependencies:
|
|
139
139
|
requirements:
|
140
140
|
- - ~>
|
141
141
|
- !ruby/object:Gem::Version
|
142
|
-
version: 2.
|
142
|
+
version: '2.11'
|
143
143
|
- !ruby/object:Gem::Dependency
|
144
144
|
name: capybara
|
145
145
|
requirement: !ruby/object:Gem::Requirement
|
@@ -244,6 +244,7 @@ files:
|
|
244
244
|
- lib/apartment.rb
|
245
245
|
- lib/apartment/adapters/abstract_adapter.rb
|
246
246
|
- lib/apartment/adapters/mysql2_adapter.rb
|
247
|
+
- lib/apartment/adapters/postgis_adapter.rb
|
247
248
|
- lib/apartment/adapters/postgresql_adapter.rb
|
248
249
|
- lib/apartment/console.rb
|
249
250
|
- lib/apartment/database.rb
|
@@ -342,7 +343,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
342
343
|
version: '0'
|
343
344
|
segments:
|
344
345
|
- 0
|
345
|
-
hash: -
|
346
|
+
hash: -3842088920209676639
|
346
347
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
347
348
|
none: false
|
348
349
|
requirements:
|
@@ -351,7 +352,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
351
352
|
version: '0'
|
352
353
|
segments:
|
353
354
|
- 0
|
354
|
-
hash: -
|
355
|
+
hash: -3842088920209676639
|
355
356
|
requirements: []
|
356
357
|
rubyforge_project:
|
357
358
|
rubygems_version: 1.8.24
|