storey 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,6 +2,8 @@ class Storey::Duplicator
2
2
  attr_accessor :source_schema, :target_schema, :source_file, :target_file, :structure_only, :file_prefix, :dump_path, :source_dump_path, :target_dump_path
3
3
 
4
4
  def initialize(from_schema, to_schema, options={})
5
+ fail Storey::SchemaNotFound, "cannot duplicate from nil schema" unless from_schema
6
+
5
7
  self.dump_path = File.join Rails.root, 'tmp', 'schema_dumps'
6
8
  self.source_dump_path = File.join self.dump_path, 'source'
7
9
  self.target_dump_path = File.join self.dump_path, 'target'
@@ -1,3 +1,3 @@
1
1
  module Storey
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
data/lib/storey.rb CHANGED
@@ -9,19 +9,24 @@ require 'storey/hstore'
9
9
  require 'storey/dumper'
10
10
 
11
11
  module Storey
12
- extend self
13
12
  RESERVED_SCHEMAS = %w(hstore)
14
13
 
15
- mattr_accessor :suffix, :default_search_path, :persistent_schemas
14
+ mattr_accessor :suffix, :persistent_schemas
15
+ mattr_writer :default_search_path
16
16
  mattr_reader :excluded_models
17
+ extend self
17
18
 
18
19
  def init
19
- @@default_search_path = schema
20
+ self.default_search_path = self.schema
20
21
  self.excluded_models ||= []
21
22
  self.persistent_schemas ||= []
22
23
  process_excluded_models
23
24
  end
24
25
 
26
+ def default_search_path
27
+ ([@@default_search_path] + self.persistent_schemas).join(',')
28
+ end
29
+
25
30
  def excluded_models=(array)
26
31
  @@excluded_models = array
27
32
  process_excluded_models
@@ -60,8 +65,7 @@ module Storey
60
65
 
61
66
  def create_plain_schema(schema_name)
62
67
  switches = self.command_line_switches(:command => %{"CREATE SCHEMA #{self.suffixify schema_name}"})
63
- command = "psql #{switches}"
64
- system(command)
68
+ `psql #{switches}`
65
69
  end
66
70
 
67
71
  def command_line_switches(options={})
@@ -108,23 +112,42 @@ module Storey
108
112
  else
109
113
  reset and return if name.blank?
110
114
  path = self.schema_search_path_for(name)
115
+
116
+ unless self.schema_exists?(name)
117
+ fail Storey::SchemaNotFound, %{The schema "#{path}" cannot be found.}
118
+ end
119
+
111
120
  ActiveRecord::Base.connection.schema_search_path = path
112
121
  end
113
122
  rescue ActiveRecord::StatementInvalid => e
114
- if e.to_s =~ /invalid value for parameter "search_path"/
115
- fail Storey::SchemaNotFound, %{The schema "#{path}" cannot be found.}
116
- elsif e.to_s =~ /relation ".*" does not exist at character \d+/
117
- warn "Still unsure why the following error occurs, but see https://github.com/ramontayag/storey/issues/11"
123
+ if e.to_s =~ /relation ".*" does not exist at character \d+/
124
+ warn "See https://github.com/ramontayag/storey/issues/11"
118
125
  raise e
119
126
  else
120
127
  raise e
121
128
  end
122
129
  end
123
130
 
131
+ def schema_exists?(name)
132
+ schema_name = self.suffixify(name)
133
+
134
+ schemas_in_db = self.schemas(suffix: self.suffix.present?)
135
+ schemas_in_db << %("$user")
136
+ schema_names = schema_name.split(',')
137
+ schemas_not_in_db = schema_names - schemas_in_db
138
+ schemas_not_in_db.empty?
139
+ # existence = schema_names.map do |s|
140
+ # schemas_in_db.include?(s)
141
+ # end
142
+ # return false if existence.include?(false)
143
+ # true
144
+ end
145
+
124
146
  def schema_search_path_for(schema_name)
147
+ schema_names = schema_name.split(',')
125
148
  path = [suffixify(schema_name)]
126
149
  self.persistent_schemas.each do |schema|
127
- path << suffixify(schema)
150
+ path << suffixify(schema) unless schema_names.include?(schema)
128
151
  end
129
152
  path.uniq.join(',')
130
153
  end
