22
33class PgSliceTest < Minitest ::Test
44 def setup
5- $conn . exec File . read ( "test/support/schema.sql" )
5+ execute File . read ( "test/support/schema.sql" )
66 end
77
88 def test_day
@@ -100,10 +100,10 @@ def test_unprep_missing_table
100100 private
101101
102102 def assert_period ( period , column : "createdAt" , trigger_based : false , tablespace : false , version : nil )
103- $conn . exec ( %!CREATE STATISTICS my_stats ON "Id", "UserId" FROM "Posts"! )
103+ execute %!CREATE STATISTICS my_stats ON "Id", "UserId" FROM "Posts"!
104104
105105 if server_version_num >= 120000 && !trigger_based
106- $conn . exec ( %!ALTER TABLE "Posts" ADD COLUMN "Gen" INTEGER GENERATED ALWAYS AS ("Id" * 10) STORED! )
106+ execute %!ALTER TABLE "Posts" ADD COLUMN "Gen" INTEGER GENERATED ALWAYS AS ("Id" * 10) STORED!
107107 end
108108
109109 run_command "prep Posts #{ column } #{ period } #{ "--trigger-based" if trigger_based } #{ "--test-version #{ version } " if version } "
@@ -144,12 +144,12 @@ def assert_period(period, column: "createdAt", trigger_based: false, tablespace:
144144 assert_equal 10000 , count ( "Posts_intermediate" )
145145
146146 # insert into old table
147- $conn . exec_params ( %!INSERT INTO "Posts" (#{ quote_ident ( column ) } ) VALUES ($1) RETURNING "Id"! , [ now . iso8601 ] ) . first
147+ execute %!INSERT INTO "Posts" (#{ quote_ident ( column ) } ) VALUES ($1) RETURNING "Id"! , [ now . iso8601 ]
148148
149149 run_command "analyze Posts"
150150 # https://github.com/postgres/postgres/commit/375aed36ad83f0e021e9bdd3a0034c0c992c66dc
151151 if server_version_num >= 150000
152- last_analyzed = $conn . exec ( "SELECT relname, last_analyze FROM pg_stat_user_tables WHERE relname LIKE 'Posts_%'" ) . to_a
152+ last_analyzed = execute ( "SELECT relname, last_analyze FROM pg_stat_user_tables WHERE relname LIKE 'Posts_%'" )
153153 assert_equal 4 , last_analyzed . count { |v | v [ "last_analyze" ] }
154154 end
155155
@@ -180,7 +180,7 @@ def assert_period(period, column: "createdAt", trigger_based: false, tablespace:
180180 assert_foreign_key new_partition_name
181181
182182 # test insert works
183- insert_result = $conn . exec_params ( %!INSERT INTO "Posts" (#{ quote_ident ( column ) } ) VALUES ($1) RETURNING "Id"! , [ now . iso8601 ] ) . first
183+ insert_result = execute ( %!INSERT INTO "Posts" (#{ quote_ident ( column ) } ) VALUES ($1) RETURNING "Id"! , [ now . iso8601 ] ) . first
184184 assert_equal 10002 , count ( "Posts" )
185185 if declarative
186186 assert insert_result [ "Id" ]
@@ -191,13 +191,13 @@ def assert_period(period, column: "createdAt", trigger_based: false, tablespace:
191191
192192 # test insert with null field
193193 error = assert_raises ( PG ::ServerError ) do
194- $conn . exec ( ' INSERT INTO "Posts" ("UserId") VALUES (1)' )
194+ execute %! INSERT INTO "Posts" ("UserId") VALUES (1)!
195195 end
196196 assert_includes error . message , "partition"
197197
198198 # test foreign key
199199 error = assert_raises ( PG ::ServerError ) do
200- $conn . exec ( %!INSERT INTO "Posts" (#{ quote_ident ( column ) } , "UserId") VALUES (NOW(), 1)! )
200+ execute %!INSERT INTO "Posts" (#{ quote_ident ( column ) } , "UserId") VALUES (NOW(), 1)!
201201 end
202202 assert_includes error . message , "violates foreign key constraint"
203203
@@ -252,36 +252,36 @@ def run_command(command, error: nil)
252252 end
253253
254254 def add_column ( table , column )
255- $conn . exec ( "ALTER TABLE #{ quote_ident ( table ) } ADD COLUMN #{ quote_ident ( column ) } timestamp" )
255+ execute "ALTER TABLE #{ quote_ident ( table ) } ADD COLUMN #{ quote_ident ( column ) } timestamp"
256256 end
257257
258258 def assert_column ( table , column )
259- assert_includes $conn . exec ( "SELECT * FROM #{ quote_ident ( table ) } LIMIT 0" ) . fields , column
259+ assert_includes execute ( "SELECT * FROM #{ quote_ident ( table ) } LIMIT 0" ) . fields , column
260260 end
261261
262262 def table_exists? ( table_name )
263- sql = <<~SQL
263+ query = <<~SQL
264264 SELECT * FROM information_schema.tables
265265 WHERE table_schema = 'public' AND table_name = $1
266266 SQL
267- result = $conn . exec_params ( sql , [ table_name ] )
267+ result = execute ( query , [ table_name ] )
268268 result . any?
269269 end
270270
271271 def count ( table_name , only : false )
272- result = $conn . exec <<~SQL
272+ result = execute <<~SQL
273273 SELECT COUNT(*) FROM #{ only ? "ONLY " : "" } #{ quote_ident ( table_name ) }
274274 SQL
275275 result . first [ "count" ] . to_i
276276 end
277277
278278 def primary_key ( table_name )
279- sql = <<~SQL
279+ query = <<~SQL
280280 SELECT pg_get_constraintdef(oid) AS def
281281 FROM pg_constraint
282282 WHERE contype = 'p' AND conrelid = $1::regclass
283283 SQL
284- result = $conn . exec_params ( sql , [ quote_ident ( table_name ) ] )
284+ result = execute ( query , [ quote_ident ( table_name ) ] )
285285 result . first
286286 end
287287
@@ -295,12 +295,12 @@ def refute_primary_key(table_name)
295295 end
296296
297297 def index ( table_name )
298- sql = <<~SQL
298+ query = <<~SQL
299299 SELECT pg_get_indexdef(indexrelid)
300300 FROM pg_index
301301 WHERE indrelid = $1::regclass AND indisprimary = 'f'
302302 SQL
303- result = $conn . exec_params ( sql , [ quote_ident ( table_name ) ] )
303+ result = execute ( query , [ quote_ident ( table_name ) ] )
304304 result . first
305305 end
306306
@@ -313,31 +313,39 @@ def refute_index(table_name)
313313 end
314314
315315 def assert_foreign_key ( table_name )
316- sql = <<~SQL
316+ query = <<~SQL
317317 SELECT pg_get_constraintdef(oid) AS def
318318 FROM pg_constraint
319319 WHERE contype = 'f' AND conrelid = $1::regclass
320320 SQL
321- result = $conn . exec_params ( sql , [ quote_ident ( table_name ) ] )
321+ result = execute ( query , [ quote_ident ( table_name ) ] )
322322 assert !result . detect { |row | row [ "def" ] =~ /\A FOREIGN KEY \( .*\) REFERENCES "Users"\( "Id"\) \z / } . nil? , "Missing foreign key on #{ table_name } "
323323 end
324324
325325 # extended statistics are built on partitioned tables
326326 # https://github.com/postgres/postgres/commit/20b9fa308ebf7d4a26ac53804fce1c30f781d60c
327327 # (backported to Postgres 10)
328328 def assert_statistics ( table_name )
329- sql = <<~SQL
329+ query = <<~SQL
330330 SELECT n_distinct
331331 FROM pg_stats_ext
332332 WHERE tablename = $1
333333 SQL
334- result = $conn . exec_params ( sql , [ table_name ] )
334+ result = execute ( query , [ table_name ] )
335335 assert result . any? , "Missing extended statistics on #{ table_name } "
336336 assert_equal %!{"1, 2": 10002}! , result . first [ "n_distinct" ]
337337 end
338338
339339 def server_version_num
340- $conn. exec ( "SHOW server_version_num" ) . first [ "server_version_num" ] . to_i
340+ execute ( "SHOW server_version_num" ) . first [ "server_version_num" ] . to_i
341+ end
342+
343+ def execute ( query , params = [ ] )
344+ if params . any?
345+ $conn. exec_params ( query , params )
346+ else
347+ $conn. exec ( query )
348+ end
341349 end
342350
343351 def quote_ident ( value )
0 commit comments