apartment 0.17.3 → 0.18.0

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/HISTORY.md CHANGED
@@ -1,3 +1,11 @@
1
+ # 0.18.0
2
+ * Nov 27, 2012
3
+
4
+ - Added `append_environment` config option [virtualstaticvoid]
5
+ - Cleaned up the readme and generator documentation
6
+ - Added `connection_class` config option [smashtank]
7
+ - Fixed a [bug](https://github.com/influitive/apartment/issues/17#issuecomment-10758327) in pg adapter when missing schema
8
+
1
9
  # 0.17.1
2
10
  * Oct 30, 2012
3
11
 
data/README.md CHANGED
@@ -33,8 +33,8 @@ you need to create a new database, you can run the following command:
33
33
 
34
34
  Apartment::Database.create('database_name')
35
35
 
36
- Apartment will create a new database in the following format: "_environment_\_database_name".
37
- In the case of a sqlite database, this will be created in your 'db/migrate' folder. With
36
+ If you're using the [prepend environment](https://github.com/influitive/apartment#handling-environments) config option or you AREN'T using Postgresql Schemas, this will create a database in the following format: "#{environment}\_database_name".
37
+ In the case of a sqlite database, this will be created in your 'db/' folder. With
38
38
  other databases, the database will be created as a new DB within the system.
39
39
 
40
40
  When you create a new database, all migrations will be run against that database, so it will be
data/apartment.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Ryan Brunner", "Brad Robertson"]
10
10
  s.summary = %q{A Ruby gem for managing database multitenancy}
11
11
  s.description = %q{Apartment allows Rack applications to deal with database multitenancy through ActiveRecord}
12
- s.email = %w{ryan@ryanbrunner.com bradleyrobertson@gmail.com}
12
+ s.email = %w{ryan@influitive.com brad@influitive.com}
13
13
  s.files = `git ls-files`.split("\n")
14
14
  s.test_files = `git ls-files -- {spec}/*`.split("\n")
15
15
 
data/lib/apartment.rb CHANGED
@@ -3,12 +3,14 @@ require 'apartment/railtie' if defined?(Rails)
3
3
  module Apartment
4
4
 
5
5
  class << self
6
- ACCESSOR_METHODS = [:use_postgres_schemas, :seed_after_create, :prepend_environment]
7
- WRITER_METHODS = [:database_names, :excluded_models, :default_schema, :persistent_schemas]
6
+ ACCESSOR_METHODS = [:use_postgres_schemas, :seed_after_create, :prepend_environment, :append_environment]
7
+ WRITER_METHODS = [:database_names, :excluded_models, :default_schema, :persistent_schemas, :connection_class]
8
8
 
9
9
  attr_accessor(*ACCESSOR_METHODS)
10
10
  attr_writer(*WRITER_METHODS)
11
11
 
12
+ delegate :connection, :establish_connection, to: :connection_class
13
+
12
14
  # configure apartment with available options
13
15
  def configure
14
16
  yield self if block_given?
@@ -37,6 +39,10 @@ module Apartment
37
39
  @persistent_schemas || []
38
40
  end
39
41
 
42
+ def connection_class
43
+ @connection_class || ActiveRecord::Base
44
+ end
45
+
40
46
  # Reset all the config for Apartment
41
47
  def reset
42
48
  (ACCESSOR_METHODS + WRITER_METHODS).each{|method| instance_variable_set(:"@#{method}", nil) }
@@ -37,7 +37,7 @@ module Apartment
37
37
  # @return {String} current database name
38
38
  #
39
39
  def current_database
40
- ActiveRecord::Base.connection.current_database
40
+ Apartment.connection.current_database
41
41
  end
42
42
  alias_method :current, :current_database
43
43
 
@@ -46,8 +46,8 @@ module Apartment
46
46
  # @param {String} database Database name
47
47
  #
48
48
  def drop(database)
49
- # ActiveRecord::Base.connection.drop_database note that drop_database will not throw an exception, so manually execute
50
- ActiveRecord::Base.connection.execute("DROP DATABASE #{environmentify(database)}" )
49
+ # Apartment.connection.drop_database note that drop_database will not throw an exception, so manually execute
50
+ Apartment.connection.execute("DROP DATABASE #{environmentify(database)}" )
51
51
 
52
52
  rescue ActiveRecord::StatementInvalid
53
53
  raise DatabaseNotFound, "The database #{environmentify(database)} cannot be found"
@@ -69,7 +69,7 @@ module Apartment
69
69
  # Establish a new connection for each specific excluded model
70
70
  #
71
71
  def process_excluded_models
72
- # All other models will shared a connection (at ActiveRecord::Base) and we can modify at will
72
+ # All other models will shared a connection (at Apartment.connection_class) and we can modify at will
73
73
  Apartment.excluded_models.each do |excluded_model|
74
74
  # Note that due to rails reloading, we now take string references to classes rather than
75
75
  # actual object references. This way when we contantize, we always get the proper class reference
@@ -85,7 +85,7 @@ module Apartment
85
85
  # Reset the database connection to the default
86
86
  #
87
87
  def reset
88
- ActiveRecord::Base.establish_connection @config
88
+ Apartment.establish_connection @config
89
89
  end
90
90
 
91
91
  # Switch to new connection (or schema if appopriate)
@@ -113,7 +113,7 @@ module Apartment
113
113
  # @param {String} database Database name
114
114
  #
115
115
  def create_database(database)
116
- ActiveRecord::Base.connection.create_database( environmentify(database) )
116
+ Apartment.connection.create_database( environmentify(database) )
117
117
 
118
118
  rescue ActiveRecord::StatementInvalid
119
119
  raise DatabaseExists, "The database #{environmentify(database)} already exists."
@@ -124,8 +124,8 @@ module Apartment
124
124
  # @param {String} database Database name
125
125
  #
126
126
  def connect_to_new(database)
127
- ActiveRecord::Base.establish_connection multi_tenantify(database)
128
- ActiveRecord::Base.connection.active? # call active? to manually check if this connection is valid
127
+ Apartment.establish_connection multi_tenantify(database)
128
+ Apartment.connection.active? # call active? to manually check if this connection is valid
129
129
 
130
130
  rescue ActiveRecord::StatementInvalid
131
131
  raise DatabaseNotFound, "The database #{environmentify(database)} cannot be found."
@@ -137,7 +137,17 @@ module Apartment
137
137
  # @return {String} database name with Rails environment *optionally* prepended
138
138
  #
139
139
  def environmentify(database)
140
- Apartment.prepend_environment && !database.include?(Rails.env) ? "#{Rails.env}_#{database}" : database
140
+ unless database.include?(Rails.env)
141
+ if Apartment.prepend_environment
142
+ "#{Rails.env}_#{database}"
143
+ elsif Apartment.append_environment
144
+ "#{database}_#{Rails.env}"
145
+ else
146
+ database
147
+ end
148
+ else
149
+ database
150
+ end
141
151
  end
142
152
 
143
153
  # Import the database schema
@@ -40,7 +40,7 @@ module Apartment
40
40
  # @param {String} database Database (schema) to drop
41
41
  #
42
42
  def drop(database)
43
- ActiveRecord::Base.connection.execute(%{DROP SCHEMA "#{database}" CASCADE})
43
+ Apartment.connection.execute(%{DROP SCHEMA "#{database}" CASCADE})
44
44
 
45
45
  rescue ActiveRecord::StatementInvalid
46
46
  raise SchemaNotFound, "The schema #{database.inspect} cannot be found."
@@ -78,7 +78,7 @@ module Apartment
78
78
  #
79
79
  def reset
80
80
  @current_database = Apartment.default_schema
81
- ActiveRecord::Base.connection.schema_search_path = full_search_path
81
+ Apartment.connection.schema_search_path = full_search_path
82
82
  end
83
83
 
84
84
  protected
@@ -89,16 +89,16 @@ module Apartment
89
89
  return reset if database.nil?
90
90
 
91
91
  @current_database = database.to_s
92
- ActiveRecord::Base.connection.schema_search_path = full_search_path
92
+ Apartment.connection.schema_search_path = full_search_path
93
93
 
94
94
  rescue ActiveRecord::StatementInvalid
95
- raise SchemaNotFound, "One of the following schema(s) is invalid: #{new_search_path}"
95
+ raise SchemaNotFound, "One of the following schema(s) is invalid: #{full_search_path}"
96
96
  end
97
97
 
98
98
  # Create the new schema
99
99
  #
100
100
  def create_database(database)
101
- ActiveRecord::Base.connection.execute(%{CREATE SCHEMA "#{database}"})
101
+ Apartment.connection.execute(%{CREATE SCHEMA "#{database}"})
102
102
 
103
103
  rescue ActiveRecord::StatementInvalid
104
104
  raise SchemaExists, "The schema #{database} already exists."
@@ -14,6 +14,7 @@ module Apartment
14
14
  config.database_names = []
15
15
  config.seed_after_create = false
16
16
  config.prepend_environment = false
17
+ config.append_environment = false
17
18
  end
18
19
  end
19
20
 
@@ -1,3 +1,3 @@
1
1
  module Apartment
2
- VERSION = "0.17.3"
2
+ VERSION = "0.18.0"
3
3
  end
@@ -1,5 +1,6 @@
1
- ##
1
+ #
2
2
  # Apartment Configuration
3
+ #
3
4
  Apartment.configure do |config|
4
5
 
5
6
  # these models will not be multi-tenanted,
@@ -18,18 +19,18 @@ Apartment.configure do |config|
18
19
  # config.prepend_environment = true
19
20
  # config.append_environment = true
20
21
 
21
- # supply list of database names
22
- config.database_names = lambda{ ToDo_Tenant_Or_User_Model.scoped.collect(&:database) }
22
+ # supply list of database names for migrations to run on
23
+ config.database_names = lambda{ ToDo_Tenant_Or_User_Model.pluck :database }
23
24
 
24
25
  end
25
26
 
26
27
  ##
27
28
  # Elevator Configuration
28
29
 
29
- # Rails.application.config.middleware.use 'Apartment::Elevators::Domain'
30
+ # Rails.application.config.middleware.use 'Apartment::Elevators::Generic', lambda { |request|
31
+ # # TODO: supply generic implementation
32
+ # }
30
33
 
31
- # Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
34
+ # Rails.application.config.middleware.use 'Apartment::Elevators::Domain'
32
35
 
33
- Rails.application.config.middleware.use 'Apartment::Elevators::Generic', Proc.new { |request|
34
- # TODO: supply generic implementation
35
- }
36
+ Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
@@ -3,7 +3,10 @@ require 'spec_helper'
3
3
  shared_examples_for "a generic apartment adapter" do
4
4
  include Apartment::Spec::AdapterRequirements
5
5
 
6
- before{ Apartment.prepend_environment = false }
6
+ before {
7
+ Apartment.prepend_environment = false
8
+ Apartment.append_environment = false
9
+ }
7
10
 
8
11
  #
9
12
  # Creates happen already in our before_filter
@@ -148,50 +148,7 @@ 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 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
151
+ connection.schema_search_path.should == public_schema
195
152
  end
196
153
 
197
154
  it "should raise an error if schema is invalid" do
@@ -256,7 +213,4 @@ shared_examples_for "a schema based apartment adapter" do
256
213
  end
257
214
  end
258
215
  end
259
-
260
-
261
-
262
216
  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.3
4
+ version: 0.18.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-11-20 00:00:00.000000000 Z
13
+ date: 2012-11-28 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -223,8 +223,8 @@ dependencies:
223
223
  description: Apartment allows Rack applications to deal with database multitenancy
224
224
  through ActiveRecord
225
225
  email:
226
- - ryan@ryanbrunner.com
227
- - bradleyrobertson@gmail.com
226
+ - ryan@influitive.com
227
+ - brad@influitive.com
228
228
  executables: []
229
229
  extensions: []
230
230
  extra_rdoc_files: []
@@ -342,7 +342,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
342
342
  version: '0'
343
343
  segments:
344
344
  - 0
345
- hash: -682928631525156949
345
+ hash: -2510818630049995736
346
346
  required_rubygems_version: !ruby/object:Gem::Requirement
347
347
  none: false
348
348
  requirements:
@@ -351,7 +351,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
351
351
  version: '0'
352
352
  segments:
353
353
  - 0
354
- hash: -682928631525156949
354
+ hash: -2510818630049995736
355
355
  requirements: []
356
356
  rubyforge_project:
357
357
  rubygems_version: 1.8.24