@@ -45,7 +45,8 @@ class SearchInAHaystack < ActiveRecord::Base
45
45
46
46
it "dumps a create_view for a materialized view in the database" do
47
47
view_definition = "SELECT 'needle'::text AS haystack"
48
- Search . connection . create_view :searches , materialized : true , sql_definition : view_definition
48
+ Search . connection . create_view :searches ,
49
+ materialized : true , sql_definition : view_definition
49
50
50
51
expect ( output ) . to include 'create_view "searches", materialized: true, sql_definition: <<-SQL'
51
52
expect ( output ) . to include view_definition
@@ -66,11 +67,11 @@ class SearchInAHaystack < ActiveRecord::Base
66
67
stream = StringIO . new
67
68
68
69
ActiveRecord ::SchemaDumper . dump ( Search . connection , stream )
70
+
69
71
views = stream . string . lines . grep ( /create_view/ ) . map do |view_line |
70
72
view_line . match ( 'create_view "(?<name>.*)"' ) [ :name ]
71
73
end
72
74
expect ( views ) . to eq ( %w[ scenic.bananas scenic.apples ] )
73
-
74
75
end
75
76
76
77
before ( :each ) do
@@ -112,12 +113,13 @@ class SearchInAHaystack < ActiveRecord::Base
112
113
context "with views using unexpected characters in name" do
113
114
it "dumps a create_view for a view in the database" do
114
115
view_definition = "SELECT 'needle'::text AS haystack"
115
- Search . connection . create_view '"search in a haystack"' , sql_definition : view_definition
116
+ Search . connection . create_view '"search in a haystack"' ,
117
+ sql_definition : view_definition
116
118
117
119
expect ( output ) . to include 'create_view "\"search in a haystack\"",'
118
120
expect ( output ) . to include view_definition
119
121
120
- Search . connection . drop_view :'" search in a haystack"'
122
+ Search . connection . drop_view :" \" search in a haystack\" "
121
123
122
124
silence_stream ( $stdout) { eval ( output ) } # standard:disable Security/Eval
123
125
@@ -129,15 +131,15 @@ class SearchInAHaystack < ActiveRecord::Base
129
131
it "dumps a create_view for a view in the database" do
130
132
view_definition = "SELECT 'needle'::text AS haystack"
131
133
Search . connection . execute (
132
- "CREATE SCHEMA scenic; SET search_path TO scenic, public" ,
134
+ "CREATE SCHEMA scenic; SET search_path TO scenic, public"
133
135
)
134
136
Search . connection . create_view 'scenic."search in a haystack"' ,
135
137
sql_definition : view_definition
136
138
137
139
expect ( output ) . to include 'create_view "scenic.\"search in a haystack\"",'
138
140
expect ( output ) . to include view_definition
139
141
140
- Search . connection . drop_view :' scenic."search in a haystack"'
142
+ Search . connection . drop_view :" scenic.\ " search in a haystack\" "
141
143
142
144
silence_stream ( $stdout) { eval ( output ) } # standard:disable Security/Eval
143
145
@@ -148,11 +150,11 @@ class SearchInAHaystack < ActiveRecord::Base
148
150
context "with views ordered by name" do
149
151
it "sorts views without dependencies" do
150
152
Search . connection . create_view "cucumber_needles" ,
151
- sql_definition : "SELECT 'kukumbas'::text as needle"
153
+ sql_definition : "SELECT 'kukumbas'::text AS needle"
152
154
Search . connection . create_view "vip_needles" ,
153
- sql_definition : "SELECT 'vip'::text as needle"
155
+ sql_definition : "SELECT 'vip'::text AS needle"
154
156
Search . connection . create_view "none_needles" ,
155
- sql_definition : "SELECT 'none_needles'::text as needle"
157
+ sql_definition : "SELECT 'none_needles'::text AS needle"
156
158
157
159
# Same here, no dependencies among existing views, all views are sorted
158
160
sorted_views = %w[ cucumber_needles vip_needles none_needles ] . sort
@@ -162,15 +164,15 @@ class SearchInAHaystack < ActiveRecord::Base
162
164
163
165
it "sorts according to dependencies" do
164
166
Search . connection . create_table ( :tasks ) { |t | t . integer :performer_id }
165
- Search . connection . create_table ( :notes ) { |t |
167
+ Search . connection . create_table ( :notes ) do |t |
166
168
t . text :title
167
169
t . integer :author_id
168
- }
170
+ end
169
171
Search . connection . create_table ( :users ) { |t | t . text :nickname }
170
- Search . connection . create_table ( :roles ) { |t |
172
+ Search . connection . create_table ( :roles ) do |t |
171
173
t . text :name
172
174
t . integer :user_id
173
- }
175
+ end
174
176
175
177
Search . connection . create_view "recent_tasks" , sql_definition : <<-SQL
176
178
SELECT id, performer_id FROM tasks WHERE id > 42
@@ -186,26 +188,26 @@ class SearchInAHaystack < ActiveRecord::Base
186
188
Search . connection . create_view "angry_zombies" , sql_definition : <<-SQL
187
189
SELECT
188
190
users.nickname,
189
- recent_tasks.id AS task_id,
190
- nirvana_notes.title AS talk
191
+ recent_tasks.id AS task_id,
192
+ nirvana_notes.title AS talk
191
193
FROM users
192
194
JOIN roles ON roles.user_id = users.id
193
195
JOIN nirvana_notes ON nirvana_notes.author_id = roles.id
194
196
JOIN recent_tasks ON recent_tasks.performer_id = roles.id
195
197
SQL
196
198
Search . connection . create_view "doctor_zombies" , sql_definition : <<-SQL
197
- SELECT id FROM old_roles WHERE name LIKE '%Dr %'
199
+ SELECT id FROM old_roles WHERE name LIKE '%dr %'
198
200
SQL
199
201
Search . connection . create_view "xenomorphs" , sql_definition : <<-SQL
200
202
SELECT id, name FROM roles WHERE name = 'xeno'
201
203
SQL
202
204
Search . connection . create_view "important_messages" , sql_definition : <<-SQL
203
- SELECT id, title FROM notes WHERE title LIKE '%IMPORTANT %'
205
+ SELECT id, title FROM notes WHERE title LIKE '%important %'
204
206
SQL
205
207
206
- # Converted with https://github.com/ggerganov/dot-to-ascii
208
+ # converted with https://github.com/ggerganov/dot-to-ascii
207
209
# digraph {
208
- # rankdir = "RL ";
210
+ # rankdir = "bt ";
209
211
# xenomorphs;
210
212
# important_messages;
211
213
# doctor_zombies -> old_roles;
@@ -214,21 +216,33 @@ class SearchInAHaystack < ActiveRecord::Base
214
216
# angry_zombies -> recent_tasks;
215
217
# }
216
218
#
217
- # +--------------------+
218
- # | recent_tasks |
219
- # +--------------------+
220
- # ^
221
- # |
222
- # |
223
- # +----------------+ +-----------+ +---------------+ +--------------------+
224
- # | doctor_zombies | --> | old_roles | <-- | nirvana_notes | <-- | angry_zombies |
225
- # +----------------+ +-----------+ +---------------+ +--------------------+
226
- # +--------------------+
227
- # | important_messages |
228
- # +--------------------+
229
- # +--------------------+
230
- # | xenomorphs |
231
- # +--------------------+
219
+ # +--------------------+
220
+ # | doctor_zombies |
221
+ # +--------------------+
222
+ # |
223
+ # |
224
+ # v
225
+ # +--------------------+
226
+ # | old_roles |
227
+ # +--------------------+
228
+ # ^
229
+ # |
230
+ # |
231
+ # +--------------------+
232
+ # | nirvana_notes |
233
+ # +--------------------+
234
+ # ^
235
+ # |
236
+ # |
237
+ # +--------------------+ +--------------+
238
+ # | angry_zombies | --> | recent_tasks |
239
+ # +--------------------+ +--------------+
240
+ # +--------------------+
241
+ # | important_messages |
242
+ # +--------------------+
243
+ # +--------------------+
244
+ # | xenomorphs |
245
+ # +--------------------+
232
246
expect ( views_in_output_order ) . to eq ( %w[
233
247
old_roles
234
248
nirvana_notes
0 commit comments