Question: schema_exclude_partitions = false development workflow
tjwallace opened this issue ยท 7 comments
If schema_exclude_partitions = false
, how do ensure there is a writable partition in development/test environments?
So I'm guessing that you have some time-based partitions being dumped in your structure.sql
and you're using db:structure:load
to recreate the database before running tests? Maybe create some partitions relative to the current time in db/seeds.rb
(or some other script) prior to running the test suite? Depending on your situation, timecop might be helpful as well.
Yes exactly: the partition is based on created_at::date
. I ended up adding a create_default_partitions
method on the model that is called in db/seeds.rb
and in an RSpec before hook. Seems to be working so far.
Time-based partitioning is hard and since I haven't yet had to deal with it in a production environment, I can't offer a ton of advice. In general, it doesn't seem like a great idea to add these types of partitions in a migration. Would be pretty annoying to add a new migration every few months to make sure the future partitions are there. If you're mostly utilizing the model query helpers, maybe it makes sense to introduce pg_partman as well.
As you play around with the library, I'd really appreciate any feedback to make things more intuitive / user-friendly.
Will do @rkrage.
One thing I noticed was I had to add self.primary_key = :id
to my model class. Don't think I saw that mentioned in the docs (maybe it's not expected?).
If partitions have already been created for that particular model, that shouldn't be necessary. PgParty pulls the primary key from the first child partition table it detects: https://github.com/rkrage/pg_party/blob/master/lib/pg_party/model/shared_methods.rb#L11-L12
Do your child partitions have PKs?
They do. And I tried removing that line and don't have an issue anymore... I'll investigate more. Thanks.
Closing this issue for now, but feel free to reach out if you have any other questions!