apartment 0.1.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +2 -0
  3. data/.rvmrc +1 -0
  4. data/Gemfile +5 -12
  5. data/HISTORY.md +31 -0
  6. data/{README.markdown → README.md} +39 -21
  7. data/Rakefile +44 -47
  8. data/apartment.gemspec +19 -58
  9. data/lib/apartment.rb +57 -6
  10. data/lib/apartment/adapters/abstract_adapter.rb +106 -0
  11. data/lib/apartment/adapters/mysql_adapter.rb +11 -0
  12. data/lib/apartment/adapters/postgresql_adapter.rb +55 -0
  13. data/lib/apartment/database.rb +58 -74
  14. data/lib/apartment/elevators/subdomain.rb +27 -0
  15. data/lib/apartment/migrator.rb +23 -0
  16. data/lib/apartment/railtie.rb +1 -2
  17. data/lib/apartment/version.rb +3 -0
  18. data/lib/tasks/apartment.rake +36 -0
  19. data/spec/apartment_spec.rb +7 -0
  20. data/spec/config/database.yml +10 -0
  21. data/spec/dummy/Rakefile +7 -0
  22. data/spec/dummy/app/controllers/application_controller.rb +6 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/models/company.rb +3 -0
  25. data/spec/dummy/app/models/user.rb +3 -0
  26. data/spec/dummy/app/views/application/index.html.erb +1 -0
  27. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  28. data/spec/dummy/config.ru +4 -0
  29. data/spec/dummy/config/application.rb +47 -0
  30. data/spec/dummy/config/boot.rb +10 -0
  31. data/spec/dummy/config/database.yml +8 -0
  32. data/spec/dummy/config/environment.rb +5 -0
  33. data/spec/dummy/config/environments/development.rb +26 -0
  34. data/spec/dummy/config/environments/production.rb +49 -0
  35. data/spec/dummy/config/environments/test.rb +35 -0
  36. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/spec/dummy/config/initializers/inflections.rb +10 -0
  38. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  39. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  40. data/spec/dummy/config/initializers/session_store.rb +8 -0
  41. data/spec/dummy/config/locales/en.yml +5 -0
  42. data/spec/dummy/config/routes.rb +3 -0
  43. data/spec/dummy/db/migrate/20110613152810_create_dummy_models.rb +20 -0
  44. data/spec/dummy/db/schema.rb +26 -0
  45. data/spec/dummy/db/seeds.rb +8 -0
  46. data/spec/dummy/db/test.sqlite3 +0 -0
  47. data/spec/dummy/public/404.html +26 -0
  48. data/spec/dummy/public/422.html +26 -0
  49. data/spec/dummy/public/500.html +26 -0
  50. data/{lib/apartment/associations/multi_tenant_association.rb → spec/dummy/public/favicon.ico} +0 -0
  51. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  52. data/spec/dummy/script/rails +6 -0
  53. data/spec/integration/adapters/postgresql_integration_spec.rb +73 -0
  54. data/spec/integration/apartment_rake_integration_spec.rb +62 -0
  55. data/spec/integration/database_integration_spec.rb +107 -0
  56. data/spec/integration/middleware/subdomain_elevator_spec.rb +63 -0
  57. data/spec/integration/setup_spec.rb +8 -0
  58. data/spec/spec_helper.rb +26 -0
  59. data/spec/support/apartment_helpers.rb +24 -0
  60. data/spec/support/capybara_sessions.rb +15 -0
  61. data/spec/support/config.rb +11 -0
  62. data/spec/support/migrate.rb +9 -0
  63. data/spec/tasks/apartment_rake_spec.rb +110 -0
  64. data/spec/unit/config_spec.rb +84 -0
  65. data/spec/unit/middleware/subdomain_elevator_spec.rb +20 -0
  66. data/spec/unit/migrator_spec.rb +87 -0
  67. metadata +112 -62
  68. data/.bundle/config +0 -2
  69. data/.project +0 -11
  70. data/Gemfile.lock +0 -22
  71. data/VERSION +0 -1
  72. data/lib/apartment/config.rb +0 -10
  73. data/lib/apartment/config/default_config.yml +0 -17
  74. data/lib/tasks/multi_tenant_migrate.rake +0 -14
  75. data/pkg/apartment-0.1.3.gem +0 -0
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apartment::Elevators::Subdomain do
4
+
5
+ describe "#subdomain" do
6
+ it "should parse subdomain" do
7
+ request = ActionDispatch::Request.new('HTTP_HOST' => 'foo.bar.com')
8
+ elevator = Apartment::Elevators::Subdomain.new(nil)
9
+ elevator.subdomain(request).should == 'foo'
10
+ end
11
+
12
+ it "should return nil when no subdomain" do
13
+ request = ActionDispatch::Request.new('HTTP_HOST' => 'bar.com')
14
+ elevator = Apartment::Elevators::Subdomain.new(nil)
15
+ elevator.subdomain(request).should be_nil
16
+ end
17
+
18
+ end
19
+
20
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apartment::Migrator do
4
+
5
+ let(:config){ Apartment::Test.config['connections']['postgresql'].symbolize_keys }
6
+ let(:schema_name){ 'some_db_schema' }
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
+
9
+ before do
10
+ ActiveRecord::Base.establish_connection config
11
+ Apartment::Database.stub(:config).and_return config # Use postgresql config for this test
12
+ @original_schema = ActiveRecord::Base.connection.schema_search_path
13
+
14
+ Apartment.configure do |config|
15
+ config.use_postgres_schemas = true
16
+ config.excluded_models = []
17
+ config.database_names = [schema_name]
18
+ end
19
+
20
+ Apartment::Database.create schema_name # create the schema
21
+ migrations_path = Rails.root + ActiveRecord::Migrator.migrations_path # tell AR where the real migrations are
22
+ ActiveRecord::Migrator.stub(:migrations_path).and_return(migrations_path)
23
+ end
24
+
25
+ after do
26
+ Apartment::Test.drop_schema(schema_name)
27
+ end
28
+
29
+ context "postgresql" do
30
+
31
+ context "using schemas" do
32
+
33
+ describe "#migrate" do
34
+ it "should connect to new db, then reset when done" do
35
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
36
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
37
+ Apartment::Migrator.migrate(schema_name)
38
+ end
39
+
40
+ it "should migrate db" do
41
+ ActiveRecord::Migrator.should_receive(:migrate)
42
+ Apartment::Migrator.migrate(schema_name)
43
+ end
44
+ end
45
+
46
+ describe "#run" do
47
+ context "up" do
48
+
49
+ it "should connect to new db, then reset when done" do
50
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
51
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
52
+ Apartment::Migrator.run(:up, schema_name, version)
53
+ end
54
+
55
+ it "should migrate to a version" do
56
+ ActiveRecord::Migrator.should_receive(:run).with(:up, anything, version)
57
+ Apartment::Migrator.run(:up, schema_name, version)
58
+ end
59
+ end
60
+
61
+ describe "down" do
62
+
63
+ it "should connect to new db, then reset when done" do
64
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(schema_name).once
65
+ ActiveRecord::Base.connection.should_receive(:schema_search_path=).with(@original_schema).once
66
+ Apartment::Migrator.run(:down, schema_name, version)
67
+ end
68
+
69
+ it "should migrate to a version" do
70
+ ActiveRecord::Migrator.should_receive(:run).with(:down, anything, version)
71
+ Apartment::Migrator.run(:down, schema_name, version)
72
+ end
73
+ end
74
+ end
75
+
76
+ describe "#rollback" do
77
+ let(:steps){ 3 }
78
+
79
+ it "should rollback the db" do
80
+ ActiveRecord::Migrator.should_receive(:rollback).with(anything, steps)
81
+ Apartment::Migrator.rollback(schema_name, steps)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ end
metadata CHANGED
@@ -1,15 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apartment
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 3
9
- version: 0.1.3
4
+ prerelease:
5
+ version: 0.5.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Ryan Brunner
9
+ - Brad Robertson
13
10
  autorequire:
