apartment 0.14.2 → 0.14.3

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.14.3
2
+ * Feb 21, 2012
3
+
4
+ - Fix yaml serialization of non DJ models
5
+
1
6
  # 0.14.2
2
7
  * Feb 21, 2012
3
8
 
data/lib/apartment.rb CHANGED
@@ -1,69 +1,69 @@
1
1
  require 'apartment/railtie' if defined?(Rails)
2
2
 
3
3
  module Apartment
4
-
4
+
5
5
  class << self
6
6
  attr_accessor :use_postgres_schemas, :seed_after_create, :prepend_environment
7
7
  attr_writer :database_names, :excluded_models
8
-
8
+
9
9
  # configure apartment with available options
10
10
  def configure
11
11
  yield self if block_given?
12
12
  end
13
-
13
+
14
14
  # Be careful not to use `return` here so both Proc and lambda can be used without breaking
15
15
  def database_names
16
16
  @database_names.respond_to?(:call) ? @database_names.call : @database_names
17
17
  end
18
-
18
+
19
19
  # Default to none
20
20
  def excluded_models
21
21
  @excluded_models || []
22
22
  end
23
-
23
+
24
24
  end
25
-
25
+
26
26
  autoload :Database, 'apartment/database'
27
27
  autoload :Migrator, 'apartment/migrator'
28
28
  autoload :Reloader, 'apartment/reloader'
29
-
29
+
30
30
  module Adapters
31
31
  autoload :AbstractAdapter, 'apartment/adapters/abstract_adapter'
32
32
  # Specific adapters will be loaded dynamically based on adapter in config
33
33
  end
34
-
34
+
35
35
  module Elevators
36
36
  autoload :Subdomain, 'apartment/elevators/subdomain'
37
37
  end
38
-
38
+
39
39
  module Delayed
40
-
40
+
41
41
  autoload :Requirements, 'apartment/delayed_job/requirements'
42
-
42
+
43
43
  module Job
44
44
  autoload :Hooks, 'apartment/delayed_job/hooks'
45
45
  end
46
46
  end
47
-
47
+
48
48
  # Exceptions
49
49
  class ApartmentError < StandardError; end
50
-
50
+
51
51
  # Raised when apartment cannot find the adapter specified in <tt>config/database.yml</tt>
52
52
  class AdapterNotFound < ApartmentError; end
53
-
53
+
54
54
  # Raised when database cannot find the specified database
55
55
  class DatabaseNotFound < ApartmentError; end
56
-
56
+
57
57
  # Raised when trying to create a database that already exists
58
58
  class DatabaseExists < ApartmentError; end
59
-
59
+
60
60
  # Raised when database cannot find the specified schema
61
61
  class SchemaNotFound < ApartmentError; end
62
-
62
+
63
63
  # Raised when trying to create a schema that already exists
64
64
  class SchemaExists < ApartmentError; end
65
-
65
+
66
66
  # Raised when an ActiveRecord object does not have the required database field on it
67
67
  class DJSerializationError < ApartmentError; end
68
-
68
+
69
69
  end
@@ -15,7 +15,7 @@ module ActiveRecord
15
15
  # Rails > 3.0 now uses encode_with to determine what to encode with yaml
16
16
  # @override to include database attribute
17
17
  def encode_with_with_database(coder)
18
- coder['database'] = database
18
+ coder['database'] = @database if @database.present?
19
19
  encode_with_without_database(coder)
20
20
  end
21
21
  alias_method_chain :encode_with, :database
@@ -4,17 +4,17 @@ require 'apartment/delayed_job/active_record' # ensure that our AR hooks are l
4
4
  module Apartment
5
5
  module Delayed
6
6
  module Job
7
-
7
+
8
8
  # Will enqueue a job ensuring that it happens within the main 'public' database
9
- #
9
+ #
10
10
  # Note that this should not longer be required for versions >= 0.11.0 when using postgresql schemas
