Skip to content

Commit

Permalink
Support to recursively create partitioned index
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrage committed Sep 16, 2023
1 parent 5a6a55c commit 762281d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 31 deletions.
12 changes: 8 additions & 4 deletions lib/pg_ha_migrations/safe_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ def safe_add_concurrent_partitioned_index(
return if if_not_exists && parent_index.valid?

child_indexes = parent_table.partitions.map do |child_table|
raise PgHaMigrations::InvalidMigrationError, "Partitioned table #{parent_table.inspect} contains sub-partitions" if child_table.natively_partitioned?

PgHaMigrations::Index.from_table_and_columns(child_table, columns)
end

Expand All @@ -221,8 +219,14 @@ def safe_add_concurrent_partitioned_index(
end

child_indexes.each do |child_index|
# CREATE INDEX CONCURRENTLY ON child_table
safe_add_concurrent_index(
add_index_method = if child_index.table.natively_partitioned?
:safe_add_concurrent_partitioned_index
else
:safe_add_concurrent_index
end

send(
add_index_method,
child_index.table.fully_qualified_name,
columns,
name: child_index.name,
Expand Down
27 changes: 0 additions & 27 deletions spec/safe_statements_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1732,33 +1732,6 @@ def up
end.to raise_error(PgHaMigrations::InvalidMigrationError, "Table \"foos3\" is not a partitioned table")
end

it "raises error when sub-partitioning detected" do
create_range_partitioned_table(:foos3, migration_klass)
create_range_partitioned_table(:foos3_sub, migration_klass)

setup_migration = Class.new(migration_klass) do
def up
unsafe_execute(<<~SQL)
ALTER TABLE foos3
ATTACH PARTITION foos3_sub
FOR VALUES FROM ('2020-01-01') TO ('2020-02-01')
SQL
end
end

setup_migration.suppress_messages { setup_migration.migrate(:up) }

test_migration = Class.new(migration_klass) do
def up
safe_add_concurrent_partitioned_index :foos3, :updated_at
end
end

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

it "raises error when index invalid" do
create_range_partitioned_table(:foos3, migration_klass, with_partman: true)

Expand Down

0 comments on commit 762281d

Please sign in to comment.