在我的 Rails 6 应用程序中,我正在创建一个我知道会变大的表。所以我使用pg_partman 按月对它进行分区。它托管在 Heroku 上,所以我按照他们的指示进行操作。迁移看起来像这样:
class CreateReceipts < ActiveRecord::Migration[6.0]
def change
reversible do |dir|
dir.up do
execute <<-SQL
create extension pg_partman;
SQL
end
dir.down do
execute <<-SQL
drop extension pg_partman;
SQL
end
end
create_table(
:receipts,
# Partitioning requires the primary key includes the column we're partitioning by.
primary_key: [:id, :created_at],
options: 'partition by range (created_at)'
) do |t|
# When using the primary key option, it ignores id: true. Make the ID column manually.
t.column :id, :bigserial, null: false
t.references :customer, null: false, foreign_key: true
t.integer :thing, null: false
t.text :stuff, null: false
t.timestamps
end
reversible do |dir|
dir.up do
execute <<-SQL
select create_parent('public.receipts', 'created_at', 'native', 'monthly');
SQL
end
dir.down do
# Dropping receipts undoes all the partitioning, except the template table.
drop_table(:template_public_receipts)
end
end
end
end
class Receipt < ApplicationRecord
# The composite primary key is only for partitioning.
self.primary_key = 'id'
# Unfortunately, partitioning gets confused if we add another unique index.
# So we must enforce ID uniqueness in the model.
validates :id, uniqueness: true
end
主键有点奇怪,但在本地可以正常工作。Heroku Postgres 有 pg_partman 扩展,所以生产很好。
问题是 HerokuCI。我正在使用推荐的 in-dyno 数据库插件。它没有pg_partman
。
-----> Preparing test database
Running: rake db:schema:load_if_ruby
db:schema:load_if_ruby completed (6.17s)
Running: rake db:structure:load_if_sql
set_config
------------
(1 row)
psql:/app/db/structure.sql:16: ERROR: could not open extension control file "/app/.indyno/vendor/postgresql/share/extension/pg_partman.control": No such file or directory
rake aborted!
failed to execute:
我宁愿不必为了这一件事将完整的数据库附加到 CI。将 pg_partman 分区硬编码到模式中感觉很奇怪,尽管最好让测试尽可能接近生产。
有替代方法吗?