14
11
  bindir: bin
15
12
  cert_chain: []
@@ -18,104 +15,162 @@ date: 2011-04-18 00:00:00 -04:00
18
15
  default_executable:
19
16
  dependencies:
20
17
  - !ruby/object:Gem::Dependency
21
- name: activesupport
18
+ name: rails
19
+ prerelease: false
22
20
  requirement: &id001 !ruby/object:Gem::Requirement
23
21
  none: false
24
22
  requirements:
25
- - - ">="
23
+ - - ~>
26
24
  - !ruby/object:Gem::Version
27
- segments:
28
- - 2
29
- - 3
30
- - 5
31
- version: 2.3.5
25
+ version: 3.0.8
32
26
  type: :runtime
33
- prerelease: false
34
27
  version_requirements: *id001
35
28
  - !ruby/object:Gem::Dependency
36
- name: shoulda
29
+ name: sqlite3
30
+ prerelease: false
37
31
  requirement: &id002 !ruby/object:Gem::Requirement
38
32
  none: false
39
33
  requirements:
40
34
  - - ">="
41
35
  - !ruby/object:Gem::Version
42
- segments:
43
- - 0
44
36
  version: "0"
45
37
  type: :development
46
- prerelease: false
47
38
  version_requirements: *id002
48
39
  - !ruby/object:Gem::Dependency
