schema_plus 1.5.2 → 1.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +1 -1
- data/gemfiles/rails-4.1/Gemfile.base +1 -1
- data/lib/schema_plus/active_record/column_options_handler.rb +1 -0
- data/lib/schema_plus/active_record/connection_adapters/column.rb +1 -1
- data/lib/schema_plus/active_record/connection_adapters/schema_statements.rb +1 -0
- data/lib/schema_plus/active_record/connection_adapters/table_definition.rb +2 -0
- data/lib/schema_plus/version.rb +1 -1
- data/runspecs +2 -2
- data/schema_plus.gemspec +3 -3
- data/spec/column_default_spec.rb +156 -0
- data/spec/column_spec.rb +20 -20
- data/spec/connection_spec.rb +1 -1
- data/spec/foreign_key_definition_spec.rb +3 -3
- data/spec/foreign_key_spec.rb +14 -12
- data/spec/index_definition_spec.rb +23 -23
- data/spec/index_spec.rb +34 -34
- data/spec/migration_spec.rb +132 -120
- data/spec/named_schemas_spec.rb +15 -15
- data/spec/schema_dumper_spec.rb +36 -36
- data/spec/schema_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -1
- data/spec/support/matchers/have_index.rb +20 -13
- data/spec/support/matchers/reference.rb +3 -3
- data/spec/views_spec.rb +17 -17
- metadata +9 -15
- data/spec/column_definition_spec.rb +0 -212
data/spec/named_schemas_spec.rb
CHANGED
@@ -46,13 +46,13 @@ describe "with multiple schemas" do
|
|
46
46
|
|
47
47
|
it "should not find indexes in other schema" do
|
48
48
|
User.reset_column_information
|
49
|
-
User.indexes.
|
49
|
+
expect(User.indexes).to be_empty
|
50
50
|
end
|
51
51
|
|
52
52
|
it "should find index in current schema" do
|
53
53
|
connection.execute 'CREATE INDEX index_users_on_login ON users (login)'
|
54
54
|
User.reset_column_information
|
55
|
-
User.indexes.map(&:name).
|
55
|
+
expect(User.indexes.map(&:name)).to eq(['index_users_on_login'])
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
@@ -72,12 +72,12 @@ describe "with multiple schemas" do
|
|
72
72
|
end
|
73
73
|
|
74
74
|
it "should not find views in other schema" do
|
75
|
-
connection.views.
|
75
|
+
expect(connection.views).to be_empty
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should find views in this schema" do
|
79
79
|
connection.execute 'CREATE VIEW myview AS SELECT * FROM users'
|
80
|
-
connection.views.
|
80
|
+
expect(connection.views).to eq(['myview'])
|
81
81
|
end
|
82
82
|
end
|
83
83
|
|
@@ -106,9 +106,9 @@ describe "with multiple schemas" do
|
|
106
106
|
t.integer :user_id, :foreign_key => false
|
107
107
|
end
|
108
108
|
Comment.reset_column_information
|
109
|
-
Comment.foreign_keys.length.
|
109
|
+
expect(Comment.foreign_keys.length).to eq(0)
|
110
110
|
User.reset_column_information
|
111
|
-
User.reverse_foreign_keys.length.
|
111
|
+
expect(User.reverse_foreign_keys.length).to eq(0)
|
112
112
|
end
|
113
113
|
|
114
114
|
it "should find foreign keys in this schema" do
|
@@ -116,9 +116,9 @@ describe "with multiple schemas" do
|
|
116
116
|
t.integer :user_id, :foreign_key => true
|
117
117
|
end
|
118
118
|
Comment.reset_column_information
|
119
|
-
Comment.foreign_keys.map(&:column_names).flatten.
|
119
|
+
expect(Comment.foreign_keys.map(&:column_names).flatten).to eq(["user_id"])
|
120
120
|
User.reset_column_information
|
121
|
-
User.reverse_foreign_keys.map(&:column_names).flatten.
|
121
|
+
expect(User.reverse_foreign_keys.map(&:column_names).flatten).to eq(["user_id"])
|
122
122
|
end
|
123
123
|
|
124
124
|
end
|
@@ -157,25 +157,25 @@ describe "with multiple schemas" do
|
|
157
157
|
end
|
158
158
|
|
159
159
|
it "should find foreign keys" do
|
160
|
-
Member.foreign_keys.
|
160
|
+
expect(Member.foreign_keys).not_to be_empty
|
161
161
|
end
|
162
162
|
|
163
163
|
it "should find reverse foreign keys" do
|
164
|
-
Group.reverse_foreign_keys.
|
164
|
+
expect(Group.reverse_foreign_keys).not_to be_empty
|
165
165
|
end
|
166
166
|
|
167
167
|
it "should reference table in same schema" do
|
168
|
-
Member.foreign_keys.map(&:references_table_name).
|
168
|
+
expect(Member.foreign_keys.map(&:references_table_name)).to include "schema_plus_test2.groups"
|
169
169
|
end
|
170
170
|
|
171
171
|
it "should reference table in default schema" do
|
172
|
-
Member.foreign_keys.map(&:references_table_name).
|
172
|
+
expect(Member.foreign_keys.map(&:references_table_name)).to include "items"
|
173
173
|
end unless SchemaPlusHelpers.mysql?
|
174
174
|
|
175
175
|
it "should include the schema in the constraint name" do
|
176
176
|
expected_names = ["fk_schema_plus_test2_members_group_id"]
|
177
177
|
expected_names << "fk_schema_plus_test2_members_item_id" unless SchemaPlusHelpers.mysql?
|
178
|
-
Member.foreign_keys.map(&:name).
|
178
|
+
expect(Member.foreign_keys.map(&:name).sort).to match_array(expected_names.sort)
|
179
179
|
end
|
180
180
|
end
|
181
181
|
|
@@ -199,14 +199,14 @@ describe "with multiple schemas" do
|
|
199
199
|
end
|
200
200
|
|
201
201
|
before(:each) do
|
202
|
-
connection.
|
202
|
+
allow(connection).to receive(:adapter_name).and_return('PostGIS')
|
203
203
|
end
|
204
204
|
|
205
205
|
it "should hide views in postgis schema" do
|
206
206
|
begin
|
207
207
|
connection.create_view "postgis.hidden", "select 1", :force => true
|
208
208
|
connection.create_view :myview, "select 2", :force => true
|
209
|
-
connection.views.
|
209
|
+
expect(connection.views).to eq(["myview"])
|
210
210
|
ensure
|
211
211
|
connection.execute 'DROP VIEW postgis.hidden' rescue nil
|
212
212
|
connection.execute 'DROP VIEW myview' rescue nil
|
data/spec/schema_dumper_spec.rb
CHANGED
@@ -49,39 +49,39 @@ describe "Schema dump" do
|
|
49
49
|
|
50
50
|
it "should include foreign_key definition" do
|
51
51
|
with_foreign_key Post, :user_id, :users, :id do
|
52
|
-
dump_posts.
|
52
|
+
expect(dump_posts).to match(to_regexp(%q{t.foreign_key ["user_id"], "users", ["id"]}))
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
it "should include foreign_key name" do
|
57
57
|
with_foreign_key Post, :user_id, :users, :id, :name => "yippee" do
|
58
|
-
dump_posts.
|
58
|
+
expect(dump_posts).to match /foreign_key.*user_id.*users.*id.*:name => "yippee"/
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should sort foreign_key definitions" do
|
63
63
|
with_foreign_keys Comment, [ [ :post_id, :posts, :id ], [ :commenter_id, :users, :id ]] do
|
64
|
-
dump_schema.
|
64
|
+
expect(dump_schema).to match(/foreign_key.+commenter_id.+foreign_key.+post_id/m)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
68
68
|
context "with constraint dependencies" do
|
69
69
|
it "should sort in Posts => Comments direction" do
|
70
70
|
with_foreign_key Comment, :post_id, :posts, :id do
|
71
|
-
dump_schema.
|
71
|
+
expect(dump_schema).to match(%r{create_table "posts".*create_table "comments"}m)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
it "should sort in Comments => Posts direction" do
|
75
75
|
with_foreign_key Post, :first_comment_id, :comments, :id do
|
76
|
-
dump_schema.
|
76
|
+
expect(dump_schema).to match(%r{create_table "comments".*create_table "posts"}m)
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
80
|
it "should handle regexp in ignore_tables" do
|
81
81
|
with_foreign_key Comment, :post_id, :posts, :id do
|
82
82
|
dump = dump_schema(:ignore => /post/)
|
83
|
-
dump.
|
84
|
-
dump.
|
83
|
+
expect(dump).to match /create_table "comments"/
|
84
|
+
expect(dump).not_to match /create_table "posts"/
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
@@ -91,19 +91,19 @@ describe "Schema dump" do
|
|
91
91
|
if SchemaPlusHelpers.postgresql?
|
92
92
|
it "should dump the default hash expr as now()" do
|
93
93
|
with_additional_column Post, :posted_at, :datetime, :default => :now do
|
94
|
-
dump_posts.
|
94
|
+
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}})
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should dump the default hash expr as CURRENT_TIMESTAMP" do
|
99
99
|
with_additional_column Post, :posted_at, :datetime, :default => {:expr => 'date \'2001-09-28\''} do
|
100
|
-
dump_posts.
|
100
|
+
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*'2001-09-28 00:00:00'})
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
104
104
|
it "can dump a complex default expression" do
|
105
105
|
with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
|
106
|
-
dump_posts.
|
106
|
+
expect(dump_posts).to match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
|
107
107
|
end
|
108
108
|
end
|
109
109
|
end
|
@@ -111,19 +111,19 @@ describe "Schema dump" do
|
|
111
111
|
if SchemaPlusHelpers.sqlite3?
|
112
112
|
it "should dump the default hash expr as now" do
|
113
113
|
with_additional_column Post, :posted_at, :datetime, :default => :now do
|
114
|
-
dump_posts.
|
114
|
+
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
118
|
it "should dump the default hash expr string as now" do
|
119
119
|
with_additional_column Post, :posted_at, :datetime, :default => { :expr => "(DATETIME('now'))" } do
|
120
|
-
dump_posts.
|
120
|
+
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})
|
121
121
|
end
|
122
122
|
end
|
123
123
|
|
124
124
|
it "should dump the default value normally" do
|
125
125
|
with_additional_column Post, :posted_at, :string, :default => "now" do
|
126
|
-
dump_posts.
|
126
|
+
expect(dump_posts).to match(%r{t\.string\s*"posted_at",\s*(?:default:|:default =>)\s*"now"})
|
127
127
|
end
|
128
128
|
end
|
129
129
|
end
|
@@ -134,37 +134,37 @@ describe "Schema dump" do
|
|
134
134
|
ActiveRecord::Migration.suppress_messages do
|
135
135
|
ActiveRecord::Migration.change_column_default :posts, :string_no_default, nil
|
136
136
|
end
|
137
|
-
dump_posts.
|
137
|
+
expect(dump_posts).to match(%r{t\.string\s+"string_no_default"\s*$})
|
138
138
|
end
|
139
139
|
|
140
140
|
it "should include foreign_key options" do
|
141
141
|
with_foreign_key Post, :user_id, :users, :id, :on_update => :cascade, :on_delete => :set_null do
|
142
|
-
dump_posts.
|
142
|
+
expect(dump_posts).to match(to_regexp(%q{t.foreign_key ["user_id"], "users", ["id"], :on_update => :cascade, :on_delete => :set_null}))
|
143
143
|
end
|
144
144
|
end
|
145
145
|
|
146
146
|
it "should include index definition" do
|
147
147
|
with_index Post, :user_id do
|
148
|
-
dump_posts.
|
148
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"]}))
|
149
149
|
end
|
150
150
|
end
|
151
151
|
|
152
152
|
it "should include index name" do
|
153
153
|
with_index Post, :user_id, :name => "custom_name" do
|
154
|
-
dump_posts.
|
154
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "custom_name"}))
|
155
155
|
end
|
156
156
|
end
|
157
157
|
|
158
158
|
it "should define unique index" do
|
159
159
|
with_index Post, :user_id, :name => "posts_user_id_index", :unique => true do
|
160
|
-
dump_posts.
|
160
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "posts_user_id_index", :unique => true}))
|
161
161
|
end
|
162
162
|
end
|
163
163
|
|
164
164
|
it "should sort indexes" do
|
165
165
|
with_index Post, :user_id do
|
166
166
|
with_index Post, :short_id do
|
167
|
-
dump_posts.
|
167
|
+
expect(dump_posts).to match(/on_short_id.+on_user_id/m)
|
168
168
|
end
|
169
169
|
end
|
170
170
|
end
|
@@ -173,7 +173,7 @@ describe "Schema dump" do
|
|
173
173
|
|
174
174
|
it "should include index order" do
|
175
175
|
with_index Post, [:user_id, :first_comment_id, :short_id], :order => { :user_id => :asc, :first_comment_id => :desc } do
|
176
|
-
dump_posts.
|
176
|
+
expect(dump_posts).to match(%r{t.index \["user_id", "first_comment_id", "short_id"\],.*:order => {"user_id" => :asc, "first_comment_id" => :desc, "short_id" => :asc}})
|
177
177
|
end
|
178
178
|
end
|
179
179
|
|
@@ -183,20 +183,20 @@ describe "Schema dump" do
|
|
183
183
|
|
184
184
|
it "should define case insensitive index" do
|
185
185
|
with_index Post, [:body, :string_no_default], :case_sensitive => false do
|
186
|
-
dump_posts.
|
186
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["body", "string_no_default"], :name => "index_posts_on_body_and_string_no_default", :case_sensitive => false}))
|
187
187
|
end
|
188
188
|
end
|
189
189
|
|
190
190
|
it "should define index with type cast" do
|
191
191
|
with_index Post, [:integer_col], :name => "index_with_type_cast", :expression => "LOWER(integer_col::text)" do
|
192
|
-
dump_posts.
|
192
|
+
expect(dump_posts).to match(to_regexp(%q{t.index :name => "index_with_type_cast", :expression => "lower((integer_col)::text)"}))
|
193
193
|
end
|
194
194
|
end
|
195
195
|
|
196
196
|
|
197
197
|
it "should define case insensitive index with mixed ids and strings" do
|
198
198
|
with_index Post, [:user_id, :str_short, :short_id, :body], :case_sensitive => false do
|
199
|
-
dump_posts.
|
199
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id", "str_short", "short_id", "body"], :name => "index_posts_on_user_id_and_str_short_and_short_id_and_body", :case_sensitive => false}))
|
200
200
|
end
|
201
201
|
end
|
202
202
|
|
@@ -204,47 +204,47 @@ describe "Schema dump" do
|
|
204
204
|
col_name = "#{col_type}_col"
|
205
205
|
it "should define case insensitive index that includes an #{col_type}" do
|
206
206
|
with_index Post, [:user_id, :str_short, col_name, :body], :case_sensitive => false do
|
207
|
-
dump_posts.
|
207
|
+
expect(dump_posts).to match(to_regexp(%Q!t.index ["user_id", "str_short", "#{col_name}", "body"], :name => "index_posts_on_user_id_and_str_short_and_#{col_name}_and_body", :case_sensitive => false!))
|
208
208
|
end
|
209
209
|
end
|
210
210
|
end
|
211
211
|
|
212
212
|
it "should define conditions" do
|
213
213
|
with_index Post, :user_id, :name => "posts_user_id_index", :conditions => "user_id IS NOT NULL" do
|
214
|
-
dump_posts.
|
214
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "posts_user_id_index", :conditions => "(user_id IS NOT NULL)"}))
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
218
218
|
it "should define expression" do
|
219
219
|
with_index Post, :name => "posts_freaky_index", :expression => "USING hash (least(id, user_id))" do
|
220
|
-
dump_posts.
|
220
|
+
expect(dump_posts).to match(to_regexp(%q{t.index :name => "posts_freaky_index", :kind => "hash", :expression => "LEAST(id, user_id)"}))
|
221
221
|
end
|
222
222
|
end
|
223
223
|
|
224
224
|
it "should dump unique: true with expression (Issue #142)" do
|
225
225
|
with_index Post, :name => "posts_user_body_index", :unique => true, :expression => "BTRIM(LOWER(body))" do
|
226
|
-
dump_posts.
|
226
|
+
expect(dump_posts).to match(%r{#{to_regexp(%q{t.index :name => "posts_user_body_index", :unique => true, :expression => "btrim(lower(body))"})}$})
|
227
227
|
end
|
228
228
|
end
|
229
229
|
|
230
230
|
|
231
231
|
it "should not define :case_sensitive => false with non-trivial expression" do
|
232
232
|
with_index Post, :name => "posts_user_body_index", :expression => "BTRIM(LOWER(body))" do
|
233
|
-
dump_posts.
|
233
|
+
expect(dump_posts).to match(%r{#{to_regexp(%q{t.index :name => "posts_user_body_index", :expression => "btrim(lower(body))"})}$})
|
234
234
|
end
|
235
235
|
end
|
236
236
|
|
237
237
|
|
238
238
|
it "should define kind" do
|
239
239
|
with_index Post, :name => "posts_body_index", :expression => "USING hash (body)" do
|
240
|
-
dump_posts.
|
240
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["body"], :name => "posts_body_index", :kind => "hash"}))
|
241
241
|
end
|
242
242
|
end
|
243
243
|
|
244
244
|
it "should not include index order for non-ordered index types" do
|
245
245
|
with_index Post, :user_id, :kind => :hash do
|
246
|
-
dump_posts.
|
247
|
-
dump_posts.
|
246
|
+
expect(dump_posts).to match(to_regexp(%q{t.index ["user_id"], :name => "index_posts_on_user_id", :kind => "hash"}))
|
247
|
+
expect(dump_posts).not_to match(%r{:order})
|
248
248
|
end
|
249
249
|
end
|
250
250
|
|
@@ -265,11 +265,11 @@ describe "Schema dump" do
|
|
265
265
|
end
|
266
266
|
|
267
267
|
it "should dump constraints after the tables they reference" do
|
268
|
-
dump_schema.
|
269
|
-
dump_schema.
|
270
|
-
dump_schema.
|
271
|
-
dump_schema.
|
272
|
-
dump_schema.
|
268
|
+
expect(dump_schema).to match(%r{create_table "comments".*foreign_key.*\["first_comment_id"\], "comments", \["id"\]}m)
|
269
|
+
expect(dump_schema).to match(%r{create_table "posts".*foreign_key.*\["first_post_id"\], "posts", \["id"\]}m)
|
270
|
+
expect(dump_schema).to match(%r{create_table "posts".*foreign_key.*\["post_id"\], "posts", \["id"\]}m)
|
271
|
+
expect(dump_schema).to match(%r{create_table "users".*foreign_key.*\["commenter_id"\], "users", \["id"\]}m)
|
272
|
+
expect(dump_schema).to match(%r{create_table "users".*foreign_key.*\["user_id"\], "users", \["id"\]}m)
|
273
273
|
end
|
274
274
|
end
|
275
275
|
end
|
data/spec/schema_spec.rb
CHANGED
@@ -23,12 +23,12 @@ describe ActiveRecord::Schema do
|
|
23
23
|
it "should create only explicity added indexes" do
|
24
24
|
do_schema
|
25
25
|
expected = SchemaPlusHelpers.mysql? ? 2 : 1
|
26
|
-
connection.tables.collect { |table| connection.indexes(table) }.flatten.
|
26
|
+
expect(connection.tables.collect { |table| connection.indexes(table) }.flatten.size).to eq(expected)
|
27
27
|
end
|
28
28
|
|
29
29
|
it "should create only explicity added foriegn keys" do
|
30
30
|
do_schema
|
31
|
-
connection.tables.collect { |table| connection.foreign_keys(table) }.flatten.
|
31
|
+
expect(connection.tables.collect { |table| connection.foreign_keys(table) }.flatten.size).to eq(2)
|
32
32
|
end
|
33
33
|
|
34
34
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -3,25 +3,34 @@ module SchemaPlusMatchers
|
|
3
3
|
class HaveIndex
|
4
4
|
|
5
5
|
def initialize(expectation, options = {})
|
6
|
-
set_required_columns(expectation
|
6
|
+
set_required_columns(expectation)
|
7
|
+
@unique = options.delete(:unique)
|
8
|
+
@name = options.delete(:name)
|
7
9
|
end
|
8
10
|
|
9
11
|
def matches?(model)
|
10
12
|
@model = model
|
11
|
-
@model.indexes.
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
indexes = @model.indexes.select { |index| index.columns.to_set == @required_columns }
|
14
|
+
if indexes.length > 1
|
15
|
+
@too_many = indexes.length
|
16
|
+
return false
|
15
17
|
end
|
18
|
+
index = indexes.first
|
19
|
+
return index && (@unique ? index.unique : true) && (@name ? index.name == @name.to_s : true)
|
16
20
|
end
|
17
21
|
|
18
|
-
def
|
19
|
-
invert = should_not ? "not to" : ""
|
20
|
-
|
22
|
+
def failure_message(should_not = false)
|
23
|
+
invert = should_not ? "not to" : "to"
|
24
|
+
what = ""
|
25
|
+
what += "unique " if @unique
|
26
|
+
what += "named '{@name}'" if @name
|
27
|
+
msg = "Expected #{@model.table_name} #{invert} contain one #{what}index on #{@required_columns.entries.inspect}"
|
28
|
+
msg += "; found #{@too_many} indexes" if @too_many
|
29
|
+
msg
|
21
30
|
end
|
22
31
|
|
23
|
-
def
|
24
|
-
|
32
|
+
def failure_message_when_negated
|
33
|
+
failure_message(true)
|
25
34
|
end
|
26
35
|
|
27
36
|
def on(expectation)
|
@@ -30,10 +39,8 @@ module SchemaPlusMatchers
|
|
30
39
|
end
|
31
40
|
|
32
41
|
private
|
33
|
-
def set_required_columns(expectation
|
42
|
+
def set_required_columns(expectation)
|
34
43
|
@required_columns = Array(expectation).collect(&:to_s).to_set
|
35
|
-
@unique = options.delete(:unique)
|
36
|
-
@name = options.delete(:name)
|
37
44
|
end
|
38
45
|
|
39
46
|
end
|
@@ -27,7 +27,7 @@ module SchemaPlusMatchers
|
|
27
27
|
!@result.empty?
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
30
|
+
def failure_message(should_not = false)
|
31
31
|
target_column_names = @column_names.present? ? "(#{@column_names.join(', ')})" : ""
|
32
32
|
destinantion_column_names = @references_table_name ? "#{@references_table_name}(#{@references_column_names.join(', ')})" : "anything"
|
33
33
|
invert = should_not ? 'not' : ''
|
@@ -41,8 +41,8 @@ module SchemaPlusMatchers
|
|
41
41
|
msg
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
45
|
-
|
44
|
+
def failure_message_when_negated
|
45
|
+
failure_message(true)
|
46
46
|
end
|
47
47
|
|
48
48
|
def on(*column_names)
|
data/spec/views_spec.rb
CHANGED
@@ -26,36 +26,36 @@ describe ActiveRecord do
|
|
26
26
|
end
|
27
27
|
|
28
28
|
it "should query correctly" do
|
29
|
-
AOnes.all.collect(&:s).
|
30
|
-
ABOnes.all.collect(&:s).
|
29
|
+
expect(AOnes.all.collect(&:s)).to eq(%W[one_one one_two])
|
30
|
+
expect(ABOnes.all.collect(&:s)).to eq(%W[one_one])
|
31
31
|
end
|
32
32
|
|
33
33
|
it "should instrospect" do
|
34
34
|
# for postgresql, ignore views named pg_*
|
35
|
-
connection.views.sort.
|
36
|
-
connection.view_definition('a_ones').
|
37
|
-
connection.view_definition('ab_ones').
|
35
|
+
expect(connection.views.sort).to eq(%W[a_ones ab_ones])
|
36
|
+
expect(connection.view_definition('a_ones')).to match(%r{^ ?SELECT .*b.*,.*s.* FROM .*items.* WHERE .*a.* = 1}mi)
|
37
|
+
expect(connection.view_definition('ab_ones')).to match(%r{^ ?SELECT .*s.* FROM .*a_ones.* WHERE .*b.* = 1}mi)
|
38
38
|
end
|
39
39
|
|
40
40
|
it "should not be listed as a table" do
|
41
|
-
connection.tables.
|
42
|
-
connection.tables.
|
41
|
+
expect(connection.tables).not_to include('a_ones')
|
42
|
+
expect(connection.tables).not_to include('ab_ones')
|
43
43
|
end
|
44
44
|
|
45
45
|
|
46
46
|
it "should be included in schema dump" do
|
47
|
-
dump.
|
48
|
-
dump.
|
47
|
+
expect(dump).to match(%r{create_view "a_ones", " ?SELECT .*b.*,.*s.* FROM .*items.* WHERE .*a.* = 1.*, :force => true}mi)
|
48
|
+
expect(dump).to match(%r{create_view "ab_ones", " ?SELECT .*s.* FROM .*a_ones.* WHERE .*b.* = 1.*, :force => true}mi)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should be included in schema dump in dependency order" do
|
52
|
-
dump.
|
52
|
+
expect(dump).to match(%r{create_table "items".*create_view "a_ones".*create_view "ab_ones"}m)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "should not be included in schema if listed in ignore_tables" do
|
56
56
|
dump(ignore_tables: /b_/) do |dump|
|
57
|
-
dump.
|
58
|
-
dump.
|
57
|
+
expect(dump).to match(%r{create_view "a_ones", " ?SELECT .*b.*,.*s.* FROM .*items.* WHERE .*a.* = 1.*, :force => true}mi)
|
58
|
+
expect(dump).not_to match(%r{"ab_ones"})
|
59
59
|
end
|
60
60
|
end
|
61
61
|
|
@@ -69,7 +69,7 @@ describe ActiveRecord do
|
|
69
69
|
# initialize the test database when testing. this meant that the
|
70
70
|
# test database had views into the development database.
|
71
71
|
db = connection.respond_to?(:current_database)? connection.current_database : ActiveRecord::Base.configurations['schema_plus'][:database]
|
72
|
-
dump.
|
72
|
+
expect(dump).not_to match(%r{#{connection.quote_table_name(db)}[.]})
|
73
73
|
end
|
74
74
|
|
75
75
|
context "duplicate view creation" do
|
@@ -91,7 +91,7 @@ describe ActiveRecord do
|
|
91
91
|
|
92
92
|
it "should override existing definition if :force true" do
|
93
93
|
migration.create_view('dupe_me', 'SELECT * FROM items WHERE (a=2)', :force => true)
|
94
|
-
connection.view_definition('dupe_me').
|
94
|
+
expect(connection.view_definition('dupe_me')).to match(%r{WHERE .*a.*=.*2}i)
|
95
95
|
end
|
96
96
|
end
|
97
97
|
|
@@ -111,17 +111,17 @@ describe ActiveRecord do
|
|
111
111
|
|
112
112
|
it "should introspect WITH CHECK OPTION" do
|
113
113
|
migration.create_view :check, 'SELECT * FROM items WHERE (a=2) WITH CHECK OPTION'
|
114
|
-
connection.view_definition('check').
|
114
|
+
expect(connection.view_definition('check')).to match(%r{WITH CASCADED CHECK OPTION$})
|
115
115
|
end
|
116
116
|
|
117
117
|
it "should introspect WITH CASCADED CHECK OPTION" do
|
118
118
|
migration.create_view :check, 'SELECT * FROM items WHERE (a=2) WITH CASCADED CHECK OPTION'
|
119
|
-
connection.view_definition('check').
|
119
|
+
expect(connection.view_definition('check')).to match(%r{WITH CASCADED CHECK OPTION$})
|
120
120
|
end
|
121
121
|
|
122
122
|
it "should introspect WITH LOCAL CHECK OPTION" do
|
123
123
|
migration.create_view :check, 'SELECT * FROM items WHERE (a=2) WITH LOCAL CHECK OPTION'
|
124
|
-
connection.view_definition('check').
|
124
|
+
expect(connection.view_definition('check')).to match(%r{WITH LOCAL CHECK OPTION$})
|
125
125
|
end
|
126
126
|
end
|
127
127
|
end
|