apartment 0.10.3 → 0.11.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,8 @@
1
+ # 0.11.0
2
+ * Sep 20, 2011
3
+
4
+ - Excluded models no longer use a different connection when using postgresql schemas. Instead their table_name is prefixed with `public.`
5
+
1
6
  # 0.10.3
2
7
  * Sep 20, 2011
3
8
 
data/lib/apartment.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'apartment/railtie'
1
+ require 'apartment/railtie' if defined?(Rails)
2
2
 
3
3
  module Apartment
4
4
 
@@ -65,11 +65,3 @@ module Apartment
65
65
  class DJSerializationError < ApartmentError; end
66
66
 
67
67
  end
68
-
69
- Apartment.configure do |config|
70
- config.excluded_models = []
71
- config.use_postgres_schemas = true
72
- config.database_names = []
73
- config.seed_after_create = false
74
- config.prepend_environment = true
75
- end
@@ -15,20 +15,10 @@ module Apartment
15
15
  @defaults = defaults
16
16
  end
17
17
 
18
- # Connect to db, do your biz, switch back to previous db
19
- #
20
- # @param {String?} database Database or schema to connect to
21
- def process(database = nil)
22
- current_db = current_database
23
- switch(database)
24
- yield if block_given?
25
- ensure
26
- switch(current_db)
27
- end
28
-
29
18
  # Create new postgres schema
30
19
  #
31
20
  # @param {String} database Database name
21
+ #
32
22
  def create(database)
33
23
  ActiveRecord::Base.connection.execute("CREATE DATABASE #{environmentify(sanitize(database))}")
34
24
 
@@ -40,12 +30,45 @@ module Apartment
40
30
  end
41
31
  end
42
32
 
33
+ # Get the current database name
34
+ #
35
+ # @return {String} current database name
36
+ #
37
+ def current_database
38
+ ActiveRecord::Base.connection.current_database
39
+ end
40
+
41
+ # Connect to db, do your biz, switch back to previous db
42
+ #
43
+ # @param {String?} database Database or schema to connect to
44
+ #
45
+ def process(database = nil)
46
+ current_db = current_database
47
+ switch(database)
48
+ yield if block_given?
49
+ ensure
50
+ switch(current_db)
51
+ end
52
+
53
+ # Establish a new connection for each specific excluded model
54
+ #
55
+ def process_excluded_models
56
+ # All other models will shared a connection (at ActiveRecord::Base) and we can modify at will
57
+ Apartment.excluded_models.each do |excluded_model|
58
+ excluded_model.establish_connection @config
59
+ end
60
+ end
61
+
43
62
  # Reset the base connection
63
+ #
44
64
  def reset
45
65
  ActiveRecord::Base.establish_connection @config
46
66
  end
47
67
 
48
- # Switch to new connection (or schema if appopriate)
68
+ # Switch to new connection (or schema if appopriate)
69
+ #
70
+ # @param {String} database Database name
71
+ #
49
72
  def switch(database = nil)
50
73
  # Just connect to default db and return
51
74
  return reset if database.nil?
@@ -53,8 +76,12 @@ module Apartment
53
76
  connect_to_new(database)
54
77
  end
55
78
 
79
+ # Prepend the environment if configured and the environment isn't already there
80
+ #
81
+ # @param {String} database Database name
82
+ # @return {String} database name with Rails environment *optionally* prepended
83
+ #
56
84
  def environmentify(database)
57
- # prepend the environment if configured and the environment isn't already there
58
85
  return "#{Rails.env}_#{database}" if Apartment.prepend_environment && !database.include?(Rails.env)
59
86
 
60
87
  database
@@ -65,11 +92,6 @@ module Apartment
65
92
  end
66
93
  alias_method :seed, :seed_data
67
94
 
68
- # Return the current database name
69
- def current_database
70
- ActiveRecord::Base.connection.current_database
71
- end
72
-
73
95
  protected
74
96
 
75
97
  def connect_to_new(database)
@@ -87,11 +109,6 @@ module Apartment
87
109
  end
88
110
  end
89
111
 
90
- # Remove all non-alphanumeric characters
91
- def sanitize(database)
92
- database.gsub(/[\W]/,'')
93
- end
94
-
95
112
  def load_or_abort(file)
96
113
  if File.exists?(file)
97
114
  # Don't log the output of loading files (such as schema or seeds)
@@ -103,6 +120,13 @@ module Apartment
103
120
  end
104
121
  end
105
122
 
123
+
124
+
125
+ # Remove all non-alphanumeric characters
126
+ def sanitize(database)
127
+ database.gsub(/[\W]/,'')
128
+ end
129
+
106
130
  end
107
131
  end
108
132
  end
@@ -43,7 +43,28 @@ module Apartment
43
43
  def current_database
44
44
  ActiveRecord::Base.connection.schema_search_path
45
45
  end
