Skip to content

Commit

Permalink
disallow concurrently / add basic docs
Browse files Browse the repository at this point in the history
Co-authored-by: Ryan Krage <[email protected]>
  • Loading branch information
rkrage and Ryan Krage committed Nov 17, 2023
1 parent e668a5c commit 2cf73fe
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions lib/pg_ha_migrations/safe_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2cf73fe

Please sign in to comment.