schema_plus 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f36351daeeb9863e7b0344d565b2f67125ed9cea
4
- data.tar.gz: cb88da4066f4db2f2dfb35f018cc38d82cf6aebc
3
+ metadata.gz: 65c72cd47992bb6bf783d19b59e5d518e9256acc
4
+ data.tar.gz: fc0f8382dfb38c00719a1e86286390de3b7726a7
5
5
  SHA512:
6
- metadata.gz: f7f2ab813dcf8bd3c02c780337fefc2ae2c5cd793dee59b1cecc76d1706749918bf534685d3d53b7c31bce4989b56e906d292ff1c254f26e9f80d56ac450aca3
7
- data.tar.gz: 79c000eaa5312482df33ec2d09638d93038ba213320172eb6a029a077efbb41164996cc344a8f3325b06d4cb7702a9aaa3153eeba8f65acf9a9e33e2bc80a651
6
+ metadata.gz: cdedc86c2659b80c404e189f178848426e60aea035fec8e3dc144fe48212dff4ce8139a24b34103f01c57a72a8099f5c9901d105cab5ffcb800d38983b1d1914
7
+ data.tar.gz: 251070435963c3a912f0f097020f707adae3aae4861316f33a245448ba32fd1fef90e908816ee239c70f9e92309f891ada6c12fc9111bb4bf83484db7ca5d760
data/README.md CHANGED
@@ -300,6 +300,10 @@ of foreign key constraints, you can re-enable it:
300
300
 
301
301
  * (nothing new yet)
302
302
 
303
+ ### 1.3.1
304
+
305
+ * Regression bug fix, non-option arguemnts to remove_index
306
+
303
307
  ### 1.3.0
304
308
 
305
309
  * Added :if_exists option for remove_index
@@ -131,10 +131,12 @@ module SchemaPlus
131
131
 
132
132
  # Extends rails' remove_index to include this options:
133
133
  # :if_exists
134
- def remove_index_with_schema_plus(table_name, options={})
135
- return if options.delete(:if_exists) and not index_name_exists?(table_name, options[:name] || index_name(table_name, options), false)
134
+ def remove_index_with_schema_plus(table_name, *args)
135
+ options = args.extract_options!
136
+ return if options.delete(:if_exists) and not index_name_exists?(table_name, options[:name] || index_name(table_name, *args), false)
136
137
  options.delete(:column) if options[:name] and ::ActiveRecord::VERSION::MAJOR < 4
137
- remove_index_without_schema_plus(table_name, options)
138
+ args << options if options.any?
139
+ remove_index_without_schema_plus(table_name, *args)
138
140
  end
139
141
 
140
142
  # called from individual adpaters, after renaming table from old
@@ -1,3 +1,3 @@
1
1
  module SchemaPlus
2
- VERSION = "1.3.0"
2
+ VERSION = "1.3.1"
3
3
  end
data/spec/index_spec.rb CHANGED
@@ -1,126 +1,201 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "add_index" do
3
+ describe "index" do
4
4
 
5
- before(:all) do
6
- define_schema(:auto_create => false) do
7
- create_table :users, :force => true do |t|
8
- t.string :login
9
- t.datetime :deleted_at
5
+ let(:migration) { ::ActiveRecord::Migration }
6
+ let(:connection) { ::ActiveRecord::Base.connection }
7
+
8
+ describe "add_index" do
9
+
10
+ before(:each) do
11
+ connection.tables.each do |table| connection.drop_table table, cascade: true end
12
+
13
+ define_schema(:auto_create => false) do
14
+ create_table :users, :force => true do |t|
15
+ t.string :login
16
+ t.datetime :deleted_at
17
+ end
18
+
19
+ create_table :posts, :force => true do |t|
20
+ t.text :body
21
+ t.integer :user_id
22
+ t.integer :author_id
23
+ end
24
+
25
+ create_table :comments, :force => true do |t|
26
+ t.text :body
27
+ t.integer :post_id
28
+ t.foreign_key :post_id, :posts, :id
29
+ end
10
30
  end
