pg_party
pg_party copied to clipboard
The way I setup on Rails 6.1
Version: Rails 6, PostgreSQL 12
- Config setting
# config/initializers/pg_party.rb
PgParty.configure do |c|
c.create_with_primary_key = false
end
- Create table migration and run
rails db:migrate
class CreateOrdersWithTablePartitioning < ActiveRecord::Migration[6.1]
def up
create_range_partition :orders, partition_key: :created_at do |t|
t.string :product_name
t.decimal :price
t.integer :status
t.references :product
t.timestamps
end
execute "ALTER TABLE public.orders ADD PRIMARY KEY (id, created_at);"
create_range_partition_of(
:orders,
name: "orders_#{Date.today.year}_#{Date.today.month}",
start_range: Date.today.beginning_of_month,
end_range: Date.today.next_month.beginning_of_month
)
end
def down
drop_table :orders
end
end
-
Add model bypassing migration
rails g model Order --skip-migration
-
Remove the warning of "WARNING: Active Record does not support composite primary key."
# /app/models/order.rb
class Order < ApplicationRecord
self.primary_key = :id
end
Thanks for author's awesome work.