From 665637fbadd3a3a1924934ddaaf10b09eeb460c7 Mon Sep 17 00:00:00 2001 From: Rafael dos Santos Silva Date: Fri, 26 Jul 2024 14:45:01 -0300 Subject: [PATCH] FIX: Properly fix ai_summaries table sequence (#727) * FIX: Properly fix ai_summaries table sequence Previous attempt at 3815360 could fail due to a race introduced in 1b0ba91 where summaries are migrated to core in a post_migrate erroneously. --- ...0240726164937_fix_ai_summaries_sequence.rb | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 db/migrate/20240726164937_fix_ai_summaries_sequence.rb diff --git a/db/migrate/20240726164937_fix_ai_summaries_sequence.rb b/db/migrate/20240726164937_fix_ai_summaries_sequence.rb new file mode 100644 index 00000000..918afc57 --- /dev/null +++ b/db/migrate/20240726164937_fix_ai_summaries_sequence.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +class FixAiSummariesSequence < ActiveRecord::Migration[7.0] + def up + begin + execute <<-SQL + SELECT + SETVAL ( + 'ai_summaries_id_seq', + ( + SELECT + GREATEST ( + ( + SELECT + MAX(id) + FROM + summary_sections + ), + ( + SELECT + MAX(id) + FROM + ai_summaries + ) + ) + ), + true + ); + SQL + rescue ActiveRecord::StatementInvalid => e + # if the summary_table does not exist, we can ignore the error + end + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end