31
+ class User < ::ActiveRecord::Base ; end
32
+ class Post < ::ActiveRecord::Base ; end
33
+ class Comment < ::ActiveRecord::Base ; end
34
+ end
11
35
 
12
- create_table :posts, :force => true do |t|
13
- t.text :body
14
- t.integer :user_id
15
- t.integer :author_id
16
- end
17
36
 
18
- create_table :comments, :force => true do |t|
19
- t.text :body
20
- t.integer :post_id
21
- t.foreign_key :post_id, :posts, :id
37
+ after(:each) do
38
+ migration.suppress_messages do
39
+ migration.remove_index(:users, :name => @index.name) if @index
22
40
  end
23
41
  end
24
- class User < ::ActiveRecord::Base ; end
25
- class Post < ::ActiveRecord::Base ; end
26
- class Comment < ::ActiveRecord::Base ; end
27
- end
28
42
 
29
- let(:migration) { ::ActiveRecord::Migration }
30
-
31
- after(:each) do
32
- migration.suppress_messages do
33
- migration.remove_index(:users, :name => @index.name) if @index
43
+ it "should create index when called without additional options" do
44
+ add_index(:users, :login)
45
+ index_for(:login).should_not be_nil
34
46
  end
35
- end
36
47
 
37
- it "should create index when called without additional options" do
38
- add_index(:users, :login)
39
- index_for(:login).should_not be_nil
40
- end
48
+ it "should create unique index" do
49
+ add_index(:users, :login, :unique => true)
50
+ index_for(:login).unique.should == true
51
+ end
41
52
 
42
- it "should create unique index" do
43
- add_index(:users, :login, :unique => true)
44
- index_for(:login).unique.should == true
45
- end
53
+ it "should assign given name" do
54
+ add_index(:users, :login, :name => 'users_login_index')
55
+ index_for(:login).name.should == 'users_login_index'
56
+ end
46
57
 
47
- it "should assign given name" do
48
- add_index(:users, :login, :name => 'users_login_index')
49
- index_for(:login).name.should == 'users_login_index'
50
- end
58
+ unless SchemaPlusHelpers.mysql?
59
+ it "should assign order" do
60
+ add_index(:users, [:login, :deleted_at], :order => {:login => :desc, :deleted_at => :asc})
61
+ index_for([:login, :deleted_at]).orders.should == {"login" => :desc, "deleted_at" => :asc}
62
+ end
63
+ end
51
64
 
52
- unless SchemaPlusHelpers.mysql?
53
- it "should assign order" do
54
- add_index(:users, [:login, :deleted_at], :order => {:login => :desc, :deleted_at => :asc})
55
- index_for([:login, :deleted_at]).orders.should == {"login" => :desc, "deleted_at" => :asc}
65
+ context "for duplicate index" do
66
+ it "should not complain if the index is the same" do
67
+ add_index(:users, :login)
68
+ index_for(:login).should_not be_nil
69
+ ActiveRecord::Base.logger.should_receive(:warn).with(/login.*Skipping/)
70
+ expect { add_index(:users, :login) }.to_not raise_error
71
+ index_for(:login).should_not be_nil
72
+ end
73
+ it "should complain if the index is different" do
74
+ add_index(:users, :login, :unique => true)
75
+ index_for(:login).should_not be_nil
76
+ expect { add_index(:users, :login) }.to raise_error
77
+ index_for(:login).should_not be_nil
78
+ end
56
79
  end
57
- end
58
80
 
81
+ if SchemaPlusHelpers.postgresql?
59
82
 