49
- name: bundler
40
+ name: rspec
41
+ prerelease: false
50
42
  requirement: &id003 !ruby/object:Gem::Requirement
51
43
  none: false
52
44
  requirements:
53
45
  - - ~>
54
46
  - !ruby/object:Gem::Version
55
- segments:
56
- - 1
57
- - 0
58
- - 0
59
- version: 1.0.0
47
+ version: 2.6.0
60
48
  type: :development
61
- prerelease: false
62
49
  version_requirements: *id003
63
50
  - !ruby/object:Gem::Dependency
64
- name: jeweler
51
+ name: rspec-rails
52
+ prerelease: false
65
53
  requirement: &id004 !ruby/object:Gem::Requirement
66
54
  none: false
67
55
  requirements:
68
56
  - - ~>
69
57
  - !ruby/object:Gem::Version
70
- segments:
71
- - 1
72
- - 5
73
- - 1
74
- version: 1.5.1
58
+ version: 2.6.1
75
59
  type: :development
76
- prerelease: false
77
60
  version_requirements: *id004
78
61
  - !ruby/object:Gem::Dependency
79
- name: rcov
62
+ name: capybara
63
+ prerelease: false
80
64
  requirement: &id005 !ruby/object:Gem::Requirement
81
65
  none: false
82
66
  requirements:
83
- - - ">="
67
+ - - "="
84
68
  - !ruby/object:Gem::Version
85
- segments:
86
- - 0
87
- version: "0"
69
+ version: 1.0.0
88
70
  type: :development
89
- prerelease: false
90
71
  version_requirements: *id005
91
- description: |-
92
- Apartment allows Rails applications to deal with
93
- multitenancy.
94
- email: ryan@ryanbrunner.com
72
+ - !ruby/object:Gem::Dependency
73
+ name: pg
74
+ prerelease: false
75
+ requirement: &id006 !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ~>
79
+ - !ruby/object:Gem::Version
80
+ version: 0.11.0
81
+ type: :development
82
+ version_requirements: *id006
83
+ - !ruby/object:Gem::Dependency
84
+ name: silent-postgres
85
+ prerelease: false
86
+ requirement: &id007 !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: 0.0.8
92
+ type: :development
93
+ version_requirements: *id007
94
+ description: Apartment allows Rails applications to deal with database multitenancy
95
+ email:
96
+ - ryan@ryanbrunner.com
97
+ - bradleyrobertson@gmail.com
95
98
  executables: []
