@@ -7,9 +7,11 @@ import postgres from 'postgres'
77 * table's `row_count` and `rows_version` sat at 0 forever there. This applies the CURRENT
88 * definitions idempotently: the statement-level INSERT/DELETE row-count triggers (0224, bodies
99 * from 0402), the deferred UPDATE trigger that bumps `rows_version` once per transaction (0390,
10- * body from 0402) and the deferred `table_id` foreign key (0390). Each logs to
11- * `user_table_row_changes`. It then reconciles the stored counts so stored + unfolded log matches
12- * reality. On a migrated database every statement replaces an object with its identical self.
10+ * body from 0402), the deferred `table_id` foreign key (0390) and the bytewise `order_key`
11+ * collation (0228), without which rows sort out of their fractional-key order. Each trigger logs
12+ * to `user_table_row_changes`. It then reconciles the stored counts so stored + unfolded log
13+ * matches reality. On a migrated database every statement replaces an object with its identical
14+ * self, and the collation is left untouched.
1315 */
1416const logger = createLogger ( 'DevTableTriggers' )
1517
8991$$ LANGUAGE plpgsql;
9092
9193DROP TRIGGER IF EXISTS user_table_rows_version_update_trigger ON user_table_rows;
94+
95+ -- Between the drop and the create: the trigger's WHEN reads order_key, which blocks retyping it.
96+ -- Only when the column still has the default collation, since the ALTER rebuilds its index.
97+ DO $$
98+ BEGIN
99+ IF (SELECT collation_name FROM information_schema.columns
100+ WHERE table_schema = current_schema() AND table_name = 'user_table_rows'
101+ AND column_name = 'order_key') IS DISTINCT FROM 'C' THEN
102+ ALTER TABLE user_table_rows ALTER COLUMN order_key SET DATA TYPE text COLLATE "C";
103+ END IF;
104+ END $$;
105+
92106CREATE CONSTRAINT TRIGGER user_table_rows_version_update_trigger
93107 AFTER UPDATE OF data, order_key ON user_table_rows
94108 DEFERRABLE INITIALLY DEFERRED
0 commit comments