From 2cf73fefc7977538c01b6338f16d58b8bc2fe498 Mon Sep 17 00:00:00 2001 From: Ryan Krage Date: Fri, 17 Nov 2023 20:01:22 +0000 Subject: [PATCH] disallow concurrently / add basic docs Co-authored-by: Ryan Krage --- README.md | 8 ++++++++ lib/pg_ha_migrations/safe_statements.rb | 4 ++++ spec/safe_statements_spec.rb | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/README.md b/README.md index 483af28..23437c3 100644 --- a/README.md +++ b/README.md @@ -171,6 +171,14 @@ Unsafely make a column not nullable. unsafe_make_column_not_nullable :table, :column ``` +#### safe\_add\_index\_on\_empty\_table + +Safely add an index on a table with zero rows. This will raise an error if the table contains data. + +```ruby +safe_add_index_on_empty_table :table, :column +``` + #### safe\_add\_concurrent\_index Add an index concurrently. diff --git a/lib/pg_ha_migrations/safe_statements.rb b/lib/pg_ha_migrations/safe_statements.rb index 1ea1c15..c6df80d 100644 --- a/lib/pg_ha_migrations/safe_statements.rb +++ b/lib/pg_ha_migrations/safe_statements.rb @@ -155,6 +155,10 @@ def unsafe_make_column_not_nullable(table, column, options={}) # options arg is end def safe_add_index_on_empty_table(table, columns, options={}) + if options[:algorithm] == :concurrently + raise ArgumentError, "Cannot call safe_add_index_on_empty_table with :algorithm => :concurrently" + end + # Avoids taking out an unnecessary SHARE lock if the table does have data _ensure_empty_table!(table) diff --git a/spec/safe_statements_spec.rb b/spec/safe_statements_spec.rb index fc70564..6a2fa82 100644 --- a/spec/safe_statements_spec.rb +++ b/spec/safe_statements_spec.rb @@ -1286,6 +1286,18 @@ def up ) end + it "raises error when :algorithm => :concurrently provided" do + test_migration = Class.new(migration_klass) do + def up + safe_add_index_on_empty_table :foos, [:bar], algorithm: :concurrently + end + end + + expect do + test_migration.suppress_messages { test_migration.migrate(:up) } + end.to raise_error(ArgumentError, "Cannot call safe_add_index_on_empty_table with :algorithm => :concurrently") + end + it "raises error when table contains rows" do setup_migration = Class.new(migration_klass) do def up