Seed FuをPostgreSQL 10 対応する

当面は、https://github.com/ppworks/seed-fu/tree/support-postgresql10 を使う。

https://www.postgresql.org/docs/10/static/release-10.html
Move sequences’ metadata fields into a new pg_sequence system catalog (Peter Eisentraut)

A sequence relation now stores only the fields that can be modified by nextval(), that is last_value, log_cnt, and is_called. Other sequence properties, such as the starting value and increment, are kept in a corresponding row of the pg_sequence catalog. ALTER SEQUENCE updates are now fully transactional, implying that the sequence is locked until commit. The nextval() and setval() functions remain nontransactional.

The main incompatibility introduced by this change is that selecting from a sequence relation now returns only the three fields named above. To obtain the sequence’s other properties, applications must look into pg_sequence. The new system view pg_sequences can also be used for this purpose; it provides column names that are more compatible with existing code.

The output of psql’s \d command for a sequence has been redesigned, too.

とのことで、いままで PostgreSQLID Sequenceを設定していた、こういうコードが落ちるようになった。

1
SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)

エラーはこんな感じ

1
2
3
4
Failure/Error: SeedFu.seed

ActiveRecord::StatementInvalid:
PG::UndefinedColumn: ERROR: column "increment_by" does not exist

以下のように、新たに導入されたpg_sequenceカタログを参照するように変更した

1
SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT seqincrement FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass), (SELECT seqmin FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass)) FROM #{@model_class.quoted_table_name}),

ちなみにRailsの類似処理は既にマージされていた。

https://github.com/rails/rails/pull/28864