Skip to content

Implement child partition table exclusion for schema dumper#93

Open
dannote wants to merge 3 commits intorkrage:masterfrom
dannote:master
Open

Implement child partition table exclusion for schema dumper#93
dannote wants to merge 3 commits intorkrage:masterfrom
dannote:master

Conversation

@dannote
Copy link

@dannote dannote commented Feb 28, 2025

What does this PR do?

Adds support for excluding partition child tables from db/schema.rb dumps when PgParty.config.schema_exclude_partitions is true, by overriding ActiveRecord::SchemaDumper#ignored?.

Why is this needed?

While PgParty::Hacks::PostgreSQLDatabaseTasks excludes partitions from structure.sql dumps, schema.rb currently includes them, leading to clutter and inconsistency. This PR ensures uniform exclusion behavior across both formats.

@rkrage
Copy link
Owner

rkrage commented Feb 28, 2025

I mean it is explicitly called out in the docs that if you're using this gem you should be using a structure file: https://github.com/rkrage/pg_party?tab=readme-ov-file#limitations

When I first wrote this gem, the partitioned tables absolutely were not represented correctly in the schema file. Has that maybe changed in newer versions of Rails?

@dannote
Copy link
Author

dannote commented Mar 1, 2025

Thanks for the quick response! I see in the docs that pg_party recommends structure.sql, but I’ve been using it with Rails 8.0.1 and found that schema.rb works pretty well for partitioned tables now. Here’s my migration:

create_list_partition :import_features, partition_key: :session_id do |t|
  t.references :layer, null: false, foreign_key: { to_table: :import_layers }
  t.references :session, null: false, foreign_key: { to_table: :import_sessions }
  t.bigint :source_id
  t.geometry :geometry
  t.jsonb :properties
  t.timestamps
end

And the resulting schema.rb:

create_table "import_features", id: false, options: "PARTITION BY LIST (session_id)", force: :cascade do |t|
  t.bigserial "id", null: false
  t.bigint "layer_id", null: false
  t.bigint "session_id", null: false
  t.bigint "source_id"
  t.geometry "geometry", limit: { srid: 0, type: "geometry" }
  t.jsonb "properties"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["layer_id"], name: "index_import_features_on_layer_id"
  t.index ["session_id"], name: "index_import_features_on_session_id"
end

create_table "import_features_template", id: :bigint, default: -> { "nextval('import_features_id_seq'::regclass)" }, force: :cascade do |t|
  t.bigint "layer_id", null: false
  t.bigint "session_id", null: false
  t.bigint "source_id"
  t.geometry "geometry", limit: { srid: 0, type: "geometry" }
  t.jsonb "properties"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.index ["layer_id"], name: "import_features_template_layer_id_idx"
  t.index ["session_id"], name: "import_features_template_session_id_idx"
end

The base table shows up nicely with the partitioning option, and my PR ensures child partitions don’t clutter it. It feels like Rails might have improved its handling of partitioned tables in newer versions now. Do you recall any specific issues with schema.rb from earlier versions?

If compatibility is a concern, I could add a version check like:

if Rails.version >= "8.0"
  ActiveRecord::SchemaDumper.prepend(PgParty::SchemaDumperExtension)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants