apartment 0.7.0 → 0.8.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 +5 -0
- data/lib/apartment/adapters/abstract_adapter.rb +8 -4
- data/lib/apartment/adapters/postgresql_adapter.rb +8 -7
- data/lib/apartment/database.rb +4 -11
- data/lib/apartment/migrator.rb +3 -3
- data/lib/apartment/version.rb +1 -1
- data/spec/integration/adapters/postgresql_integration_spec.rb +14 -3
- data/spec/integration/apartment_rake_integration_spec.rb +1 -1
- data/spec/integration/database_integration_spec.rb +8 -0
- data/spec/support/apartment_helpers.rb +1 -1
- metadata +3 -3
data/HISTORY.md
CHANGED
@@ -15,16 +15,15 @@ module Apartment
|
|
15
15
|
@defaults = defaults
|
16
16
|
end
|
17
17
|
|
18
|
-
# Connect to db or schema, do stuff, reset
|
18
|
+
# Connect to db or schema, do stuff, reset (equivalent to a switch... do stuff... reset)
|
19
19
|
#
|
20
20
|
# @param {String} database Database or schema to connect to
|
21
|
-
def
|
21
|
+
def process(database)
|
22
22
|
connect_to_new(database)
|
23
23
|
yield if block_given?
|
24
24
|
ensure
|
25
25
|
reset
|
26
26
|
end
|
27
|
-
alias_method :process, :connect_and_reset # more succinct name
|
28
27
|
|
29
28
|
# Create new postgres schema
|
30
29
|
#
|
@@ -32,7 +31,7 @@ module Apartment
|
|
32
31
|
def create(database)
|
33
32
|
# TODO create_database unless using_schemas?
|
34
33
|
|
35
|
-
|
34
|
+
process(database) do
|
36
35
|
import_database_schema
|
37
36
|
|
38
37
|
# Manually init schema migrations table (apparently there were issues with Postgres when this isn't done)
|
@@ -61,6 +60,11 @@ module Apartment
|
|
61
60
|
end
|
62
61
|
alias_method :seed, :seed_data
|
63
62
|
|
63
|
+
# Return the current database name
|
64
|
+
def current_database
|
65
|
+
ActiveRecord::Base.connection.current_database
|
66
|
+
end
|
67
|
+
|
64
68
|
protected
|
65
69
|
|
66
70
|
def create_schema
|
@@ -3,7 +3,7 @@ module Apartment
|
|
3
3
|
module Database
|
4
4
|
|
5
5
|
def self.postgresql_adapter(config)
|
6
|
-
Adapters::PostgresqlAdapter.new config, :schema_search_path => ActiveRecord::Base.connection.schema_search_path
|
6
|
+
Adapters::PostgresqlAdapter.new config, :schema_search_path => ActiveRecord::Base.connection.schema_search_path # this is the initial search path before any switches happen
|
7
7
|
end
|
8
8
|
end
|
9
9
|
|
@@ -14,7 +14,6 @@ module Apartment
|
|
14
14
|
# Set schema path or connect to new db
|
15
15
|
def connect_to_new(database)
|
16
16
|
return ActiveRecord::Base.connection.schema_search_path = database if using_schemas?
|
17
|
-
|
18
17
|
super
|
19
18
|
rescue ActiveRecord::StatementInvalid => e
|
20
19
|
raise SchemaNotFound, e
|
@@ -28,12 +27,14 @@ module Apartment
|
|
28
27
|
end
|
29
28
|
|
30
29
|
def reset
|
31
|
-
|
32
|
-
|
33
|
-
else
|
34
|
-
super
|
35
|
-
end
|
30
|
+
return ActiveRecord::Base.connection.schema_search_path = @defaults[:schema_search_path] if using_schemas?
|
31
|
+
super
|
36
32
|
end
|
33
|
+
|
34
|
+
def current_database
|
35
|
+
return ActiveRecord::Base.connection.schema_search_path if using_schemas?
|
36
|
+
super
|
37
|
+
end
|
37
38
|
|
38
39
|
protected
|
39
40
|
|
data/lib/apartment/database.rb
CHANGED
@@ -1,26 +1,19 @@
|
|
1
|
-
require 'active_support/core_ext/
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
2
|
|
3
3
|
module Apartment
|
4
4
|
module Database
|
5
5
|
|
6
|
-
MULTI_TENANT_METHODS = [:create, :switch, :reset, :connect_and_reset, :process, :seed]
|
7
|
-
|
8
6
|
class << self
|
9
7
|
|
8
|
+
# pass these methods to our adapter
|
9
|
+
delegate :create, :switch, :reset, :process, :seed, :current_database, :to => :adapter
|
10
|
+
|
10
11
|
# Call init to establish a connection to the public schema on all excluded models
|
11
12
|
# This must be done before creating any new schemas or switching
|
12
13
|
def init
|
13
14
|
connect_exclusions
|
14
15
|
end
|
15
16
|
|
16
|
-
MULTI_TENANT_METHODS.each do |method|
|
17
|
-
class_eval <<-RUBY
|
18
|
-
def #{method}(*args, &block)
|
19
|
-
adapter.send(:#{method}, *args, &block)
|
20
|
-
end
|
21
|
-
RUBY
|
22
|
-
end
|
23
|
-
|
24
17
|
def adapter
|
25
18
|
@adapter ||= begin
|
26
19
|
adapter_method = "#{config[:adapter]}_adapter"
|
data/lib/apartment/migrator.rb
CHANGED
@@ -6,17 +6,17 @@ module Apartment
|
|
6
6
|
|
7
7
|
# Migrate to latest
|
8
8
|
def migrate(database)
|
9
|
-
Database.
|
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
|
-
Database.
|
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
|
-
Database.
|
19
|
+
Database.process(database){ ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_path, step) }
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
data/lib/apartment/version.rb
CHANGED
@@ -8,6 +8,10 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
8
8
|
@pg = Apartment::Database.postgresql_adapter Apartment::Test.config['connections']['postgresql'].symbolize_keys
|
9
9
|
end
|
10
10
|
|
11
|
+
after do
|
12
|
+
ActiveRecord::Base.clear_all_connections!
|
13
|
+
end
|
14
|
+
|
11
15
|
context "using schemas" do
|
12
16
|
|
13
17
|
let(:schema1){ 'first_db_schema' }
|
@@ -36,15 +40,15 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
36
40
|
end
|
37
41
|
end
|
38
42
|
|
39
|
-
describe "#
|
43
|
+
describe "#process" do
|
40
44
|
it "should connect" do
|
41
|
-
@pg.
|
45
|
+
@pg.process(schema1) do
|
42
46
|
ActiveRecord::Base.connection.schema_search_path.should == schema1
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
46
50
|
it "should reset" do
|
47
|
-
@pg.
|
51
|
+
@pg.process(schema1)
|
48
52
|
ActiveRecord::Base.connection.schema_search_path.should == schema_search_path
|
49
53
|
end
|
50
54
|
end
|
@@ -69,5 +73,12 @@ describe Apartment::Adapters::PostgresqlAdapter do
|
|
69
73
|
end
|
70
74
|
end
|
71
75
|
|
76
|
+
describe "#current_database" do
|
77
|
+
it "should return the current schema name" do
|
78
|
+
@pg.switch(schema1)
|
79
|
+
@pg.current_database.should == schema1
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
72
83
|
end
|
73
84
|
end
|
@@ -113,6 +113,14 @@ describe Apartment::Database do
|
|
113
113
|
end
|
114
114
|
end
|
115
115
|
|
116
|
+
describe "#current_database" do
|
117
|
+
|
118
|
+
it "should return the current schema search path" do
|
119
|
+
Apartment::Database.switch database
|
120
|
+
Apartment::Database.current_database.should == database
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
116
124
|
end
|
117
125
|
|
118
126
|
end
|
@@ -7,7 +7,7 @@ module Apartment
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def self.drop_schema(schema)
|
10
|
-
ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE")
|
10
|
+
ActiveRecord::Base.silence{ ActiveRecord::Base.connection.execute("DROP SCHEMA IF EXISTS #{schema} CASCADE") }
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.create_schema(schema)
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: apartment
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.8.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ryan Brunner
|
@@ -185,7 +185,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
185
185
|
requirements:
|
186
186
|
- - ">="
|
187
187
|
- !ruby/object:Gem::Version
|
188
|
-
hash:
|
188
|
+
hash: -2374879157766889848
|
189
189
|
segments:
|
190
190
|
- 0
|
191
191
|
version: "0"
|
@@ -194,7 +194,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
194
194
|
requirements:
|
195
195
|
- - ">="
|
196
196
|
- !ruby/object:Gem::Version
|
197
|
-
hash:
|
197
|
+
hash: -2374879157766889848
|
198
198
|
segments:
|
199
199
|
- 0
|
200
200
|
version: "0"
|