apartment 0.10.1 → 0.10.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +0 -5
- data/HISTORY.md +5 -0
- data/README.md +39 -25
- data/Rakefile +35 -8
- data/apartment.gemspec +5 -3
- data/lib/apartment.rb +2 -1
- data/lib/apartment/adapters/abstract_adapter.rb +17 -19
- data/lib/apartment/adapters/mysql_adapter.rb +7 -0
- data/lib/apartment/adapters/postgresql_adapter.rb +27 -32
- data/lib/apartment/version.rb +1 -1
- data/spec/adapters/mysql_adapter_spec.rb +36 -0
- data/spec/{integration/adapters/postgresql_integration_spec.rb → adapters/postgresql_adapter_spec.rb} +30 -23
- data/spec/apartment_spec.rb +4 -0
- data/spec/config/database.yml +6 -4
- data/spec/dummy/db/schema.rb +1 -0
- data/spec/integration/database_integration_spec.rb +1 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/support/apartment_helpers.rb +6 -0
- metadata +45 -23
- data/spec/integration/setup_spec.rb +0 -8
data/Gemfile
CHANGED
@@ -1,8 +1,3 @@
|
|
1
1
|
source "http://rubygems.org"
|
2
2
|
|
3
|
-
# Uses patched version of postgresql adapter to enable apartment usage.
|
4
|
-
# Current postgresql adapter in rails doesn't properly support the use of postgresql schemas
|
5
|
-
# Pull request sent => https://github.com/rails/rails/pull/1604
|
6
|
-
gem 'rails', '3.0.8', :git => 'git://github.com/bradrobertson/rails', :branch => '3-0-stable'
|
7
|
-
|
8
3
|
gemspec
|
data/HISTORY.md
CHANGED
data/README.md
CHANGED
@@ -2,16 +2,9 @@
|
|
2
2
|
*Multitenancy for Rails 3*
|
3
3
|
|
4
4
|
Apartment provides tools to help you deal with multiple databases in your Rails
|
5
|
-
|
5
|
+
application. If you need to have certain data sequestered based on account or company,
|
6
6
|
but still allow some data to exist in a common database, Apartment can help.
|
7
7
|
|
8
|
-
## Caveats
|
9
|
-
|
10
|
-
Apartment was built to deal with a very particular use-case - the need to spin up
|
11
|
-
multiple databases within the same application instance on-demand while Rails is running.
|
12
|
-
If your setup can accomodate creating new databases on deploy (by adding a new database to your
|
13
|
-
database.yml), or doesn't need 100% database isolation, other solutions might be far simpler
|
14
|
-
for your use case.
|
15
8
|
|
16
9
|
## Installation
|
17
10
|
|
@@ -19,11 +12,13 @@ for your use case.
|
|
19
12
|
|
20
13
|
Add the following to your Gemfile:
|
21
14
|
|
22
|
-
|
15
|
+
gem 'apartment'
|
23
16
|
|
24
17
|
That's all you need to set up the Apartment libraries. If you want to switch databases
|
25
18
|
on a per-user basis, look under "Usage - Switching databases per request", below.
|
26
19
|
|
20
|
+
*NOTE: If using [postgresl schemas](http://www.postgresql.org/docs/9.0/static/ddl-schemas.html) you must use Rails >= 3.0.10, it contains a [patch](https://github.com/rails/rails/pull/1607) that has better postgresql schema support*
|
21
|
+
|
27
22
|
## Usage
|
28
23
|
|
29
24
|
### Creating new Databases
|
@@ -31,10 +26,10 @@ on a per-user basis, look under "Usage - Switching databases per request", below
|
|
31
26
|
Before you can switch to a new apartment database, you will need to create it. Whenever
|
32
27
|
you need to create a new database, you can run the following command:
|
33
28
|
|
34
|
-
|
29
|
+
Apartment::Database.create('database_name')
|
35
30
|
|
36
|
-
Apartment will create a new database in the following format: "
|
37
|
-
In the case of a sqlite database, this will be created in your 'db/migrate'
|
31
|
+
Apartment will create a new database in the following format: "_environment_\_database_name".
|
32
|
+
In the case of a sqlite database, this will be created in your 'db/migrate' folder. With
|
38
33
|
other databases, the database will be created as a new DB within the system.
|
39
34
|
|
40
35
|
When you create a new database, all migrations will be run against that database, so it will be
|
@@ -43,10 +38,12 @@ up to date when create returns.
|
|
43
38
|
#### Notes on PostgreSQL
|
44
39
|
|
45
40
|
PostgreSQL works slightly differently than other databases when creating a new DB. If you
|
46
|
-
are using PostgreSQL, Apartment will set up a new **schema** and migrate into there. This
|
41
|
+
are using PostgreSQL, Apartment by default will set up a new **schema** and migrate into there. This
|
47
42
|
provides better performance, and allows Apartment to work on systems like Heroku, which
|
48
43
|
would not allow a full new database to be created.
|
49
44
|
|
45
|
+
One can optionally use the full database creation instead if they want, though this is not recommended
|
46
|
+
|
50
47
|
### Switching Databases
|
51
48
|
|
52
49
|
To switch databases using Apartment, use the following command:
|
@@ -72,14 +69,32 @@ to a database schema of the same name. It can be used like so:
|
|
72
69
|
end
|
73
70
|
end
|
74
71
|
|
72
|
+
## Config
|
73
|
+
|
74
|
+
The following config options should be set up in a Rails initializer such as:
|
75
|
+
|
76
|
+
config/initializers/apartment.rb
|
77
|
+
|
78
|
+
To set config options, add this to your initializer:
|
79
|
+
|
80
|
+
Apartment.configure do |config|
|
81
|
+
# set your options (described below) here
|
82
|
+
end
|
83
|
+
|
75
84
|
### Excluding models
|
76
85
|
|
77
86
|
If you have some models that should always access the 'root' database, you can specify this by configuring
|
78
87
|
Apartment using `Apartment.configure`. This will yield a config object for you. You can set excluded models like so:
|
88
|
+
|
89
|
+
config.excluded_models = [User, Company] # these models will not be multi-tenanted, but remain in the global (public) namespace
|
90
|
+
|
91
|
+
### Handling Environments
|
79
92
|
|
80
|
-
|
81
|
-
|
82
|
-
|
93
|
+
By default, when not using postgresql schemas, Apartment will prepend the environment to the database name
|
94
|
+
to ensure there is no conflict between your environments. This is mainly for the benefit of your development
|
95
|
+
and test environments. If you wish to turn this option off in production, you could do something like:
|
96
|
+
|
97
|
+
config.prepend_environment = !Rails.env.production?
|
83
98
|
|
84
99
|
### Managing Migrations
|
85
100
|
|
@@ -88,14 +103,10 @@ of dbs to Apartment. You can make this dynamic by providing a Proc object to be
|
|
88
103
|
This object should yield an array of string representing each database name. Example:
|
89
104
|
|
90
105
|
# Dynamically get database names to migrate
|
91
|
-
|
92
|
-
config.database_names = lambda{ Company.all.collect(&:database_name) }
|
93
|
-
end
|
106
|
+
config.database_names = lambda{ Customer.select(:database_name).map(&:database_name) }
|
94
107
|
|
95
108
|
# Use a static list of database names for migrate
|
96
|
-
|
97
|
-
config.database_names = ['db1', 'db2']
|
98
|
-
end
|
109
|
+
config.database_names = ['db1', 'db2']
|
99
110
|
|
100
111
|
You can then migration your databases using the rake task:
|
101
112
|
|
@@ -128,8 +139,11 @@ that a `database` attribute is set on this model *before* it is serialized, to e
|
|
128
139
|
end
|
129
140
|
end
|
130
141
|
|
131
|
-
##
|
142
|
+
## Contributing
|
132
143
|
|
133
|
-
*
|
144
|
+
* Please issue pull requests to the `development` branch. All development happens here, master is used for releases
|
145
|
+
* Ensure that your code is accompanied with tests. No code will be merged without tests
|
134
146
|
|
135
|
-
##
|
147
|
+
## TODO
|
148
|
+
|
149
|
+
* Shared examples for testing to ensure consistency across all adapters
|
data/Rakefile
CHANGED
@@ -11,7 +11,7 @@ end
|
|
11
11
|
|
12
12
|
namespace :spec do
|
13
13
|
|
14
|
-
[:tasks, :unit, :integration].each do |type|
|
14
|
+
[:tasks, :unit, :adapters, :integration].each do |type|
|
15
15
|
RSpec::Core::RakeTask.new(type) do |spec|
|
16
16
|
spec.pattern = "spec/#{type}/**/*_spec.rb"
|
17
17
|
end
|
@@ -33,19 +33,46 @@ namespace :postgres do
|
|
33
33
|
|
34
34
|
desc 'Build the PostgreSQL test databases'
|
35
35
|
task :build_db do
|
36
|
-
%x{ createdb -E UTF8 #{
|
37
|
-
ActiveRecord::Base.establish_connection
|
36
|
+
%x{ createdb -E UTF8 #{pg_config['database']} } rescue "test db already exists"
|
37
|
+
ActiveRecord::Base.establish_connection pg_config
|
38
38
|
load 'spec/dummy/db/schema.rb'
|
39
39
|
end
|
40
40
|
|
41
41
|
desc "drop the PostgreSQL test database"
|
42
42
|
task :drop_db do
|
43
|
-
puts "dropping database #{
|
44
|
-
%x{ dropdb #{
|
43
|
+
puts "dropping database #{pg_config['database']}"
|
44
|
+
%x{ dropdb #{pg_config['database']} }
|
45
45
|
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
namespace :mysql do
|
50
|
+
require 'active_record'
|
51
|
+
require "#{File.join(File.dirname(__FILE__), 'spec', 'support', 'config')}"
|
46
52
|
|
47
|
-
|
48
|
-
|
53
|
+
desc 'Build the MySQL test databases'
|
54
|
+
task :build_db do
|
55
|
+
%x{ mysqladmin -u root create #{my_config['database']} } rescue "test db already exists"
|
56
|
+
ActiveRecord::Base.establish_connection my_config
|
57
|
+
load 'spec/dummy/db/schema.rb'
|
49
58
|
end
|
50
59
|
|
51
|
-
|
60
|
+
desc "drop the MySQL test database"
|
61
|
+
task :drop_db do
|
62
|
+
puts "dropping database #{my_config['database']}"
|
63
|
+
%x{ mysqladmin -u root drop #{my_config['database']} }
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
def config
|
69
|
+
Apartment::Test.config['connections']
|
70
|
+
end
|
71
|
+
|
72
|
+
def pg_config
|
73
|
+
config['postgresql']
|
74
|
+
end
|
75
|
+
|
76
|
+
def my_config
|
77
|
+
config['mysql']
|
78
|
+
end
|
data/apartment.gemspec
CHANGED
@@ -14,17 +14,19 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.files = `git ls-files`.split("\n")
|
15
15
|
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
16
16
|
|
17
|
-
s.homepage = %q{http://github.com/
|
17
|
+
s.homepage = %q{http://github.com/bradrobertson/apartment}
|
18
18
|
s.licenses = ["MIT"]
|
19
19
|
s.require_paths = ["lib"]
|
20
20
|
s.rubygems_version = %q{1.3.7}
|
21
21
|
|
22
|
-
s.add_dependency 'rails', '
|
22
|
+
s.add_dependency 'rails', '>= 3.0.10'
|
23
|
+
s.add_development_dependency 'rake', '~> 0.8.7'
|
23
24
|
s.add_development_dependency 'sqlite3'
|
24
25
|
s.add_development_dependency 'rspec', '~> 2.6.0'
|
25
26
|
s.add_development_dependency 'rspec-rails', '~> 2.6.1'
|
26
27
|
s.add_development_dependency 'capybara', '1.0.0'
|
27
28
|
s.add_development_dependency 'pg', '~> 0.11.0'
|
28
|
-
s.add_development_dependency
|
29
|
+
s.add_development_dependency 'mysql2', '0.2.7'
|
30
|
+
s.add_development_dependency "silent-postgres", "~> 0.1.1"
|
29
31
|
s.add_development_dependency 'delayed_job', '~> 2.1.4'
|
30
32
|
end
|
data/lib/apartment.rb
CHANGED
@@ -3,7 +3,7 @@ require 'apartment/railtie'
|
|
3
3
|
module Apartment
|
4
4
|
|
5
5
|
class << self
|
6
|
-
attr_accessor :use_postgres_schemas, :seed_after_create
|
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
|
@@ -71,4 +71,5 @@ Apartment.configure do |config|
|
|
71
71
|
config.use_postgres_schemas = true
|
72
72
|
config.database_names = []
|
73
73
|
config.seed_after_create = false
|
74
|
+
config.prepend_environment = true
|
74
75
|
end
|
@@ -10,14 +10,14 @@ module Apartment
|
|
10
10
|
# @param {Hash} config Database config
|
11
11
|
# @param {Hash} defaults Some default options
|
12
12
|
#
|
13
|
-
def initialize(config, defaults)
|
13
|
+
def initialize(config, defaults = {})
|
14
14
|
@config = config
|
15
15
|
@defaults = defaults
|
16
16
|
end
|
17
17
|
|
18
|
-
# Connect to db
|
18
|
+
# Connect to db, do your biz, switch back to previous db
|
19
19
|
#
|
20
|
-
# @param {String?} database Database or schema to connect to
|
20
|
+
# @param {String?} database Database or schema to connect to
|
21
21
|
def process(database = nil)
|
22
22
|
current_db = current_database
|
23
23
|
switch(database)
|
@@ -30,14 +30,11 @@ module Apartment
|
|
30
30
|
#
|
31
31
|
# @param {String} database Database name
|
32
32
|
def create(database)
|
33
|
-
|
33
|
+
ActiveRecord::Base.connection.execute("CREATE DATABASE #{environmentify(sanitize(database))}")
|
34
34
|
|
35
35
|
process(database) do
|
36
36
|
import_database_schema
|
37
37
|
|
38
|
-
# Manually init schema migrations table (apparently there were issues with Postgres when this isn't done)
|
39
|
-
ActiveRecord::Base.connection.initialize_schema_migrations_table
|
40
|
-
|
41
38
|
# Seed data if appropriate
|
42
39
|
seed_data if Apartment.seed_after_create
|
43
40
|
end
|
@@ -55,6 +52,13 @@ module Apartment
|
|
55
52
|
|
56
53
|
connect_to_new(database)
|
57
54
|
end
|
55
|
+
|
56
|
+
def environmentify(database)
|
57
|
+
# prepend the environment if configured and the environment isn't already there
|
58
|
+
return "#{Rails.env}_#{database}" if Apartment.prepend_environment && !database.include?(Rails.env)
|
59
|
+
|
60
|
+
database
|
61
|
+
end
|
58
62
|
|
59
63
|
def seed_data
|
60
64
|
load_or_abort("#{Rails.root}/db/seeds.rb")
|
@@ -68,10 +72,6 @@ module Apartment
|
|
68
72
|
|
69
73
|
protected
|
70
74
|
|
71
|
-
def create_schema
|
72
|
-
# noop
|
73
|
-
end
|
74
|
-
|
75
75
|
def connect_to_new(database)
|
76
76
|
ActiveRecord::Base.establish_connection multi_tenantify(database)
|
77
77
|
end
|
@@ -83,7 +83,7 @@ module Apartment
|
|
83
83
|
# Return a new config that is multi-tenanted
|
84
84
|
def multi_tenantify(database)
|
85
85
|
@config.clone.tap do |config|
|
86
|
-
config[
|
86
|
+
config[:database] = environmentify(database)
|
87
87
|
end
|
88
88
|
end
|
89
89
|
|
@@ -92,14 +92,12 @@ module Apartment
|
|
92
92
|
database.gsub(/[\W]/,'')
|
93
93
|
end
|
94
94
|
|
95
|
-
# Whether or not to use postgresql schemas
|
96
|
-
def using_schemas?
|
97
|
-
false
|
98
|
-
end
|
99
|
-
|
100
95
|
def load_or_abort(file)
|
101
96
|
if File.exists?(file)
|
102
|
-
|
97
|
+
# Don't log the output of loading files (such as schema or seeds)
|
98
|
+
silence_stream(STDOUT) do
|
99
|
+
load(file)
|
100
|
+
end
|
103
101
|
else
|
104
102
|
abort %{#{file} doesn't exist yet}
|
105
103
|
end
|
@@ -107,4 +105,4 @@ module Apartment
|
|
107
105
|
|
108
106
|
end
|
109
107
|
end
|
110
|
-
end
|
108
|
+
end
|
@@ -3,56 +3,51 @@ module Apartment
|
|
3
3
|
module Database
|
4
4
|
|
5
5
|
def self.postgresql_adapter(config)
|
6
|
-
|
6
|
+
Apartment.use_postgres_schemas ?
|
7
|
+
Adapters::PostgresqlSchemaAdapter.new(config, :schema_search_path => ActiveRecord::Base.connection.schema_search_path) :
|
8
|
+
Adapters::PostgresqlAdapter.new(config)
|
7
9
|
end
|
8
10
|
end
|
9
11
|
|
10
12
|
module Adapters
|
11
13
|
|
14
|
+
# Default adapter when not using Postgresql Schemas
|
12
15
|
class PostgresqlAdapter < AbstractAdapter
|
16
|
+
end
|
17
|
+
|
18
|
+
# Separate Adapter for Postgresql when using schemas
|
19
|
+
class PostgresqlSchemaAdapter < AbstractAdapter
|
13
20
|
|
14
21
|
# Set schema path or connect to new db
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
22
|
+
# TODO sanitize method doesn't work with schemas as the default schema uses "$user", stripping out the quotes makes it fail
|
23
|
+
def connect_to_new(database = nil)
|
24
|
+
return reset if database.nil?
|
25
|
+
ActiveRecord::Base.connection.schema_search_path = database
|
19
26
|
rescue ActiveRecord::StatementInvalid => e
|
20
27
|
raise SchemaNotFound, e
|
21
28
|
end
|
22
29
|
|
23
30
|
def create(database)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
31
|
+
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{database}")
|
32
|
+
|
33
|
+
process(database) do
|
34
|
+
import_database_schema
|
35
|
+
|
36
|
+
# Seed data if appropriate
|
37
|
+
seed_data if Apartment.seed_after_create
|
38
|
+
end
|
39
|
+
rescue ActiveRecord::StatementInvalid => e
|
40
|
+
raise SchemaExists, e
|
28
41
|
end
|
29
42
|
|
43
|
+
def current_database
|
44
|
+
ActiveRecord::Base.connection.schema_search_path
|
45
|
+
end
|
46
|
+
|
30
47
|
def reset
|
31
|
-
|
32
|
-
|
33
|
-
super # if !using_schemas?
|
48
|
+
ActiveRecord::Base.connection.schema_search_path = @defaults[:schema_search_path]
|
34
49
|
end
|
35
50
|
|
36
|
-
def current_database
|
37
|
-
return ActiveRecord::Base.connection.schema_search_path if using_schemas?
|
38
|
-
|
39
|
-
super # if !using_schemas?
|
40
|
-
end
|
41
|
-
|
42
|
-
protected
|
43
|
-
|
44
|
-
def create_schema(database)
|
45
|
-
reset
|
46
|
-
|
47
|
-
ActiveRecord::Base.connection.execute("CREATE SCHEMA #{sanitize(database)}")
|
48
|
-
rescue Exception => e
|
49
|
-
raise SchemaExists, e
|
50
|
-
end
|
51
|
-
|
52
|
-
def using_schemas?
|
53
|
-
Apartment.use_postgres_schemas
|
54
|
-
end
|
55
|
-
|
56
51
|
end
|
57
52
|
|
58
53
|
end
|
data/lib/apartment/version.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'apartment/adapters/mysql_adapter' # specific adapters get dynamically loaded based on adapter name, so we must manually require here
|
3
|
+
|
4
|
+
describe Apartment::Adapters::MysqlAdapter do
|
5
|
+
|
6
|
+
before do
|
7
|
+
ActiveRecord::Base.establish_connection Apartment::Test.config['connections']['mysql']
|
8
|
+
@mysql = Apartment::Database.mysql_adapter Apartment::Test.config['connections']['mysql'].symbolize_keys
|
9
|
+
end
|
10
|
+
|
11
|
+
after do
|
12
|
+
ActiveRecord::Base.clear_all_connections!
|
13
|
+
end
|
14
|
+
|
15
|
+
context "using databases" do
|
16
|
+
|
17
|
+
let(:database1){ 'first_database' }
|
18
|
+
|
19
|
+
before do
|
20
|
+
@mysql.create(database1)
|
21
|
+
end
|
22
|
+
|
23
|
+
after do
|
24
|
+
ActiveRecord::Base.connection.drop_database(@mysql.environmentify(database1))
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#create" do
|
28
|
+
it "should create the new database" do
|
29
|
+
ActiveRecord::Base.connection.execute("SELECT schema_name FROM information_schema.schemata").collect{|row| row[0]}.should include(@mysql.environmentify(database1))
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
end
|
@@ -5,7 +5,7 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
5
5
|
|
6
6
|
before do
|
7
7
|
ActiveRecord::Base.establish_connection Apartment::Test.config['connections']['postgresql']
|
8
|
-
@
|
8
|
+
@schema_search_path = ActiveRecord::Base.connection.schema_search_path
|
9
9
|
end
|
10
10
|
|
11
11
|
after do
|
@@ -13,72 +13,79 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
context "using schemas" do
|
16
|
-
|
17
|
-
let(:schema1){ 'first_db_schema' }
|
18
|
-
let(:schema_search_path){ ActiveRecord::Base.connection.schema_search_path }
|
19
16
|
|
17
|
+
let(:schema){ 'first_db_schema' }
|
18
|
+
|
19
|
+
subject{ Apartment::Database.postgresql_adapter Apartment::Test.config['connections']['postgresql'].symbolize_keys }
|
20
|
+
|
20
21
|
before do
|
21
|
-
|
22
|
+
Apartment.use_postgres_schemas = true
|
23
|
+
subject.create(schema)
|
22
24
|
end
|
23
25
|
|
24
26
|
after do
|
25
|
-
Apartment::Test.drop_schema(
|
27
|
+
Apartment::Test.drop_schema(schema)
|
26
28
|
end
|
27
|
-
|
29
|
+
|
28
30
|
describe "#create" do
|
31
|
+
|
29
32
|
it "should create the new schema" do
|
30
|
-
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect{|row| row['nspname']}.should include(
|
33
|
+
ActiveRecord::Base.connection.execute("SELECT nspname FROM pg_namespace;").collect{|row| row['nspname']}.should include(schema)
|
31
34
|
end
|
32
35
|
|
33
36
|
it "should load schema.rb to new schema" do
|
34
|
-
ActiveRecord::Base.connection.schema_search_path =
|
37
|
+
ActiveRecord::Base.connection.schema_search_path = schema
|
35
38
|
ActiveRecord::Base.connection.tables.should include('companies')
|
36
39
|
end
|
37
40
|
|
38
41
|
it "should reset connection when finished" do
|
39
|
-
ActiveRecord::Base.connection.schema_search_path.should_not ==
|
42
|
+
ActiveRecord::Base.connection.schema_search_path.should_not == schema
|
40
43
|
end
|
41
44
|
end
|
42
45
|
|
43
46
|
describe "#process" do
|
44
47
|
it "should connect" do
|
45
|
-
|
46
|
-
ActiveRecord::Base.connection.schema_search_path.should ==
|
48
|
+
subject.process(schema) do
|
49
|
+
ActiveRecord::Base.connection.schema_search_path.should == schema
|
47
50
|
end
|
48
51
|
end
|
49
52
|
|
50
53
|
it "should reset" do
|
51
|
-
|
52
|
-
ActiveRecord::Base.connection.schema_search_path.should == schema_search_path
|
54
|
+
subject.process(schema)
|
55
|
+
ActiveRecord::Base.connection.schema_search_path.should == @schema_search_path
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
56
59
|
describe "#reset" do
|
57
60
|
it "should reset connection" do
|
58
|
-
|
59
|
-
|
60
|
-
ActiveRecord::Base.connection.schema_search_path.should == schema_search_path
|
61
|
+
subject.switch(schema)
|
62
|
+
subject.reset
|
63
|
+
ActiveRecord::Base.connection.schema_search_path.should == @schema_search_path
|
61
64
|
end
|
62
65
|
end
|
63
66
|
|
64
67
|
describe "#switch" do
|
65
68
|
it "should connect to new schema" do
|
66
|
-
|
67
|
-
ActiveRecord::Base.connection.schema_search_path.should ==
|
69
|
+
subject.switch(schema)
|
70
|
+
ActiveRecord::Base.connection.schema_search_path.should == schema
|
68
71
|
end
|
69
72
|
|
70
73
|
it "should reset connection if database is nil" do
|
71
|
-
|
72
|
-
ActiveRecord::Base.connection.schema_search_path.should == schema_search_path
|
74
|
+
subject.switch
|
75
|
+
ActiveRecord::Base.connection.schema_search_path.should == @schema_search_path
|
73
76
|
end
|
74
77
|
end
|
75
78
|
|
76
79
|
describe "#current_database" do
|
77
80
|
it "should return the current schema name" do
|
78
|
-
|
79
|
-
|
81
|
+
subject.switch(schema)
|
82
|
+
subject.current_database.should == schema
|
80
83
|
end
|
81
84
|
end
|
82
85
|
|
83
86
|
end
|
87
|
+
|
88
|
+
context "using databases" do
|
89
|
+
# TODO
|
90
|
+
end
|
84
91
|
end
|
data/spec/apartment_spec.rb
CHANGED
data/spec/config/database.yml
CHANGED
@@ -3,8 +3,10 @@ connections:
|
|
3
3
|
adapter: postgresql
|
4
4
|
database: apartment_postgresql_test
|
5
5
|
username: root
|
6
|
-
password:
|
7
|
-
|
6
|
+
password:
|
7
|
+
|
8
8
|
mysql:
|
9
|
-
|
10
|
-
database: apartment_mysql_test
|
9
|
+
adapter: mysql2
|
10
|
+
database: apartment_mysql_test
|
11
|
+
username: root
|
12
|
+
password:
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,6 +11,7 @@ describe Apartment::Database do
|
|
11
11
|
let(:database2){ "yet_another_database" }
|
12
12
|
|
13
13
|
before do
|
14
|
+
Apartment.use_postgres_schemas = true
|
14
15
|
ActiveRecord::Base.establish_connection config
|
15
16
|
Apartment::Test.load_schema # load the Rails schema in the public db schema
|
16
17
|
Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
|
data/spec/spec_helper.rb
CHANGED
@@ -22,4 +22,10 @@ RSpec.configure do |config|
|
|
22
22
|
|
23
23
|
config.include RSpec::Integration::CapybaraSessions, :type => :request
|
24
24
|
|
25
|
+
config.before(:all) do
|
26
|
+
# Ensure that each test starts with a clean connect
|
27
|
+
# Necessary as some tests will leak things like current_schema into the next
|
28
|
+
ActiveRecord::Base.clear_all_connections!
|
29
|
+
end
|
30
|
+
|
25
31
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: apartment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.10.
|
5
|
+
version: 0.10.2
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan Brunner
|
@@ -19,15 +19,26 @@ dependencies:
|
|
19
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
|
-
- -
|
22
|
+
- - ">="
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version: 3.0.
|
24
|
+
version: 3.0.10
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
27
|
version_requirements: *id001
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
|
-
name:
|
29
|
+
name: rake
|
30
30
|
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 0.8.7
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: *id002
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: sqlite3
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
31
42
|
none: false
|
32
43
|
requirements:
|
33
44
|
- - ">="
|
@@ -35,10 +46,10 @@ dependencies:
|
|
35
46
|
version: "0"
|
36
47
|
type: :development
|
37
48
|
prerelease: false
|
38
|
-
version_requirements: *
|
49
|
+
version_requirements: *id003
|
39
50
|
- !ruby/object:Gem::Dependency
|
40
51
|
name: rspec
|
41
|
-
requirement: &
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
42
53
|
none: false
|
43
54
|
requirements:
|
44
55
|
- - ~>
|
@@ -46,10 +57,10 @@ dependencies:
|
|
46
57
|
version: 2.6.0
|
47
58
|
type: :development
|
48
59
|
prerelease: false
|
49
|
-
version_requirements: *
|
60
|
+
version_requirements: *id004
|
50
61
|
- !ruby/object:Gem::Dependency
|
51
62
|
name: rspec-rails
|
52
|
-
requirement: &
|
63
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
53
64
|
none: false
|
54
65
|
requirements:
|
55
66
|
- - ~>
|
@@ -57,10 +68,10 @@ dependencies:
|
|
57
68
|
version: 2.6.1
|
58
69
|
type: :development
|
59
70
|
prerelease: false
|
60
|
-
version_requirements: *
|
71
|
+
version_requirements: *id005
|
61
72
|
- !ruby/object:Gem::Dependency
|
62
73
|
name: capybara
|
63
|
-
requirement: &
|
74
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
64
75
|
none: false
|
65
76
|
requirements:
|
66
77
|
- - "="
|
@@ -68,10 +79,10 @@ dependencies:
|
|
68
79
|
version: 1.0.0
|
69
80
|
type: :development
|
70
81
|
prerelease: false
|
71
|
-
version_requirements: *
|
82
|
+
version_requirements: *id006
|
72
83
|
- !ruby/object:Gem::Dependency
|
73
84
|
name: pg
|
74
|
-
requirement: &
|
85
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
75
86
|
none: false
|
76
87
|
requirements:
|
77
88
|
- - ~>
|
@@ -79,21 +90,32 @@ dependencies:
|
|
79
90
|
version: 0.11.0
|
80
91
|
type: :development
|
81
92
|
prerelease: false
|
82
|
-
version_requirements: *
|
93
|
+
version_requirements: *id007
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: mysql2
|
96
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - "="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 0.2.7
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: *id008
|
83
105
|
- !ruby/object:Gem::Dependency
|
84
106
|
name: silent-postgres
|
85
|
-
requirement: &
|
107
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
86
108
|
none: false
|
87
109
|
requirements:
|
88
110
|
- - ~>
|
89
111
|
- !ruby/object:Gem::Version
|
90
|
-
version: 0.
|
112
|
+
version: 0.1.1
|
91
113
|
type: :development
|
92
114
|
prerelease: false
|
93
|
-
version_requirements: *
|
115
|
+
version_requirements: *id009
|
94
116
|
- !ruby/object:Gem::Dependency
|
95
117
|
name: delayed_job
|
96
|
-
requirement: &
|
118
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
97
119
|
none: false
|
98
120
|
requirements:
|
99
121
|
- - ~>
|
@@ -101,7 +123,7 @@ dependencies:
|
|
101
123
|
version: 2.1.4
|
102
124
|
type: :development
|
103
125
|
prerelease: false
|
104
|
-
version_requirements: *
|
126
|
+
version_requirements: *id010
|
105
127
|
description: Apartment allows Rails applications to deal with database multitenancy
|
106
128
|
email:
|
107
129
|
- ryan@ryanbrunner.com
|
@@ -135,6 +157,8 @@ files:
|
|
135
157
|
- lib/apartment/railtie.rb
|
136
158
|
- lib/apartment/version.rb
|
137
159
|
- lib/tasks/apartment.rake
|
160
|
+
- spec/adapters/mysql_adapter_spec.rb
|
161
|
+
- spec/adapters/postgresql_adapter_spec.rb
|
138
162
|
- spec/apartment_spec.rb
|
139
163
|
- spec/config/database.yml
|
140
164
|
- spec/dummy/Rakefile
|
@@ -173,12 +197,10 @@ files:
|
|
173
197
|
- spec/dummy/public/favicon.ico
|
174
198
|
- spec/dummy/public/stylesheets/.gitkeep
|
175
199
|
- spec/dummy/script/rails
|
176
|
-
- spec/integration/adapters/postgresql_integration_spec.rb
|
177
200
|
- spec/integration/apartment_rake_integration_spec.rb
|
178
201
|
- spec/integration/database_integration_spec.rb
|
179
202
|
- spec/integration/delayed_job_integration_spec.rb
|
180
203
|
- spec/integration/middleware/subdomain_elevator_spec.rb
|
181
|
-
- spec/integration/setup_spec.rb
|
182
204
|
- spec/spec_helper.rb
|
183
205
|
- spec/support/apartment_helpers.rb
|
184
206
|
- spec/support/capybara_sessions.rb
|
@@ -188,7 +210,7 @@ files:
|
|
188
210
|
- spec/unit/middleware/subdomain_elevator_spec.rb
|
189
211
|
- spec/unit/migrator_spec.rb
|
190
212
|
has_rdoc: true
|
191
|
-
homepage: http://github.com/
|
213
|
+
homepage: http://github.com/bradrobertson/apartment
|
192
214
|
licenses:
|
193
215
|
- MIT
|
194
216
|
post_install_message:
|
@@ -201,7 +223,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
201
223
|
requirements:
|
202
224
|
- - ">="
|
203
225
|
- !ruby/object:Gem::Version
|
204
|
-
hash:
|
226
|
+
hash: -795357038977467043
|
205
227
|
segments:
|
206
228
|
- 0
|
207
229
|
version: "0"
|
@@ -210,7 +232,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
210
232
|
requirements:
|
211
233
|
- - ">="
|
212
234
|
- !ruby/object:Gem::Version
|
213
|
-
hash:
|
235
|
+
hash: -795357038977467043
|
214
236
|
segments:
|
215
237
|
- 0
|
216
238
|
version: "0"
|