@@ -145,7 +168,6 @@ module Storey
145
168
  end
146
169
 
147
170
  def suffixify(schema_name)
148
-
149
171
  if Storey.suffix &&
150
172
  !schema_name.include?(Storey.suffix) &&
151
173
  !matches_default_search_path?(schema_name)
@@ -178,8 +200,7 @@ module Storey
178
200
  paths.each do |path|
179
201
  return true if path == schema_name
180
202
  end
181
- return true if self.default_search_path == schema_name
182
- return false
203
+ self.default_search_path == schema_name
183
204
  end
184
205
 
185
206
  protected
@@ -11,6 +11,16 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 0) do
14
+ ActiveRecord::Schema.define(:version => 20120115060728) do
15
+
16
+ create_table "companies", :force => true do |t|
17
+ t.string "name"
18
+ t.string "schema_name"
19
+ end
20
+
21
+ create_table "posts", :force => true do |t|
22
+ t.string "name"
23
+ t.text "body"
24
+ end
15
25
 
16
26
  end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Storey', '#default_search_path' do
4
+
5
+ it 'should include the persistent schemas' do
6
+ Storey.persistent_schemas = %w(hello there)
7
+ Storey.default_search_path.should == '"$user",public,hello,there'
8
+ end
9
+
10
+ end
@@ -7,6 +7,13 @@ describe Storey, "#duplicate!" do
7
7
  FileUtils.rm_rf(tmp_dir)
8
8
  end
9
9
 
10
+ context 'when the target schema is nil' do
11
+ it 'should raise an error' do
12
+ expect { Storey.duplicate! nil, 'new' }.
13
+ to raise_error(Storey::SchemaNotFound, "cannot duplicate from nil schema")
14
+ end
15
+ end
16
+
10
17
  context "when there's no suffix set" do
11
18
  before do
12
19
  Storey.create 'ricky' do
@@ -5,17 +5,16 @@ describe Storey::Hstore do
5
5
  it 'should install the extension into the hstore schema' do
6
6
  Storey.persistent_schemas = %w(hstore)
7
7
  described_class.install
8
- expect {
9
- ActiveRecord::Base.connection.execute "DROP EXTENSION hstore"
10
- }.to_not raise_error(ActiveRecord::StatementInvalid)
8
+ expect { ActiveRecord::Base.connection.execute "DROP EXTENSION hstore" }.
9
+ to_not raise_error(ActiveRecord::StatementInvalid)
11
10
  end
12
11
 
13
12
  context 'when hstore is not one of the persistent schemas' do
14
13
  it 'should fail with an StoreyError' do
15
14
  Storey.persistent_schemas = []
16
- expect {
17
- described_class.install
18
- }.to raise_error(Storey::StoreyError, 'You are attempting to install hstore data type, but the hstore schema (where the data type will be installed) is not one of the persistent schemas. Please add hstore to the list of persistent schemas.')
15
+ message = 'You are attempting to install hstore data type, but the hstore schema (where the data type will be installed) is not one of the persistent schemas. Please add hstore to the list of persistent schemas.'
16
+ expect { described_class.install }.
17
+ to raise_error(Storey::StoreyError, message)
19
18
  end
20
19
  end
21
20
  end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Storey, '#schema_exists?' do
4
+ context 'the schema is the default search path' do
5
+ it 'should return true' do
6
+ Storey.schema_exists?('"$user",public').should be_true
7
+ end
8
+ end
9
+
10
+ context 'when there is no suffix' do
11
+ context 'when the schema exists' do
12
+ before do
13
+ Storey.create 'hoo'
14
+ end
15
+
16
+ it 'should return true' do
17
+ Storey.schema_exists?('hoo').should be_true
18
+ end
19
+ end
20
+
21
+ context 'when the schema does not exist' do
22
+ it 'should return false' do
23
+ Storey.schema_exists?('boo').should be_false
24
+ end
25
+ end
26
+ end
27
+
28
+ context 'when there is a suffix' do
29
+ context 'when the schema exists' do
30
+ before do
31
+ Storey.suffix = '_boo'
32
+ Storey.create 'croo'
33
+ end
34
+
35
+ it 'should return true' do
36
+ Storey.schema_exists?('croo').should be_true
37
+ end
38
+ end
39
+
40
+ context 'when the schema does not exist' do
41
+ it 'should return false' do
42
+ Storey.schema_exists?('croo').should be_false
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,10 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Storey, '.schema_search_path_for' do
4
- context 'given a blank argument' do
5
- it 'should return the default path' do
6
- Storey.switch
7
- Storey.schema.should == Storey.default_search_path
8
- end
3
+ describe Storey, '#schema_search_path_for' do
4
+
5
+ context 'given a search path that is one of the persistent schemas' do
6
+ # Storey.create 'bola'
7
+ # Storey.create 'halla'
8
+ Storey.persistent_schemas = %w(halla)
9
+ Storey.schema_search_path_for('bola,halla').should == 'bola,halla'
10
+ Storey.schema_search_path_for('halla').should == 'halla'
11
+ Storey.schema_search_path_for('halla,bola').should == 'halla,bola'
9
12
  end
13
+
10
14
  end
@@ -60,13 +60,15 @@ describe Storey, "#switch" do
60
60
  end
61
61
 
62
62
  it "should raise an error naming the schema with suffix" do
63
- expect {Storey.switch "babo"}.to raise_error(Storey::SchemaNotFound, %{The schema "babo_rock" cannot be found.})
63
+ expect {Storey.switch "babo"}.
64
+ to raise_error(Storey::SchemaNotFound, %{The schema "babo_rock" cannot be found.})
64
65
  end
65
66
  end
66
67
 
67
68
  context "when the suffix is not set" do
68
69
  it "should raise an error" do
69
- expect {Storey.switch "babo"}.to raise_error(Storey::SchemaNotFound, %{The schema "babo" cannot be found.})
70
+ expect {Storey.switch "babo"}.
71
+ to raise_error(Storey::SchemaNotFound, %{The schema "babo" cannot be found.})
70
72
  end
71
73
  end
72
74
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: storey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-13 00:00:00.000000000 Z
12
+ date: 2012-10-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec-rails
@@ -160,12 +160,14 @@ files:
160
160
  - spec/storey/configuration_spec.rb
161
161
  - spec/storey/create_plain_schema_spec.rb
162
162
  - spec/storey/create_spec.rb
163
+ - spec/storey/default_search_path_spec.rb
163
164
  - spec/storey/drop_spec.rb
164
165
  - spec/storey/dumper_spec.rb
165
166
  - spec/storey/duplicate_spec.rb
166
167
  - spec/storey/excluded_models_spec.rb
167
168
  - spec/storey/hstore_spec.rb
168
169
  - spec/storey/persistent_schemas_spec.rb
170
+ - spec/storey/schema_exists_spec.rb
169
171
  - spec/storey/schema_search_path_for_spec.rb
170
172
  - spec/storey/schema_spec.rb
171
173
  - spec/storey/schemas_spec.rb
@@ -187,7 +189,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
187
189
  version: '0'
188
190
  segments:
189
191
  - 0
190
- hash: 1992492096624626052
192
+ hash: -1937923977582358929
191
193
  required_rubygems_version: !ruby/object:Gem::Requirement
192
194
  none: false
193
195
  requirements:
@@ -196,7 +198,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
198
  version: '0'
197
199
  segments:
198
200
  - 0
199
- hash: 1992492096624626052
201
+ hash: -1937923977582358929
200
202
  requirements: []
201
203
  rubyforge_project: storey
202
204
  rubygems_version: 1.8.24
@@ -250,12 +252,14 @@ test_files:
250
252
  - spec/storey/configuration_spec.rb
251
253
  - spec/storey/create_plain_schema_spec.rb
252
254
  - spec/storey/create_spec.rb
255
+ - spec/storey/default_search_path_spec.rb
253
256
  - spec/storey/drop_spec.rb
254
257
  - spec/storey/dumper_spec.rb
255
258
  - spec/storey/duplicate_spec.rb
256
259
  - spec/storey/excluded_models_spec.rb
257
260
  - spec/storey/hstore_spec.rb
258
261
  - spec/storey/persistent_schemas_spec.rb
262
+ - spec/storey/schema_exists_spec.rb
259
263
  - spec/storey/schema_search_path_for_spec.rb
260
264
  - spec/storey/schema_spec.rb
261
265
  - spec/storey/schemas_spec.rb