storey 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +3 -0
- data/lib/storey.rb +30 -6
- data/lib/storey/version.rb +1 -1
- data/spec/dummy/db/schema.rb +1 -0
- data/spec/spec_helper.rb +13 -1
- data/spec/storey/persistent_schemas_spec.rb +4 -0
- data/spec/storey/switch_spec.rb +58 -0
- data/storey.gemspec +1 -1
- metadata +59 -97
data/README.md
CHANGED
@@ -17,6 +17,9 @@ Typically set in an initializer: config/initializer/storey.rb
|
|
17
17
|
# Used for obscuring the schema name - which is important when performing schema duplication.
|
18
18
|
# Storey.suffix = "_suffix"
|
19
19
|
|
20
|
+
# Defines schemas that should always stay in the search path, apart from the one you switched to.
|
21
|
+
# Storey.persistent_schemas = %w(hstore)
|
22
|
+
|
20
23
|
# Methods
|
21
24
|
|
22
25
|
## schemas
|
data/lib/storey.rb
CHANGED
@@ -10,12 +10,13 @@ module Storey
|
|
10
10
|
|
11
11
|
autoload :Duplicator, 'storey/duplicator'
|
12
12
|
|
13
|
-
mattr_accessor :suffix, :default_search_path
|
13
|
+
mattr_accessor :suffix, :default_search_path, :persistent_schemas
|
14
14
|
mattr_reader :excluded_models
|
15
15
|
|
16
16
|
def init
|
17
17
|
@@default_search_path = schema
|
18
18
|
self.excluded_models ||= []
|
19
|
+
self.persistent_schemas ||= []
|
19
20
|
process_excluded_models
|
20
21
|
end
|
21
22
|
|
@@ -89,19 +90,28 @@ module Storey
|
|
89
90
|
result
|
90
91
|
else
|
91
92
|
reset and return if name.blank?
|
92
|
-
|
93
|
-
ActiveRecord::Base.connection.schema_search_path =
|
93
|
+
path = self.schema_search_path_for(name)
|
94
|
+
ActiveRecord::Base.connection.schema_search_path = path
|
94
95
|
end
|
95
96
|
rescue ActiveRecord::StatementInvalid => e
|
96
97
|
if e.to_s =~ /invalid value for parameter "search_path"/
|
97
|
-
fail Storey::SchemaNotFound, %{The schema "#{
|
98
|
+
fail Storey::SchemaNotFound, %{The schema "#{path}" cannot be found.}
|
98
99
|
else
|
99
100
|
raise e
|
100
101
|
end
|
101
102
|
end
|
102
103
|
|
104
|
+
def schema_search_path_for(schema_name)
|
105
|
+
path = [suffixify(schema_name)]
|
106
|
+
self.persistent_schemas.each do |schema|
|
107
|
+
path << suffixify(schema)
|
108
|
+
end
|
109
|
+
path.uniq.join(',')
|
110
|
+
end
|
111
|
+
|
103
112
|
def reload_config!
|
104
113
|
self.excluded_models = []
|
114
|
+
self.persistent_schemas = []
|
105
115
|
self.suffix = nil
|
106
116
|
end
|
107
117
|
|
@@ -123,7 +133,20 @@ module Storey
|
|
123
133
|
end
|
124
134
|
|
125
135
|
def unsuffixify(name)
|
126
|
-
|
136
|
+
search_path = name
|
137
|
+
if Storey.suffix
|
138
|
+
paths = []
|
139
|
+
name.split(',').each do |schema|
|
140
|
+
result = if schema =~ /(\w+)#{Storey.suffix}/
|
141
|
+
$1
|
142
|
+
else
|
143
|
+
schema
|
144
|
+
end
|
145
|
+
paths << result
|
146
|
+
end
|
147
|
+
search_path = paths.join(',')
|
148
|
+
end
|
149
|
+
search_path
|
127
150
|
end
|
128
151
|
|
129
152
|
protected
|
@@ -133,7 +156,8 @@ module Storey
|
|
133
156
|
end
|
134
157
|
|
135
158
|
def reset
|
136
|
-
|
159
|
+
path = self.schema_search_path_for(self.default_search_path)
|
160
|
+
ActiveRecord::Base.connection.schema_search_path = path
|
137
161
|
end
|
138
162
|
|
139
163
|
# Loads the Rails schema.rb into the current schema
|
data/lib/storey/version.rb
CHANGED
data/spec/dummy/db/schema.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -9,9 +9,21 @@ require 'database_cleaner'
|
|
9
9
|
# Include rake so we can instantiate the @rake variable and call rake tasks
|
10
10
|
require 'rake'
|
11
11
|
|
12
|
-
require '
|
12
|
+
require 'pry'
|
13
13
|
|
14
14
|
RSpec.configure do |config|
|
15
|
+
config.before(:suite) do
|
16
|
+
# Clean the public schema
|
17
|
+
Storey.switch do
|
18
|
+
tables = ActiveRecord::Base.connection.tables
|
19
|
+
# Don't invoke DatabaseCleaner if there are no tables,
|
20
|
+
# since that library chokes and tries to drop tables without names
|
21
|
+
if tables.size != 1 or tables[0] != 'schema_migrations'
|
22
|
+
DatabaseCleaner.clean_with :truncation
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
15
27
|
config.before(:each) do
|
16
28
|
# We don't want configuration to leak into other tests
|
17
29
|
Storey.reload_config!
|
data/spec/storey/switch_spec.rb
CHANGED
@@ -99,4 +99,62 @@ describe Storey, "#switch" do
|
|
99
99
|
end
|
100
100
|
end
|
101
101
|
end
|
102
|
+
|
103
|
+
context 'when persitent schemas are set' do
|
104
|
+
|
105
|
+
context 'when suffixes are not set' do
|
106
|
+
before do
|
107
|
+
Storey.create 'foobar'
|
108
|
+
persistent_schemas = %w(handle bar foo)
|
109
|
+
persistent_schemas.each do |schema|
|
110
|
+
Storey.create schema
|
111
|
+
end
|
112
|
+
Storey.persistent_schemas = persistent_schemas
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should switch to the schema with the persitent schemas still in the search path' do
|
116
|
+
Storey.switch 'foobar'
|
117
|
+
Storey.schema.should == %{foobar,handle,bar,foo}
|
118
|
+
|
119
|
+
Storey.switch
|
120
|
+
Storey.schema.should == %{"$user",public,handle,bar,foo}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context 'when suffixes are set' do
|
125
|
+
before do
|
126
|
+
Storey.suffix = '_boomboom'
|
127
|
+
Storey.create 'foobar'
|
128
|
+
persistent_schemas = %w(handle bar foo)
|
129
|
+
persistent_schemas.each do |schema|
|
130
|
+
Storey.create schema
|
131
|
+
end
|
132
|
+
Storey.persistent_schemas = persistent_schemas
|
133
|
+
end
|
134
|
+
|
135
|
+
it 'should switch to the schema with the persitent schemas still in the search path' do
|
136
|
+
Storey.switch 'foobar'
|
137
|
+
Storey.schema.should == %{foobar,handle,bar,foo}
|
138
|
+
|
139
|
+
Storey.switch
|
140
|
+
Storey.schema.should == %{"$user",public,handle,bar,foo}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'when switching to one of the persistent schemas' do
|
145
|
+
before do
|
146
|
+
persistent_schemas = %w(handle bar foo)
|
147
|
+
persistent_schemas.each do |schema|
|
148
|
+
Storey.create schema
|
149
|
+
end
|
150
|
+
Storey.persistent_schemas = persistent_schemas
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'should not have duplicate schemas in the search path' do
|
154
|
+
Storey.switch 'bar'
|
155
|
+
Storey.schema.should == %{bar,handle,foo}
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
end
|
102
160
|
end
|
data/storey.gemspec
CHANGED
@@ -22,6 +22,6 @@ Gem::Specification.new do |s|
|
|
22
22
|
s.add_development_dependency "rspec-rails"
|
23
23
|
s.add_development_dependency "pg", "~> 0.12.2"
|
24
24
|
s.add_development_dependency "database_cleaner"
|
25
|
-
s.add_development_dependency "
|
25
|
+
s.add_development_dependency "pry"
|
26
26
|
s.add_runtime_dependency "rails", "~> 3.2.2"
|
27
27
|
end
|
metadata
CHANGED
@@ -1,107 +1,79 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: storey
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 0
|
10
|
-
version: 0.1.0
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Ramon Tayag
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-06-17 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: rspec-rails
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &82245390 !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 0
|
32
|
-
version: "0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
33
22
|
type: :development
|
34
|
-
version_requirements: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: pg
|
37
23
|
prerelease: false
|
38
|
-
|
24
|
+
version_requirements: *82245390
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: pg
|
27
|
+
requirement: &82244830 !ruby/object:Gem::Requirement
|
39
28
|
none: false
|
40
|
-
requirements:
|
29
|
+
requirements:
|
41
30
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
hash: 43
|
44
|
-
segments:
|
45
|
-
- 0
|
46
|
-
- 12
|
47
|
-
- 2
|
31
|
+
- !ruby/object:Gem::Version
|
48
32
|
version: 0.12.2
|
49
33
|
type: :development
|
50
|
-
version_requirements: *id002
|
51
|
-
- !ruby/object:Gem::Dependency
|
52
|
-
name: database_cleaner
|
53
34
|
prerelease: false
|
54
|
-
|
35
|
+
version_requirements: *82244830
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: database_cleaner
|
38
|
+
requirement: &82244090 !ruby/object:Gem::Requirement
|
55
39
|
none: false
|
56
|
-
requirements:
|
57
|
-
- -
|
58
|
-
- !ruby/object:Gem::Version
|
59
|
-
|
60
|
-
segments:
|
61
|
-
- 0
|
62
|
-
version: "0"
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
63
44
|
type: :development
|
64
|
-
version_requirements: *id003
|
65
|
-
- !ruby/object:Gem::Dependency
|
66
|
-
name: ruby-debug
|
67
45
|
prerelease: false
|
68
|
-
|
46
|
+
version_requirements: *82244090
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: pry
|
49
|
+
requirement: &82243450 !ruby/object:Gem::Requirement
|
69
50
|
none: false
|
70
|
-
requirements:
|
71
|
-
- -
|
72
|
-
- !ruby/object:Gem::Version
|
73
|
-
|
74
|
-
segments:
|
75
|
-
- 0
|
76
|
-
version: "0"
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
77
55
|
type: :development
|
78
|
-
version_requirements: *id004
|
79
|
-
- !ruby/object:Gem::Dependency
|
80
|
-
name: rails
|
81
56
|
prerelease: false
|
82
|
-
|
57
|
+
version_requirements: *82243450
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rails
|
60
|
+
requirement: &82242810 !ruby/object:Gem::Requirement
|
83
61
|
none: false
|
84
|
-
requirements:
|
62
|
+
requirements:
|
85
63
|
- - ~>
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
hash: 11
|
88
|
-
segments:
|
89
|
-
- 3
|
90
|
-
- 2
|
91
|
-
- 2
|
64
|
+
- !ruby/object:Gem::Version
|
92
65
|
version: 3.2.2
|
93
66
|
type: :runtime
|
94
|
-
|
95
|
-
|
96
|
-
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *82242810
|
69
|
+
description: Storey aims to simplify the implementation of managing a multi-tenant
|
70
|
+
application.
|
71
|
+
email:
|
97
72
|
- ramon@tayag.net
|
98
73
|
executables: []
|
99
|
-
|
100
74
|
extensions: []
|
101
|
-
|
102
75
|
extra_rdoc_files: []
|
103
|
-
|
104
|
-
files:
|
76
|
+
files:
|
105
77
|
- .gitignore
|
106
78
|
- .rspec
|
107
79
|
- .rvmrc
|
@@ -162,44 +134,34 @@ files:
|
|
162
134
|
- spec/storey/drop_spec.rb
|
163
135
|
- spec/storey/duplicate_spec.rb
|
164
136
|
- spec/storey/excluded_models_spec.rb
|
137
|
+
- spec/storey/persistent_schemas_spec.rb
|
165
138
|
- spec/storey/schema_spec.rb
|
166
139
|
- spec/storey/schemas_spec.rb
|
167
140
|
- spec/storey/switch_spec.rb
|
168
141
|
- spec/tasks/storey_rake_spec.rb
|
169
142
|
- storey.gemspec
|
170
|
-
has_rdoc: true
|
171
143
|
homepage: https://github.com/ramontayag/storey
|
172
144
|
licenses: []
|
173
|
-
|
174
145
|
post_install_message:
|
175
146
|
rdoc_options: []
|
176
|
-
|
177
|
-
require_paths:
|
147
|
+
require_paths:
|
178
148
|
- lib
|
179
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
180
150
|
none: false
|
181
|
-
requirements:
|
182
|
-
- -
|
183
|
-
- !ruby/object:Gem::Version
|
184
|
-
|
185
|
-
|
186
|
-
- 0
|
187
|
-
version: "0"
|
188
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
151
|
+
requirements:
|
152
|
+
- - ! '>='
|
153
|
+
- !ruby/object:Gem::Version
|
154
|
+
version: '0'
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
189
156
|
none: false
|
190
|
-
requirements:
|
191
|
-
- -
|
192
|
-
- !ruby/object:Gem::Version
|
193
|
-
|
194
|
-
segments:
|
195
|
-
- 0
|
196
|
-
version: "0"
|
157
|
+
requirements:
|
158
|
+
- - ! '>='
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
197
161
|
requirements: []
|
198
|
-
|
199
162
|
rubyforge_project: storey
|
200
|
-
rubygems_version: 1.
|
163
|
+
rubygems_version: 1.8.11
|
201
164
|
signing_key:
|
202
165
|
specification_version: 3
|
203
166
|
summary: Manage multiple PostgreSQL schemas in your multi-tenant app.
|
204
167
|
test_files: []
|
205
|
-
|