Skip to content

Commit a13b874

Browse files
author
Thomas Hareau
authored
not adding index concurrently if table creation (#32)
* Not adding index concurrently if table creation * Using :default instead of nil
1 parent aecbcc3 commit a13b874

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/safe-pg-migrations/plugins/statement_insurer.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module SafePgMigrations
44
module StatementInsurer
55
PG_11_VERSION_NUM = 110_000
66

7-
%i[change_column_null change_column create_table].each do |method|
7+
%i[change_column_null change_column].each do |method|
88
define_method method do |*args, &block|
99
with_setting(:statement_timeout, SafePgMigrations.config.pg_safe_timeout) { super(*args, &block) }
1010
end
@@ -46,8 +46,25 @@ def add_foreign_key(from_table, to_table, **options)
4646
without_statement_timeout { validate_foreign_key from_table, options_or_to_table } unless validate_present
4747
end
4848

49+
def create_table(*)
50+
with_setting(:statement_timeout, SafePgMigrations.config.pg_safe_timeout) do
51+
super do |td|
52+
yield td if block_given?
53+
td.indexes.map! do |key, index_options|
54+
index_options[:algorithm] ||= :default
55+
[key, index_options]
56+
end
57+
end
58+
end
59+
end
60+
4961
def add_index(table_name, column_name, **options)
50-
options[:algorithm] = :concurrently
62+
if options[:algorithm] == :default
63+
options.delete :algorithm
64+
else
65+
options[:algorithm] = :concurrently
66+
end
67+
5168
SafePgMigrations.say_method_call(:add_index, table_name, column_name, options)
5269

5370
without_timeout { super }

test/create_table_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def change
4444
# Create the index.
4545
'SET statement_timeout TO 0',
4646
'SET lock_timeout TO 0',
47-
'CREATE INDEX CONCURRENTLY "index_users_on_user_id" ON "users" ("user_id")',
47+
'CREATE INDEX "index_users_on_user_id" ON "users" ("user_id")',
4848
"SET lock_timeout TO '5s'",
4949
"SET statement_timeout TO '5s'",
5050

@@ -87,7 +87,7 @@ def change
8787
"SET statement_timeout TO '5s'",
8888
'SET statement_timeout TO 0',
8989
'SET lock_timeout TO 0',
90-
'CREATE INDEX CONCURRENTLY "index_users_on_email" ON "users" ("email")',
90+
'CREATE INDEX "index_users_on_email" ON "users" ("email")',
9191
"SET lock_timeout TO '5s'",
9292
"SET statement_timeout TO '5s'",
9393
"SET statement_timeout TO '70s'",

0 commit comments

Comments
 (0)