Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrage committed Sep 7, 2023
1 parent 1ac387c commit 8fe3505
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
43 changes: 40 additions & 3 deletions lib/pg_ha_migrations/safe_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ def safe_add_concurrent_partitioned_index(
using: nil,
unique: nil,
where: nil,
comment: nil
comment: nil,
recurse: nil
)
raise ArgumentError, "Expected <name_suffix> to be present" unless name_suffix.present?

Expand All @@ -200,16 +201,47 @@ def safe_add_concurrent_partitioned_index(

_validate_index_name!(parent_index)

# Validate index names upfront to avoid unnecessary recursion
child_tables_with_metadata = _partitions_for_table(parent_schema, parent_table).map do |child_schema, child_table|
raise PgHaMigrations::InvalidMigrationError, "Partitioned table #{parent_table.inspect} contains sub-partitions" if _partitioned_table?(child_schema, child_table)

child_index = "#{child_table}_#{name_suffix}"

_validate_index_name!(child_index)

[child_schema, child_table, child_index]
end

# When sub-partitioning detected:
# - recursively create indexes when recurse is true
# - do nothing when if_not_exists is true and index already valid
# - otherwise raise helpful error
child_tables_with_metadata.each do |child_schema, child_table, child_index|
next unless _partitioned_table?(child_schema, child_table)

if recurse
safe_add_concurrent_partitioned_index(
"#{connection.quote_schema_name(child_schema)}.#{child_table}",
columns,
name_suffix: name_suffix,
if_not_exists: if_not_exists,
recurse: recurse,
using: using,
unique: unique,
where: where,
)
elsif !if_not_exists
message = "Partitioned table #{parent_table.inspect} contains sub-partitions. " \
"If sub-partition indexes have already been created, execute this method with 'if_not_exists: true'. " \
"If sub-partition indexes have not been created, consider executing this method with 'recurse: true'."

raise PgHaMigrations::InvalidMigrationError, message
elsif !_index_valid?(child_schema, child_index)
message = "Sub-partition #{child_table.inspect} does not have valid index #{child_index.inspect}. " \
"Consider executing this method with 'recurse: true'."

raise PgHaMigrations::InvalidMigrationError, message
end
end

# TODO: take out ShareLock after issue #39 is implemented
safely_acquire_lock_for_table(fully_qualified_parent_table) do
# CREATE INDEX ON ONLY parent_table
Expand All @@ -227,6 +259,11 @@ def safe_add_concurrent_partitioned_index(
end

child_tables_with_metadata.each do |child_schema, child_table, child_index|
# If we get past all of the validation above, we can safely assume that
# indexes on sub-partitions have already been created recursviely or in a
# separate, explicit call to safe_add_concurrent_partitioned_index
next if _partitioned_table?(child_schema, child_table)

# CREATE INDEX CONCURRENTLY ON child_table
safe_add_concurrent_index(
"#{connection.quote_schema_name(child_schema)}.#{child_table}",
Expand Down
2 changes: 1 addition & 1 deletion spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,7 @@ def up

expect do
test_migration.suppress_messages { test_migration.migrate(:up) }
end.to raise_error(PgHaMigrations::InvalidMigrationError, "Partitioned table \"foos3\" contains sub-partitions")
end.to raise_error(PgHaMigrations::InvalidMigrationError, /Partitioned table "foos3" contains sub-partitions/)
end

it "raises error when index invalid" do
Expand Down

0 comments on commit 8fe3505

Please sign in to comment.