From 0d8f9273131052dc8d9b976b4fcae52cacb0d5d4 Mon Sep 17 00:00:00 2001
From: Jeb Bearer <jeb@espressosys.com>
Date: Fri, 12 Jul 2024 16:39:32 -0400
Subject: [PATCH] Migrate to version-aware expression for generated merkle root
 columns

---
 ...6__alter_merkle_root_column_expressions.sql | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 sequencer/api/migrations/V36__alter_merkle_root_column_expressions.sql

diff --git a/sequencer/api/migrations/V36__alter_merkle_root_column_expressions.sql b/sequencer/api/migrations/V36__alter_merkle_root_column_expressions.sql
new file mode 100644
index 0000000000..ff5bcf8eea
--- /dev/null
+++ b/sequencer/api/migrations/V36__alter_merkle_root_column_expressions.sql
@@ -0,0 +1,18 @@
+-- The generated columns for header merkle roots were originally created by extracting fields
+-- `block_merkle_tree_root` and `fee_merkle_tree_root` from the header JSON. Post 0.1, though, the
+-- header serialization changed so that these fields are now nested one level deeper:
+-- `fields.block_merkle_tree_root` and `fields.fee_merkle_tree_root`. This migration alters the
+-- generated column expression to use NULL coalescing to extract the value from either of these
+-- paths depending on which version of the header we have.
+--
+-- Pre 17.x (we target Postgres >= 16.x), there is not explicit instruction for changing the
+-- expression of a generated column, so the best we can do is drop and re-add the column with a
+-- different expression.
+
+ALTER TABLE header
+    DROP column block_merkle_tree_root,
+    ADD column block_merkle_tree_root text
+        GENERATED ALWAYS AS (coalesce(data->'fields'->>'block_merkle_tree_root', data->>'block_merkle_tree_root')) STORED NOT NULL,
+    DROP column fee_merkle_tree_root,
+    ADD column fee_merkle_tree_root text
+        GENERATED ALWAYS AS (coalesce(data->'fields'->>'fee_merkle_tree_root', data->>'fee_merkle_tree_root')) STORED NOT NULL;