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 +4 -4
- data/README.md +4 -0
- data/lib/schema_plus/active_record/connection_adapters/abstract_adapter.rb +5 -3
- data/lib/schema_plus/version.rb +1 -1
- data/spec/index_spec.rb +167 -90
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65c72cd47992bb6bf783d19b59e5d518e9256acc
|
4
|
+
data.tar.gz: fc0f8382dfb38c00719a1e86286390de3b7726a7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdedc86c2659b80c404e189f178848426e60aea035fec8e3dc144fe48212dff4ce8139a24b34103f01c57a72a8099f5c9901d105cab5ffcb800d38983b1d1914
|
7
|
+
data.tar.gz: 251070435963c3a912f0f097020f707adae3aae4861316f33a245448ba32fd1fef90e908816ee239c70f9e92309f891ada6c12fc9111bb4bf83484db7ca5d760
|
data/README.md
CHANGED
@@ -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,
|
135
|
-
|
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
|
-
|
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
|
data/lib/schema_plus/version.rb
CHANGED
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 "
|
3
|
+
describe "index" do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
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
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
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
|
-
|
138
|
+
describe "remove_index" do
|
77
139
|
|
78
|
-
|
79
|
-
|
80
|
-
|
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 "
|
92
|
-
add_index
|
93
|
-
|
94
|
-
|
95
|
-
|
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 "
|
100
|
-
add_index
|
101
|
-
|
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 "
|
105
|
-
add_index
|
106
|
-
|
107
|
-
|
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 "
|
111
|
-
|
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 "
|
115
|
-
|
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 "
|
119
|
-
expect {
|
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
|
-
|
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
|
134
|
-
|
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.
|
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-
|
12
|
+
date: 2013-10-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|