46
+
47
+ # Set the table_name to always use the public namespace for excluded models
48
+ #
49
+ def process_excluded_models
50
+
51
+ Apartment.excluded_models.each do |excluded_model|
52
+ # some models (such as delayed_job) seem to load and cache their column names before this,
53
+ # so would never get the public prefix, so reset first
54
+ excluded_model.reset_column_information
55
+
56
+ # Ensure that if a schema *was* set, we override
57
+ table_name = excluded_model.table_name.split('.', 2).last
58
+
59
+ # Not sure why, but Delayed::Job somehow ignores table_name_prefix... so we'll just manually set table name instead
60
+ excluded_model.table_name = "public.#{table_name}"
61
+ end
62
+ end
46
63
 
64
+ # Reset schema search path to the default schema_search_path
65
+ #
66
+ # @return {String} default schema search path
67
+ #
47
68
  def reset
48
69
  ActiveRecord::Base.connection.schema_search_path = @defaults[:schema_search_path]
49
70
  end
@@ -6,12 +6,12 @@ module Apartment
6
6
  class << self
7
7
 
8
8
  # pass these methods to our adapter
9
- delegate :create, :switch, :reset, :process, :seed, :current_database, :to => :adapter
9
+ delegate :create, :current_database, :process, :process_excluded_models, :reset, :seed, :switch, :to => :adapter
10
10
 
11
11
  # Call init to establish a connection to the public schema on all excluded models
12
12
  # This must be done before creating any new schemas or switching
13
13
  def init
14
- connect_exclusions
14
+ process_excluded_models
15
15
  end
16
16
 
17
17
  def adapter
@@ -39,14 +39,6 @@ module Apartment
39
39
 
40
40
  private
41
41
 
42
- def connect_exclusions
43
- # Establish a connection for each specific excluded model
44
- # Thus all other models will shared a connection (at ActiveRecord::Base) and we can modify at will
45
- Apartment.excluded_models.each do |excluded_model|
46
- excluded_model.establish_connection config
47
- end
48
- end
49
-
50
42
  def config
51
43
  @config ||= Rails.configuration.database_configuration[Rails.env].symbolize_keys
52
44
  end
@@ -5,10 +5,10 @@ module Apartment
5
5
  module Delayed
6
6
  module Job
7
7
 
8
- # Will enqueue a job ensuring that it happens within the public schema
9
- # This is a work-around due to the fact that DJ for some reason always
10
- # queues its jobs in the current_schema, rather than the public schema
11
- # as it is supposed to
8
+ # Will enqueue a job ensuring that it happens within the main 'public' database
9
+ #
10
+ # Note that this should not longer be required for versions >= 0.11.0 when using postgresql schemas
11
+ #
12
12
  def self.enqueue(payload_object, options = {})
13
13
  Apartment::Database.process do
14
14
  ::Delayed::Job.enqueue(payload_object, options)
@@ -2,6 +2,22 @@ require 'rails'
2
2
 
3
3
  module Apartment
4
4
  class Railtie < Rails::Railtie
5
+
6
+ # Ensure that active_record is loaded, then run default config
7
+ initializer 'apartment.configure' do
8
+ # ActiveSupport.on_load(:active_record) do
9
+
10
+ Apartment.configure do |config|
11
+ config.excluded_models = []
12
+ config.use_postgres_schemas = true
13
+ config.database_names = []
14
+ config.seed_after_create = false
15
+ config.prepend_environment = true
16
+ end
17
+
18
+ # end
19
+ end
20
+
5
21
  rake_tasks do
6
22
  load 'tasks/apartment.rake'
7
23
  end
@@ -1,3 +1,3 @@
1
1
  module Apartment
2
- VERSION = "0.10.3"
2
+ VERSION = "0.11.0"
3
3
  end
@@ -25,11 +25,11 @@ describe Apartment::Database do
25
25
  describe "#init" do
26
26
 
27
27
  it "should process model exclusions" do
28
- Company.should_receive(:establish_connection).with( config )
29
-
30
28
  Apartment.configure do |config|
31
29
  config.excluded_models = [Company]
32
30
  end
31
+
32
+ Company.table_name.should == "public.companies"
33
33
  end
34
34
 
35
35
  end
@@ -164,7 +164,7 @@ describe Apartment::Database do
164
164
 
165
165
  it "should ignore excluded models" do
166
166
  Apartment::Database.switch database
167
- Company.connection.schema_search_path.should == @schema_search_path
167
+ Company.table_name.should include('public')
168
168
  end
169
169
 
170
170
  it "should create excluded models in public schema" do
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: apartment
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.10.3
5
+ version: 0.11.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Brunner
@@ -223,7 +223,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
223
223
  requirements:
224
224
  - - ">="
225
225
  - !ruby/object:Gem::Version
226
- hash: 2467393586415245119
226
+ hash: -1056095168951008340
227
227
  segments:
228
228
  - 0
229
229
  version: "0"
@@ -232,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
232
232
  requirements:
233
233
  - - ">="
234
234
  - !ruby/object:Gem::Version
235
- hash: 2467393586415245119
235
+ hash: -1056095168951008340
236
236
  segments:
237
237
  - 0
238
238
  version: "0"