apartment 0.16.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,4 +3,6 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  *.log
6
-
6
+ *.sw[pno]
7
+ spec/config/database.yml
8
+ spec/dummy/config/database.yml
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.8.7
4
3
  - 1.9.2
5
4
  - 1.9.3
data/HISTORY.md CHANGED
@@ -1,8 +1,16 @@
1
+ # 0.17.0
2
+ * Sept 26, 2012
3
+
4
+ - Apartment has [a new home!](https://github.com/influitive/apartment)
5
+ - Support Sidekiq hooks to switch dbs [maedhr]
6
+ - Allow VERSION to be used on apartment:migrate [Bhavin Kamani]
7
+
1
8
  # 0.16.0
2
9
  * June 1, 2012
3
10
 
4
11
  - Apartment now supports a default_schema to be set, rather than relying on ActiveRecord's default schema_search_path
5
- - Additional schemas can always be maintained in the schema_search_path by configuring persistent_schemas
12
+ - Additional schemas can always be maintained in the schema_search_path by configuring persistent_schemas [ryanbrunner]
13
+ - This means Hstore is officially supported!!
6
14
  - There is now a full domain based elevator to switch dbs based on the whole domain [lcowell]
7
15
  - There is now a generic elevator that takes a Proc to switch dbs based on the return value of that proc.
8
16
 
data/README.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # Apartment
2
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/influitive/apartment)
3
+ [![Build Status](https://secure.travis-ci.org/influitive/apartment.png?branch=development)](http://travis-ci.org/influitive/apartment)
4
+
2
5
  *Multitenancy for Rails 3 and ActiveRecord*
3
6
 
4
7
  Apartment provides tools to help you deal with multiple databases in your Rails
@@ -129,14 +132,31 @@ Apartment will normally just switch the `schema_search_path` whole hog to the on
129
132
 
130
133
  config.persistent_schemas = ['some', 'other', 'schemas']
131
134
 
132
- This has numerous useful applications. [Hstore](http://www.postgresql.org/docs/9.1/static/hstore.html), for instance, is a popular storage engine for Postgresql. In order to use Hstore, you have to install to a specific schema and have that always in the `schema_search_path`. This could be achieved like so:
135
+ This has numerous useful applications. [Hstore](http://www.postgresql.org/docs/9.1/static/hstore.html), for instance, is a popular storage engine for Postgresql. In order to use Hstore, you have to install it to a specific schema and have that always in the `schema_search_path`. This could be achieved like so:
133
136
 
137
+ # NOTE do not do this in a migration, must be done
138
+ # manually before you configure apartment with hstore
134
139
  # In a rake task, or on the console...
135
140
  ActiveRecord::Base.connection.execute("CREATE SCHEMA hstore; CREATE EXTENSION HSTORE SCHEMA hstore")
136
141
 
137
142
  # configure Apartment to maintain the `hstore` schema in the `schema_search_path`
138
143
  config.persistent_schemas = ['hstore']
139
144
 
145
+ There are a few caveats to be aware of when using `hstore`. First off, the hstore schema and extension creation need to be done manually *before* you reference it in any way in your migrations, database.yml or apartment. This is an unfortunate manual step, but I haven't found a way around it. You can achieve this from the command line using something like:
146
+
147
+ rails r 'ActiveRecord::Base.connection.execute("CREATE SCHEMA hstore; CREATE EXTENSION HSTORE SCHEMA hstore")'
148
+
149
+ Next, your `database.yml` file must mimic what you've set for your default and persistent schemas in Apartment. When you run migrataions with Rails, it won't know about the hstore schema because Apartment isn't injected into the default connection, it's done on a per-request basis, therefore Rails doesn't know about `hstore` during migrations. To do so, add the following to your `database.yml` for all environments
150
+
151
+ # database.yml
152
+ ...
153
+ adapter: postgresql
154
+ schema_search_path: "public,hstore"
155
+ ...
156
+
157
+ This would be for a config with `default_schema` set to `public` and `persistent_schemas` set to `['hstore']`
158
+
159
+
140
160
  ### Managing Migrations
141
161
 
142
162
  In order to migrate all of your databases (or posgresql schemas) you need to provide a list
@@ -205,7 +225,9 @@ All jobs *must* stored in the global (public) namespace, so add it to the list o
205
225
 
206
226
  ## Development
207
227
 
208
- * The Local setup for development assumes that a root user with no password exists for both mysql and postgresl
228
+ * In both `spec/dummy/config` and `spec/config`, you will see `database.yml.sample` files
229
+ * Copy them into the same directory but with the name `database.yml`
230
+ * Edit them to fit your own settings
209
231
  * Rake tasks (see the Rakefile) will help you setup your dbs necessary to run tests
210
232
  * Please issue pull requests to the `development` branch. All development happens here, master is used for releases
211
- * Ensure that your code is accompanied with tests. No code will be merged without tests
233
+ * Ensure that your code is accompanied with tests. No code will be merged without tests
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ Bundler::GemHelper.install_tasks
5
5
  require "rspec"
6
6
  require "rspec/core/rake_task"
7
7
 
8
- RSpec::Core::RakeTask.new(:spec => "db:test:prepare") do |spec|
8
+ RSpec::Core::RakeTask.new(:spec => %w{ db:copy_credentials db:test:prepare }) do |spec|
9
9
  spec.pattern = "spec/**/*_spec.rb"
10
10
  # spec.rspec_opts = '--order rand:16996'
11
11
  end
@@ -26,6 +26,16 @@ namespace :db do
26
26
  namespace :test do
27
27
  task :prepare => %w{postgres:drop_db postgres:build_db mysql:drop_db mysql:build_db}
28
28
  end
29
+
30
+ desc "copy sample database credential files over if real files don't exist"
31
+ task :copy_credentials do
32
+ require 'fileutils'
33
+ apartment_db_file = 'spec/config/database.yml'
34
+ rails_db_file = 'spec/dummy/config/database.yml'
35
+
36
+ FileUtils.copy(apartment_db_file + '.sample', apartment_db_file, :verbose => true) unless File.exists?(apartment_db_file)
37
+ FileUtils.copy(rails_db_file + '.sample', rails_db_file, :verbose => true) unless File.exists?(rails_db_file)
38
+ end
29
39
  end
30
40
 
31
41
  namespace :postgres do
@@ -7,13 +7,13 @@ Gem::Specification.new do |s|
7
7
  s.version = Apartment::VERSION
8
8
 
9
9
  s.authors = ["Ryan Brunner", "Brad Robertson"]
10
- s.summary = %q{A Ruby gem for managing database multitenancy in Rails applications}
11
- s.description = %q{Apartment allows Rails applications to deal with database multitenancy}
10
+ s.summary = %q{A Ruby gem for managing database multitenancy}
11
+ s.description = %q{Apartment allows Rack applications to deal with database multitenancy through ActiveRecord}
12
12
  s.email = %w{ryan@ryanbrunner.com bradleyrobertson@gmail.com}
13
13
  s.files = `git ls-files`.split("\n")
14
14
  s.test_files = `git ls-files -- {spec}/*`.split("\n")
15
15
 
16
- s.homepage = %q{http://github.com/bradrobertson/apartment}
16
+ s.homepage = %q{https://github.com/influitive/apartment}
17
17
  s.licenses = ["MIT"]
18
18
  s.require_paths = ["lib"]
19
19
  s.rubygems_version = %q{1.3.7}
@@ -0,0 +1,9 @@
1
+ machine:
2
+ ruby:
3
+ version: 1.9.3-p194
4
+
5
+ test:
6
+ override:
7
+ - bundle exec rake spec:
8
+ environment:
9
+ RAILS_ENV: test
@@ -39,6 +39,7 @@ module Apartment
39
39
  def current_database
40
40
  ActiveRecord::Base.connection.current_database
41
41
  end
42
+ alias_method :current, :current_database
42
43
 
43
44
  # Drop the database
44
45
  #
@@ -109,15 +109,9 @@ module Apartment
109
109
  # Generate the final search path to set including persistent_schemas
110
110
  #
111
111
  def full_search_path
112
- persistent_schemas = persistent_schema_string
112
+ persistent_schemas = Apartment.persistent_schemas.join(', ')
113
113
  @current_database.to_s + (persistent_schemas.empty? ? "" : ", #{persistent_schemas}")
114
114
  end
115
-
116
- # Cached persistent schemas joined in a valid search path format (comma separated)
117
- #
118
- def persistent_schema_string
119
- Apartment.persistent_schemas.join(', ')
120
- end
121
115
  end
122
116
  end
123
117
  end
@@ -8,7 +8,7 @@ module Apartment
8
8
 
9
9
  extend self
10
10
 
11
- delegate :create, :current_database, :drop, :process, :process_excluded_models, :reset, :seed, :switch, :to => :adapter
11
+ delegate :create, :current_database, :current, :drop, :process, :process_excluded_models, :reset, :seed, :switch, :to => :adapter
12
12
 
13
13
  attr_writer :config
14
14
 
@@ -6,7 +6,11 @@ module Apartment
6
6
 
7
7
  # Migrate to latest
8
8
  def migrate(database)
9
- Database.process(database){ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path) }
9
+ Database.process(database) do
10
+ ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
11
+ ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
12
+ end
13
+ end
10
14
  end
11
15
 
12
16
  # Migrate up/down to a specific version
@@ -20,4 +24,4 @@ module Apartment
20
24
  end
21
25
  end
22
26
 
23
- end
27
+ end
@@ -0,0 +1,12 @@
1
+ module Apartment
2
+ module Sidekiq
3
+ module Client
4
+ class DatabaseMiddleware
5
+ def call(worker_class, item, queue)
6
+ item["apartment"] = Apartment::Database.current_database
7
+ yield
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Apartment
2
+ module Sidekiq
3
+ module Server
4
+ class DatabaseMiddleware
5
+ def call(worker_class, item, queue)
6
+ Apartment::Database.process(item['apartment']) do
7
+ yield
8
+ end
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,3 +1,3 @@
1
1
  module Apartment
2
- VERSION = "0.16.0"
2
+ VERSION = "0.17.0"
3
3
  end
@@ -7,7 +7,7 @@ test:
7
7
  min_messages: WARNING
8
8
  pool: 5
9
9
  timeout: 5000
10
-
10
+
11
11
  development:
12
12
  adapter: postgresql
13
13
  database: apartment_postgresql_development
@@ -24,6 +24,9 @@ describe "apartment rake tasks" do
24
24
  config.excluded_models = ["Company"]
25
25
  config.database_names = lambda{ Company.scoped.collect(&:database) }
26
26
  end
27
+
28
+ # fix up table name of shared/excluded models
29
+ Company.table_name = 'public.companies'
27
30
  end
28
31
 
29
32
  context "with x number of databases" do
@@ -26,8 +26,10 @@ shared_context "elevators", :elevator => true do
26
26
  let(:api) { Apartment::Database }
27
27
 
28
28
  before do
29
+ Apartment.reset # reset all config
29
30
  Apartment.seed_after_create = false
30
31
  Apartment.use_postgres_schemas = true
32
+ api.reload! # reload adapter
31
33
 
32
34
  api.create(database1)
33
35
  api.create(database2)
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.16.0
4
+ version: 0.17.0
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-06-01 00:00:00.000000000 Z
13
+ date: 2012-09-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
17
- requirement: &2152850820 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,15 @@ dependencies:
22
22
  version: 3.1.2
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152850820
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: 3.1.2
26
31
  - !ruby/object:Gem::Dependency
27
32
  name: rack
28
- requirement: &2152849500 !ruby/object:Gem::Requirement
33
+ requirement: !ruby/object:Gem::Requirement
29
34
  none: false
30
35
  requirements:
31
36
  - - ! '>='
@@ -33,10 +38,15 @@ dependencies:
33
38
  version: 1.3.6
34
39
  type: :runtime
35
40
  prerelease: false
36
- version_requirements: *2152849500
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: 1.3.6
37
47
  - !ruby/object:Gem::Dependency
38
48
  name: pry
39
- requirement: &2152722880 !ruby/object:Gem::Requirement
49
+ requirement: !ruby/object:Gem::Requirement
40
50
  none: false
41
51
  requirements:
42
52
  - - ~>
@@ -44,10 +54,15 @@ dependencies:
44
54
  version: 0.9.9
45
55
  type: :development
46
56
  prerelease: false
47
- version_requirements: *2152722880
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ~>
61
+ - !ruby/object:Gem::Version
62
+ version: 0.9.9
48
63
  - !ruby/object:Gem::Dependency
49
64
  name: rails
50
- requirement: &2152722120 !ruby/object:Gem::Requirement
65
+ requirement: !ruby/object:Gem::Requirement
51
66
  none: false
52
67
  requirements:
53
68
  - - ! '>='
@@ -55,10 +70,15 @@ dependencies:
55
70
  version: 3.1.2
56
71
  type: :development
57
72
  prerelease: false
58
- version_requirements: *2152722120
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: 3.1.2
59
79
  - !ruby/object:Gem::Dependency
60
80
  name: rake
61
- requirement: &2152721240 !ruby/object:Gem::Requirement
81
+ requirement: !ruby/object:Gem::Requirement
62
82
  none: false
63
83
  requirements:
64
84
  - - ~>
@@ -66,10 +86,15 @@ dependencies:
66
86
  version: 0.9.2
67
87
  type: :development
68
88
  prerelease: false
69
- version_requirements: *2152721240
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ~>
93
+ - !ruby/object:Gem::Version
94
+ version: 0.9.2
70
95
  - !ruby/object:Gem::Dependency
71
96
  name: sqlite3
72
- requirement: &2152719600 !ruby/object:Gem::Requirement
97
+ requirement: !ruby/object:Gem::Requirement
73
98
  none: false
74
99
  requirements:
75
100
  - - ! '>='
@@ -77,10 +102,15 @@ dependencies:
77
102
  version: '0'
78
103
  type: :development
79
104
  prerelease: false
80
- version_requirements: *2152719600
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
81
111
  - !ruby/object:Gem::Dependency
82
112
  name: rspec
83
- requirement: &2152717500 !ruby/object:Gem::Requirement
113
+ requirement: !ruby/object:Gem::Requirement
84
114
  none: false
85
115
  requirements:
86
116
  - - ~>
@@ -88,10 +118,15 @@ dependencies:
88
118
  version: 2.10.0
89
119
  type: :development
90
120
  prerelease: false
91
- version_requirements: *2152717500
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ~>
125
+ - !ruby/object:Gem::Version
126
+ version: 2.10.0
92
127
  - !ruby/object:Gem::Dependency
93
128
  name: rspec-rails
94
- requirement: &2152733100 !ruby/object:Gem::Requirement
129
+ requirement: !ruby/object:Gem::Requirement
95
130
  none: false
96
131
  requirements:
97
132
  - - ~>
@@ -99,10 +134,15 @@ dependencies:
99
134
  version: 2.10.0
100
135
  type: :development
101
136
  prerelease: false
102
- version_requirements: *2152733100
137
+ version_requirements: !ruby/object:Gem::Requirement
138
+ none: false
139
+ requirements:
140
+ - - ~>
141
+ - !ruby/object:Gem::Version
142
+ version: 2.10.0
103
143
  - !ruby/object:Gem::Dependency
104
144
  name: capybara
105
- requirement: &2152732580 !ruby/object:Gem::Requirement
145
+ requirement: !ruby/object:Gem::Requirement
106
146
  none: false
107
147
  requirements:
108
148
  - - ~>
@@ -110,10 +150,15 @@ dependencies:
110
150
  version: 1.0.0
111
151
  type: :development
112
152
  prerelease: false
113
- version_requirements: *2152732580
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ none: false
155
+ requirements:
156
+ - - ~>
157
+ - !ruby/object:Gem::Version
158
+ version: 1.0.0
114
159
  - !ruby/object:Gem::Dependency
115
160
  name: pg
116
- requirement: &2152732000 !ruby/object:Gem::Requirement
161
+ requirement: !ruby/object:Gem::Requirement
117
162
  none: false
118
163
  requirements:
119
164
  - - ! '>='
@@ -121,10 +166,15 @@ dependencies:
121
166
  version: 0.11.0
122
167
  type: :development
123
168
  prerelease: false
124
- version_requirements: *2152732000
169
+ version_requirements: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: 0.11.0
125
175
  - !ruby/object:Gem::Dependency
126
176
  name: mysql2
127
- requirement: &2152731420 !ruby/object:Gem::Requirement
177
+ requirement: !ruby/object:Gem::Requirement
128
178
  none: false
129
179
  requirements:
130
180
  - - ~>
@@ -132,10 +182,15 @@ dependencies:
132
182
  version: 0.3.10
133
183
  type: :development
134
184
  prerelease: false
135
- version_requirements: *2152731420
185
+ version_requirements: !ruby/object:Gem::Requirement
186
+ none: false
187
+ requirements:
188
+ - - ~>
189
+ - !ruby/object:Gem::Version
190
+ version: 0.3.10
136
191
  - !ruby/object:Gem::Dependency
137
192
  name: delayed_job
138
- requirement: &2152730820 !ruby/object:Gem::Requirement
193
+ requirement: !ruby/object:Gem::Requirement
139
194
  none: false
140
195
  requirements:
141
196
  - - ~>
@@ -143,10 +198,15 @@ dependencies:
143
198
  version: '3.0'
144
199
  type: :development
145
200
  prerelease: false
146
- version_requirements: *2152730820
201
+ version_requirements: !ruby/object:Gem::Requirement
202
+ none: false
203
+ requirements:
204
+ - - ~>
205
+ - !ruby/object:Gem::Version
206
+ version: '3.0'
147
207
  - !ruby/object:Gem::Dependency
148
208
  name: delayed_job_active_record
149
- requirement: &2152730320 !ruby/object:Gem::Requirement
209
+ requirement: !ruby/object:Gem::Requirement
150
210
  none: false
151
211
  requirements:
152
212
  - - ! '>='
@@ -154,8 +214,14 @@ dependencies:
154
214
  version: '0'
155
215
  type: :development
156
216
  prerelease: false
157
- version_requirements: *2152730320
158
- description: Apartment allows Rails applications to deal with database multitenancy
217
+ version_requirements: !ruby/object:Gem::Requirement
218
+ none: false
219
+ requirements:
220
+ - - ! '>='
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ description: Apartment allows Rack applications to deal with database multitenancy
224
+ through ActiveRecord
159
225
  email:
160
226
  - ryan@ryanbrunner.com
161
227
  - bradleyrobertson@gmail.com
@@ -173,6 +239,7 @@ files:
173
239
  - README.md
174
240
  - Rakefile
175
241
  - apartment.gemspec
242
+ - circle.yml
176
243
  - lib/apartment.rb
177
244
  - lib/apartment/adapters/abstract_adapter.rb
178
245
  - lib/apartment/adapters/mysql2_adapter.rb
@@ -189,12 +256,14 @@ files:
189
256
  - lib/apartment/migrator.rb
190
257
  - lib/apartment/railtie.rb
191
258
  - lib/apartment/reloader.rb
259
+ - lib/apartment/sidekiq/client/database_middleware.rb
260
+ - lib/apartment/sidekiq/server/database_middleware.rb
192
261
  - lib/apartment/version.rb
193
262
  - lib/tasks/apartment.rake
194
263
  - spec/adapters/mysql2_adapter_spec.rb
195
264
  - spec/adapters/postgresql_adapter_spec.rb
196
265
  - spec/apartment_spec.rb
197
- - spec/config/database.yml
266
+ - spec/config/database.yml.sample
198
267
  - spec/database_spec.rb
199
268
  - spec/dummy/Rakefile
200
269
  - spec/dummy/app/controllers/application_controller.rb
@@ -206,7 +275,7 @@ files:
206
275
  - spec/dummy/config.ru
207
276
  - spec/dummy/config/application.rb
208
277
  - spec/dummy/config/boot.rb
209
- - spec/dummy/config/database.yml
278
+ - spec/dummy/config/database.yml.sample
210
279
  - spec/dummy/config/environment.rb
211
280
  - spec/dummy/config/environments/development.rb
212
281
  - spec/dummy/config/environments/production.rb
@@ -252,7 +321,7 @@ files:
252
321
  - spec/unit/middleware/subdomain_elevator_spec.rb
253
322
  - spec/unit/migrator_spec.rb
254
323
  - spec/unit/reloader_spec.rb
255
- homepage: http://github.com/bradrobertson/apartment
324
+ homepage: https://github.com/influitive/apartment
256
325
  licenses:
257
326
  - MIT
258
327
  post_install_message:
@@ -267,7 +336,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
267
336
  version: '0'
268
337
  segments:
269
338
  - 0
270
- hash: -2664931283611215158
339
+ hash: 345291853530360169
271
340
  required_rubygems_version: !ruby/object:Gem::Requirement
272
341
  none: false
273
342
  requirements:
@@ -276,11 +345,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
276
345
  version: '0'
277
346
  segments:
278
347
  - 0
279
- hash: -2664931283611215158
348
+ hash: 345291853530360169
280
349
  requirements: []
281
350
  rubyforge_project:
282
- rubygems_version: 1.8.10
351
+ rubygems_version: 1.8.24
283
352
  signing_key:
284
353
  specification_version: 3
285
- summary: A Ruby gem for managing database multitenancy in Rails applications
354
+ summary: A Ruby gem for managing database multitenancy
286
355
  test_files: []