11
- #
11
+ #
12
12
  def self.enqueue(payload_object, options = {})
13
13
  Apartment::Database.process do
14
14
  ::Delayed::Job.enqueue(payload_object, options)
15
15
  end
16
16
  end
17
-
17
+
18
18
  end
19
19
  end
20
20
  end
@@ -1,4 +1,4 @@
1
- require 'apartment/delayed_job/enqueue'
1
+ require 'apartment/delayed_job/active_record' # ensure that our AR hooks are loaded when queueing
2
2
 
3
3
  module Apartment
4
4
  module Delayed
@@ -3,25 +3,25 @@ module Apartment
3
3
  # Provides a rack based db switching solution based on subdomains
4
4
  # Assumes that database name should match subdomain
5
5
  class Subdomain
6
-
6
+
7
7
  def initialize(app)
8
8
  @app = app
9
9
  end
10
-
10
+
11
11
  def call(env)
12
12
  request = ActionDispatch::Request.new(env)
13
-
13
+
14
14
  database = subdomain(request)
15
-
15
+
16
16
  Apartment::Database.switch database if database
17
-
17
+
18
18
  @app.call(env)
19
19
  end
20
-
20
+
21
21
  def subdomain(request)
22
22
  request.subdomain.present? && request.subdomain || nil
23
23
  end
24
-
24
+
25
25
  end
26
26
  end
27
27
  end
@@ -1,23 +1,23 @@
1
1
  module Apartment
2
-
2
+
3
3
  module Migrator
4
-
4
+
5
5
  extend self
6
-
6
+
7
7
  # Migrate to latest
8
8
  def migrate(database)
9
9
  Database.process(database){ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path) }
10
10
  end
11
-
11
+
12
12
  # Migrate up/down to a specific version
13
13
  def run(direction, database, version)
14
14
  Database.process(database){ ActiveRecord::Migrator.run(direction, ActiveRecord::Migrator.migrations_path, version) }
15
15
  end
16
-
16
+
17
17
  # rollback latest migration `step` number of times
18
18
  def rollback(database, step = 1)
19
19
  Database.process(database){ ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_path, step) }
20
20
  end
21
21
  end
22
-
22
+
23
23
  end
@@ -2,11 +2,11 @@ require 'rails'
2
2
 
3
3
  module Apartment
4
4
  class Railtie < Rails::Railtie
5
-
6
- #
5
+
6
+ #
7
7
  # Set up our default config options
8
8
  # Do this before the app initializers run so we don't override custom settings
9
- #
9
+ #
10
10
  config.before_initialize do
11
11
  Apartment.configure do |config|
12
12
  config.excluded_models = []
@@ -16,39 +16,39 @@ module Apartment
16
16
  config.prepend_environment = true
17
17
  end
18
18
  end
19
-
19
+
20
20
  # Hook into ActionDispatch::Reloader to ensure Apartment is properly initialized
21
21
  # Note that this doens't entirely work as expected in Development, because this is called before classes are reloaded
22
22
  # See the above middleware/console declarations below to help with this. Hope to fix that soon.
23
- #
23
+ #
24
24
  config.to_prepare do
25
25
  Apartment::Database.init
26
26
  end
27
-
28
- #
27
+
28
+ #
29
29
  # Ensure rake tasks are loaded
30
- #
30
+ #
31
31
  rake_tasks do
32
32
  load 'tasks/apartment.rake'
33
33
  end
34
-
35
- #
34
+
35
+ #
36
36
  # The following initializers are a workaround to the fact that I can't properly hook into the rails reloader
37
37
  # Note this is technically valid for any environment where cache_classes is false, for us, it's just development
38
- #
38
+ #
39
39
  if Rails.env.development?
40
-
40
+
41
41
  # Apartment::Reloader is middleware to initialize things properly on each request to dev
42
42
  initializer 'apartment.init' do |app|
43
43
  app.config.middleware.use "Apartment::Reloader"
44
44
  end
45
-
45
+
46
46
  # Overrides reload! to also call Apartment::Database.init as well so that the reloaded classes have the proper table_names
47
47
  console do
48
48
  require 'apartment/console'
49
49
  end
50
-
50
+
51
51
  end
52
-
52
+
53
53
  end
54
54
  end
@@ -1,3 +1,3 @@
1
1
  module Apartment
2
- VERSION = "0.14.2"
2
+ VERSION = "0.14.3"
3
3
  end
@@ -4,7 +4,7 @@ describe Apartment do
4
4
  it "should be valid" do
5
5
  Apartment.should be_a(Module)
6
6
  end
7
-
7
+
8
8
  it "should be a valid app" do
9
9
  ::Rails.application.should be_a(Dummy::Application)
10
10
  end
@@ -30,31 +30,6 @@ describe Apartment::Delayed do
30
30
  Apartment::Test.reset
31
31
  end
32
32
 
33
- describe Apartment::Delayed::Job do
34
- context "#enqueue" do
35
-
36
- before do
37
- Apartment::Database.reset
38
- end
39
-
40
- it "should queue up jobs in the public schema" do
41
- dj_count = Delayed::Job.count
42
- Apartment::Database.switch database
43
- Apartment::Delayed::Job.enqueue FakeDjClass.new
44
- Apartment::Database.reset
45
-
46
- Delayed::Job.count.should == dj_count + 1
47
- end
48
-
49
- it "should not queue jobs in the current schema" do
50
- Apartment::Database.switch database
51
- expect {
52
- Apartment::Delayed::Job.enqueue FakeDjClass.new
53
- }.to_not change(Delayed::Job, :count) # because we will still be on the `database` schema, not public
54
- end
55
- end
56
- end
57
-
58
33
  describe Apartment::Delayed::Requirements do
59
34
 
60
35
  before do
@@ -1,29 +1,29 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Apartment do
4
-
4
+
5
5
  describe "#config" do
6
-
6
+
7
7
  let(:excluded_models){ [Company] }
8
-
8
+
9
9
  after do
10
10
  Apartment::Test.reset
11
11
  end
12
-
12
+
13
13
  it "should yield the Apartment object" do
14
14
  Apartment.configure do |config|
15
15
  config.excluded_models = []
16
16
  config.should == Apartment
17
17
  end
18
18
  end
19
-
19
+
20
20
  it "should set excluded models" do
21
21
  Apartment.configure do |config|
22
22
  config.excluded_models = excluded_models
23
23
  end
24
24
  Apartment.excluded_models.should == excluded_models
25
25
  end
26
-
26
+
27
27
  it "should set postgres_schemas" do
28
28
  Apartment.configure do |config|
29
29
  config.excluded_models = []
@@ -31,7 +31,7 @@ describe Apartment do
31
31
  end
32
32
  Apartment.use_postgres_schemas.should be_false
33
33
  end
34
-
34
+
35
35
  it "should set seed_after_create" do
36
36
  Apartment.configure do |config|
37
37
  config.excluded_models = []
@@ -39,40 +39,40 @@ describe Apartment do
39
39
  end
40
40
  Apartment.seed_after_create.should be_true
41
41
  end
42
-
42
+
43
43
  context "databases" do
44
44
  it "should return object if it doesnt respond_to call" do
45
45
  database_names = ['users', 'companies']
46
-
46
+
47
47
  Apartment.configure do |config|
48
48
  config.excluded_models = []
49
49
  config.database_names = database_names
50
50
  end
51
51
  Apartment.database_names.should == database_names
52
52
  end
53
-
53
+
54
54
  it "should invoke the proc if appropriate" do
55
55
  database_names = lambda{ ['users', 'users'] }
56
56
  database_names.should_receive(:call)
57
-
57
+
58
58
  Apartment.configure do |config|
59
59
  config.excluded_models = []
60
60
  config.database_names = database_names
61
61
  end