96
99
 
97
100
  extensions: []
98
101
 
99
- extra_rdoc_files:
100
- - README.markdown
102
+ extra_rdoc_files: []
103
+
101
104
  files:
102
- - .bundle/config
103
- - .project
105
+ - .gitignore
106
+ - .rspec
107
+ - .rvmrc
104
108
  - Gemfile
105
- - Gemfile.lock
106
- - README.markdown
109
+ - HISTORY.md
110
+ - README.md
107
111
  - Rakefile
108
- - VERSION
109
- - apartment-0.1.3.gem
110
112
  - apartment.gemspec
111
113
  - lib/apartment.rb
112
- - lib/apartment/associations/multi_tenant_association.rb
113
- - lib/apartment/config.rb
114
- - lib/apartment/config/default_config.yml
114
+ - lib/apartment/adapters/abstract_adapter.rb
115
+ - lib/apartment/adapters/mysql_adapter.rb
116
+ - lib/apartment/adapters/postgresql_adapter.rb
115
117
  - lib/apartment/database.rb
118
+ - lib/apartment/elevators/subdomain.rb
119
+ - lib/apartment/migrator.rb
116
120
  - lib/apartment/railtie.rb
117
- - lib/tasks/multi_tenant_migrate.rake
118
- - pkg/apartment-0.1.3.gem
121
+ - lib/apartment/version.rb
122
+ - lib/tasks/apartment.rake
123
+ - spec/apartment_spec.rb
124
+ - spec/config/database.yml
125
+ - spec/dummy/Rakefile
126
+ - spec/dummy/app/controllers/application_controller.rb
127
+ - spec/dummy/app/helpers/application_helper.rb
128
+ - spec/dummy/app/models/company.rb
129
+ - spec/dummy/app/models/user.rb
130
+ - spec/dummy/app/views/application/index.html.erb
131
+ - spec/dummy/app/views/layouts/application.html.erb
132
+ - spec/dummy/config.ru
133
+ - spec/dummy/config/application.rb
134
+ - spec/dummy/config/boot.rb
135
+ - spec/dummy/config/database.yml
136
+ - spec/dummy/config/environment.rb
137
+ - spec/dummy/config/environments/development.rb
138
+ - spec/dummy/config/environments/production.rb
139
+ - spec/dummy/config/environments/test.rb
140
+ - spec/dummy/config/initializers/backtrace_silencers.rb
141
+ - spec/dummy/config/initializers/inflections.rb
142
+ - spec/dummy/config/initializers/mime_types.rb
143
+ - spec/dummy/config/initializers/secret_token.rb
144
+ - spec/dummy/config/initializers/session_store.rb
145
+ - spec/dummy/config/locales/en.yml
146
+ - spec/dummy/config/routes.rb
147
+ - spec/dummy/db/migrate/20110613152810_create_dummy_models.rb
148
+ - spec/dummy/db/schema.rb
149
+ - spec/dummy/db/seeds.rb
150
+ - spec/dummy/db/test.sqlite3
151
+ - spec/dummy/log/development.log
152
+ - spec/dummy/log/production.log
153
+ - spec/dummy/log/server.log
154
+ - spec/dummy/public/404.html
155
+ - spec/dummy/public/422.html
156
+ - spec/dummy/public/500.html
157
+ - spec/dummy/public/favicon.ico
158
+ - spec/dummy/public/stylesheets/.gitkeep
159
+ - spec/dummy/script/rails
160
+ - spec/integration/adapters/postgresql_integration_spec.rb
161
+ - spec/integration/apartment_rake_integration_spec.rb
162
+ - spec/integration/database_integration_spec.rb
163
+ - spec/integration/middleware/subdomain_elevator_spec.rb
164
+ - spec/integration/setup_spec.rb
165
+ - spec/spec_helper.rb
166
+ - spec/support/apartment_helpers.rb
167
+ - spec/support/capybara_sessions.rb
168
+ - spec/support/config.rb
169
+ - spec/support/migrate.rb
170
+ - spec/tasks/apartment_rake_spec.rb
171
+ - spec/unit/config_spec.rb
172
+ - spec/unit/middleware/subdomain_elevator_spec.rb
173
+ - spec/unit/migrator_spec.rb
119
174
  has_rdoc: true