60
- context "for duplicate index" do
61
- it "should not complain if the index is the same" do
62
- add_index(:users, :login)
63
- index_for(:login).should_not be_nil
64
- ActiveRecord::Base.logger.should_receive(:warn).with(/login.*Skipping/)
65
- expect { add_index(:users, :login) }.to_not raise_error
66
- index_for(:login).should_not be_nil
67
- end
68
- it "should complain if the index is different" do
69
- add_index(:users, :login, :unique => true)
70
- index_for(:login).should_not be_nil
71
- expect { add_index(:users, :login) }.to raise_error
72
- index_for(:login).should_not be_nil
83
+ it "should assign conditions" do
84
+ add_index(:users, :login, :conditions => 'deleted_at IS NULL')
85
+ index_for(:login).conditions.should == '(deleted_at IS NULL)'
86
+ end
87
+
88
+ it "should assign expression, conditions and kind" do
89
+ add_index(:users, :expression => "USING hash (upper(login)) WHERE deleted_at IS NULL", :name => 'users_login_index')
90
+ @index = User.indexes.detect { |i| i.expression.present? }
91
+ @index.expression.should == "upper((login)::text)"
92
+ @index.conditions.should == "(deleted_at IS NULL)"
93
+ @index.kind.should == "hash"
94
+ end
95
+
96
+ it "should allow to specify expression, conditions and kind separately" do
97
+ add_index(:users, :kind => "hash", :expression => "upper(login)", :conditions => "deleted_at IS NULL", :name => 'users_login_index')
98
+ @index = User.indexes.detect { |i| i.expression.present? }
99
+ @index.expression.should == "upper((login)::text)"
100
+ @index.conditions.should == "(deleted_at IS NULL)"
101
+ @index.kind.should == "hash"
102
+ end
103
+
104
+ it "should allow to specify kind" do
105
+ add_index(:users, :login, :kind => "hash")
106
+ index_for(:login).kind.should == 'hash'
107
+ end
108
+
109
+ it "should allow to specify actual expression only" do
110
+ add_index(:users, :expression => "upper(login)", :name => 'users_login_index')
111
+ @index = User.indexes.detect { |i| i.name == 'users_login_index' }
112
+ @index.expression.should == "upper((login)::text)"
113
+ end
114
+
115
+ it "should raise if no column given and expression is missing" do
116
+ expect { add_index(:users, :name => 'users_login_index') }.to raise_error(ArgumentError, /expression/)
117
+ end
118
+
119
+ it "should raise if expression without name is given" do
120
+ expect { add_index(:users, :expression => "USING btree (login)") }.to raise_error(ArgumentError, /name/)
121
+ end
122
+
123
+ it "should raise if expression is given and case_sensitive is false" do
124
+ expect { add_index(:users, :name => 'users_login_index', :expression => "USING btree (login)", :case_sensitive => false) }.to raise_error(ArgumentError, /use LOWER/i)
125
+ end
126
+
127
+
128
+ end # of postgresql specific examples
129
+
130
+ protected
131
+
132
+ def index_for(column_names)
133
+ @index = User.indexes.detect { |i| i.columns == Array(column_names).collect(&:to_s) }
73
134
  end
135
+
74
136
  end
75
137
 
76
- if SchemaPlusHelpers.postgresql?
138
+ describe "remove_index" do
77
139
 
78
- it "should assign conditions" do
79
- add_index(:users, :login, :conditions => 'deleted_at IS NULL')
80
- index_for(:login).conditions.should == '(deleted_at IS NULL)'
140
+ before(:each) do
141
+ connection.tables.each do |table| connection.drop_table table, cascade: true end
142
+ define_schema(:auto_create => false) do
143
+ create_table :users, :force => true do |t|
144
+ t.string :login
145
+ t.datetime :deleted_at
146
+ end
147
+ end
148
+ class User < ::ActiveRecord::Base ; end
81
149
  end
82
150
 
83
- it "should assign expression, conditions and kind" do
84
- add_index(:users, :expression => "USING hash (upper(login)) WHERE deleted_at IS NULL", :name => 'users_login_index')
85
- @index = User.indexes.detect { |i| i.expression.present? }
86
- @index.expression.should == "upper((login)::text)"
87
- @index.conditions.should == "(deleted_at IS NULL)"
88
- @index.kind.should == "hash"
89
- end
90
151
 
