pg_party icon indicating copy to clipboard operation
pg_party copied to clipboard

The way I setup on Rails 6.1

Open mfa777 opened this issue 2 years ago • 0 comments

Version: Rails 6, PostgreSQL 12

  1. Config setting
# config/initializers/pg_party.rb
PgParty.configure do |c|
  c.create_with_primary_key = false
end
  1. 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
  1. Add model bypassing migration rails g model Order --skip-migration

  2. 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.

mfa777 avatar Sep 16 '21 02:09 mfa777