120
175
  homepage: http://github.com/ryanbrunner/apartment
121
176
  licenses:
@@ -130,24 +185,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
185
  requirements:
131
186
  - - ">="
132
187
  - !ruby/object:Gem::Version
133
- hash: -190181027694817836
134
- segments:
135
- - 0
136
188
  version: "0"
137
189
  required_rubygems_version: !ruby/object:Gem::Requirement
138
190
  none: false
139
191
  requirements:
140
192
  - - ">="
141
193
  - !ruby/object:Gem::Version
142
- segments:
143
- - 0
144
194
  version: "0"
145
195
  requirements: []
146
196
 
147
197
  rubyforge_project:
148
- rubygems_version: 1.3.7
198
+ rubygems_version: 1.6.2
149
199
  signing_key:
150
200
  specification_version: 3
151
- summary: A Ruby gem for managing multitenancy in Rails applications
201
+ summary: A Ruby gem for managing database multitenancy in Rails applications
152
202
  test_files: []
153
203
 
@@ -1,2 +0,0 @@
1
- ---
2
- BUNDLE_DISABLE_SHARED_GEMS: "1"
data/.project DELETED
@@ -1,11 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <projectDescription>
3
- <name>apartment</name>
4
- <comment></comment>
5
- <projects>
6
- </projects>
7
- <buildSpec>
8
- </buildSpec>
9
- <natures>
10
- </natures>
11
- </projectDescription>
@@ -1,22 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activesupport (3.0.3)
5
- git (1.2.5)
6
- jeweler (1.5.1)
7
- bundler (~> 1.0.0)
8
- git (>= 1.2.5)
9
- rake
10
- rake (0.8.7)
11
- rcov (0.9.9)
12
- shoulda (2.11.3)
13
-
14
- PLATFORMS
15
- ruby
16
-
17
- DEPENDENCIES
18
- activesupport (>= 2.3.5)
19
- bundler (~> 1.0.0)
20
- jeweler (~> 1.5.1)
21
- rcov
22
- shoulda
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.1.3
@@ -1,10 +0,0 @@
1
- module Apartment
2
- config = {:excluded_models => ["User"],
3
- :use_postgres_schemas => true
4
- }
5
-
6
- if File.exists? "config/apartment.yml"
7
- config = config.merge YAML.load_file "config/apartment.yml"
8
- Config = OpenStruct.new(config)
9
- end
10
- end
@@ -1,17 +0,0 @@
1
- # Apartment configuration
2
-
3
- # Excluded models
4
- # Indicate which models should be excluded from multi-tenancy. If you have information
5
- # that does not vary between databases, or information that should be considered global,
6
- # specify it here. Any classes involved in determining which database to access MUST be
7
- # specified here
8
-
9
- excluded_models: [User]
10
-
11
- # Postgres integration mode
12
- # By default, apartment switches databases in Postgres by changing the schema search path
13
- # of the current environment database. If preferred, this can be turned off and Postgres will
14
- # look in different databases for multi-tenanted models.
15
-
16
- use_postgres_schemas: true
17
-
@@ -1,14 +0,0 @@
1
- namespace :apartment do
2
- desc "Apply database changes to all multi-tenant databases"
3
- task :migrate => :environment do |t, args|
4
-
5
- puts "[Migrating to default environment]"
6
- ActiveRecord::Migrator.migrate(File.join(Rails.root, 'db', 'migrate'))
7
-
8
- Admin::Company.where("database is not null").select("distinct database").each do |u|
9
- puts "[Migrating to #{u.database}]"
10
-
11
- Apartment::Database.migrate u.database
12
- end
13
- end
14
- end