62
62
  Apartment.database_names
63
63
  end
64
-
64
+
65
65
  it "should return the invoked proc if appropriate" do
66
66
  dbs = lambda{ Company.scoped }
67
-
67
+
68
68
  Apartment.configure do |config|
69
69
  config.excluded_models = []
70
70
  config.database_names = dbs
71
71
  end
72
-
72
+
73
73
  Apartment.database_names.should == Company.scoped
74
74
  end
75
75
  end
76
-
76
+
77
77
  end
78
78
  end
@@ -1,57 +1,57 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Apartment::Migrator do
4
-
4
+
5
5
  let(:config){ Apartment::Test.config['connections']['postgresql'].symbolize_keys }
6
6
  let(:schema_name){ 'some_db_schema' }
7
7
  let(:version){ 20110613152810 } # note this is brittle! I've literally just taken the version of the one migration I made... don't change this version
8
-
8
+
9
9
  before do
10
10
  ActiveRecord::Base.establish_connection config
11
11
  Apartment::Database.stub(:config).and_return config # Use postgresql config for this test
12
12
  @original_schema = ActiveRecord::Base.connection.schema_search_path
13
-
13
+
14
14
  Apartment.configure do |config|
15
15
  config.use_postgres_schemas = true
16
16
  config.excluded_models = []
17
17
  config.database_names = [schema_name]
18
18
  end
19
-
19
+
20
20
  Apartment::Database.create schema_name # create the schema
21
21
  migrations_path = Rails.root + ActiveRecord::Migrator.migrations_path # tell AR where the real migrations are
22
22
  ActiveRecord::Migrator.stub(:migrations_path).and_return(migrations_path)
23
23
  end
24
-
24
+
25
25
  after do
26
26
  Apartment::Test.drop_schema(schema_name)
27
27
  end
28
-
28
+
29
29
  context "postgresql" do
30
30
 
31
31
  context "using schemas" do
32
-
32
+
33
33
  describe "#migrate" do
34
34
  it "should connect to new db, then reset when done" do
35
35
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
36
36
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
37
37
  Apartment::Migrator.migrate(schema_name)
38
38
  end
39
-
39
+
40
40
  it "should migrate db" do
41
41
  ActiveRecord::Migrator.should_receive(:migrate)
42
42
  Apartment::Migrator.migrate(schema_name)
43
43
  end
44
44
  end
45
-
45
+
46
46
  describe "#run" do
47
47
  context "up" do
48
-
48
+
49
49
  it "should connect to new db, then reset when done" do
50
50
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
51
51
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
52
52
  Apartment::Migrator.run(:up, schema_name, version)
53
53
  end
54
-
54
+
55
55
  it "should migrate to a version" do
56
56
  ActiveRecord::Migrator.should_receive(:run).with(:up, anything, version)
57
57
  Apartment::Migrator.run(:up, schema_name, version)
@@ -59,7 +59,7 @@ describe Apartment::Migrator do
59
59
  end
60
60
 
61
61
  describe "down" do
62
-
62
+
63
63
  it "should connect to new db, then reset when done" do
64
64
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
65
65
  ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
@@ -75,7 +75,7 @@ describe Apartment::Migrator do
75
75
 
76
76
  describe "#rollback" do
77
77
  let(:steps){ 3 }
78
-
78
+
79
79
  it "should rollback the db" do
80
80
  ActiveRecord::Migrator.should_receive(:rollback).with(anything, steps)
81
81
  Apartment::Migrator.rollback(schema_name, steps)
@@ -83,5 +83,5 @@ describe Apartment::Migrator do
83
83
  end
84
84
  end
85
85
  end
86
-
86
+
87
87
  end
@@ -1,22 +1,22 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Apartment::Reloader do
4
-
4
+
5
5
  context "using postgresql schemas" do
6
-
6
+
7
7
  before do
8
8
  Apartment.excluded_models = ["Company"]
9
9
  Company.reset_table_name # ensure we're clean
10
10
  end
11
-
11
+
12
12
  subject{ Apartment::Reloader.new(mock("Rack::Application", :call => nil)) }
13
-
13
+
14
14
  it "should initialize apartment when called" do
15
15
  Company.table_name.should_not include('public.')
16
16
  subject.call(mock('env'))
17
17
  Company.table_name.should include('public.')
18
18
  end
19
19
  end
20
-
21
-
20
+
21
+
22
22
  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.14.2
4
+ version: 0.14.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-21 00:00:00.000000000Z
13
+ date: 2012-02-21 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
17
- requirement: &2155984060 !ruby/object:Gem::Requirement
17
+ requirement: &2152184980 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 3.1.2
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2155984060
25
+ version_requirements: *2152184980
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &2155981900 !ruby/object:Gem::Requirement
28
+ requirement: &2152183300 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 0.9.2
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *2155981900
36
+ version_requirements: *2152183300
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: sqlite3
39
- requirement: &2155980140 !ruby/object:Gem::Requirement
39
+ requirement: &2152182600 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *2155980140
47
+ version_requirements: *2152182600
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &2155978440 !ruby/object:Gem::Requirement
50
+ requirement: &2152181680 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 2.8.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2155978440
58
+ version_requirements: *2152181680
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rspec-rails
61
- requirement: &2155928900 !ruby/object:Gem::Requirement
61
+ requirement: &2152177640 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 2.8.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2155928900
69
+ version_requirements: *2152177640
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: capybara
72
- requirement: &2155927460 !ruby/object:Gem::Requirement
72
+ requirement: &2152176580 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - =
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.0.0
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2155927460
80
+ version_requirements: *2152176580
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: pg
83
- requirement: &2155925380 !ruby/object:Gem::Requirement
83
+ requirement: &2152175640 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ~>
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: 0.11.0
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2155925380
91
+ version_requirements: *2152175640
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: mysql2
94
- requirement: &2155923980 !ruby/object:Gem::Requirement
94
+ requirement: &2152162340 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ~>
@@ -99,10 +99,10 @@ dependencies:
99
99
  version: 0.3.7
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *2155923980
102
+ version_requirements: *2152162340
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: silent-postgres
105
- requirement: &2155922040 !ruby/object:Gem::Requirement
105
+ requirement: &2152160960 !ruby/object:Gem::Requirement
106
106
  none: false
107
107
  requirements:
108
108
  - - ~>
@@ -110,10 +110,10 @@ dependencies:
110
110
  version: 0.1.1
111
111
  type: :development
112
112
  prerelease: false
113
- version_requirements: *2155922040
113
+ version_requirements: *2152160960
114
114
  - !ruby/object:Gem::Dependency
115
115
  name: delayed_job
116
- requirement: &2155918340 !ruby/object:Gem::Requirement
116
+ requirement: &2152159540 !ruby/object:Gem::Requirement
117
117
  none: false
118
118
  requirements:
119
119
  - - ~>
@@ -121,7 +121,7 @@ dependencies:
121
121
  version: 2.1.4
122
122
  type: :development
123
123
  prerelease: false
124
- version_requirements: *2155918340
124
+ version_requirements: *2152159540
125
125
  description: Apartment allows Rails applications to deal with database multitenancy
126
126
  email:
127
127
  - ryan@ryanbrunner.com
@@ -221,7 +221,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
221
221
  version: '0'
222
222
  segments:
223
223
  - 0
224
- hash: -739145364198480753
224
+ hash: 3522357192769439782
225
225
  required_rubygems_version: !ruby/object:Gem::Requirement
226
226
  none: false
227
227
  requirements:
@@ -230,7 +230,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
230
230
  version: '0'
231
231
  segments:
232
232
  - 0
233
- hash: -739145364198480753
233
+ hash: 3522357192769439782
234
234
  requirements: []
235
235
  rubyforge_project:
236
236
  rubygems_version: 1.8.10