91
- it "should allow to specify expression, conditions and kind separately" do
92
- add_index(:users, :kind => "hash", :expression => "upper(login)", :conditions => "deleted_at IS NULL", :name => 'users_login_index')
93
- @index = User.indexes.detect { |i| i.expression.present? }
94
- @index.expression.should == "upper((login)::text)"
95
- @index.conditions.should == "(deleted_at IS NULL)"
96
- @index.kind.should == "hash"
152
+ it "removes index by column name (symbols)" do
153
+ add_index :users, :login
154
+ User.indexes.length.should == 1
155
+ remove_index :users, :login
156
+ User.indexes.length.should == 0
97
157
  end
98
158
 
99
- it "should allow to specify kind" do
100
- add_index(:users, :login, :kind => "hash")
101
- index_for(:login).kind.should == 'hash'
159
+ it "removes index by column name (symbols)" do
160
+ add_index :users, :login
161
+ User.indexes.length.should == 1
162
+ remove_index 'users', 'login'
163
+ User.indexes.length.should == 0
102
164
  end
103
165
 
104
- it "should allow to specify actual expression only" do
105
- add_index(:users, :expression => "upper(login)", :name => 'users_login_index')
106
- @index = User.indexes.detect { |i| i.name == 'users_login_index' }
107
- @index.expression.should == "upper((login)::text)"
166
+ it "removes multi-column index by column names (symbols)" do
167
+ add_index :users, [:login, :deleted_at]
168
+ User.indexes.length.should == 1
169
+ remove_index :users, [:login, :deleted_at]
170
+ User.indexes.length.should == 0
108
171
  end
109
172
 
110
- it "should raise if no column given and expression is missing" do
111
- expect { add_index(:users, :name => 'users_login_index') }.to raise_error(ArgumentError, /expression/)
173
+ it "removes multi-column index by column names (strings)" do
174
+ add_index 'users', [:login, :deleted_at]
175
+ User.indexes.length.should == 1
176
+ remove_index 'users', ['login', 'deleted_at']
177
+ User.indexes.length.should == 0
112
178
  end
113
179
 
114
- it "should raise if expression without name is given" do
115
- expect { add_index(:users, :expression => "USING btree (login)") }.to raise_error(ArgumentError, /name/)
180
+ it "removes index if_exists" do
181
+ add_index :users, :login
182
+ User.indexes.length.should == 1
183
+ remove_index :users, :login, :if_exists => true
184
+ User.indexes.length.should == 0
116
185
  end
117
186
 
118
- it "should raise if expression is given and case_sensitive is false" do
119
- expect { add_index(:users, :name => 'users_login_index', :expression => "USING btree (login)", :case_sensitive => false) }.to raise_error(ArgumentError, /use LOWER/i)
187
+ it "raises exception if doesn't exist" do
188
+ expect {
189
+ remove_index :users, :login
190
+ }.to raise_error
120
191
  end
121
192
 
122
-
123
- end # of postgresql specific examples
193
+ it "doesn't raise exception with :if_exists" do
194
+ expect {
195
+ remove_index :users, :login, :if_exists => true
196
+ }.to_not raise_error
197
+ end
198
+ end
124
199
 
125
200
  protected
126
201
  def add_index(*args)
@@ -130,9 +205,11 @@ describe "add_index" do
130
205
  User.reset_column_information
131
206
  end
132
207
 
133
- def index_for(column_names)
134
- @index = User.indexes.detect { |i| i.columns == Array(column_names).collect(&:to_s) }
208
+ def remove_index(*args)
209
+ migration.suppress_messages do
210
+ migration.remove_index(*args)
211
+ end
212
+ User.reset_column_information
135
213
  end
136
214
 
137
-
138
215
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_plus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ronen Barzel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-26 00:00:00.000000000 Z
12
+ date: 2013-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails