From 10824c74cb9f32a315ea505a241a5193bf3bd2dc Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Tue, 24 Nov 2026 11:05:12 -0800 Subject: [PATCH] fix(db): give push-managed databases the bytewise order_key collation --- apps/sim/lib/table/rows/writes.integration.ts | 14 +++++++++++-- packages/db/schema.ts | 3 ++- .../db/scripts/apply-dev-table-triggers.ts | 20 ++++++++++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/apps/sim/lib/table/rows/writes.integration.ts b/apps/sim/lib/table/rows/writes.integration.ts index 15d293371e6..6456cd37999 100644 --- a/apps/sim/lib/table/rows/writes.integration.ts +++ b/apps/sim/lib/table/rows/writes.integration.ts @@ -2079,14 +2079,24 @@ describe('table row writes against real PostgreSQL', () => { table, 'lockless-batch' ) - // Byte order, as migration 0228 sets on the column: `db:push` leaves the default collation. const orderedNames = async (tableId: string) => ( await control<{ name: string }[]>` SELECT data->>'name' AS name FROM user_table_rows - WHERE table_id = ${tableId} ORDER BY order_key COLLATE "C", id` + WHERE table_id = ${tableId} ORDER BY order_key, id` ).map((row) => row.name) + it('orders rows by order_key in byte order, the order fractional keys are minted in', async () => { + const table = await createTable(textColumns('name')) + await seedRows(table.id, [ + { id: `${table.id}-a`, data: { name: 'a0' }, orderKey: 'a0' }, + { id: `${table.id}-z`, data: { name: 'Zz' }, orderKey: 'Zz' }, + ]) + + // Bytewise `Z` (0x5A) precedes `a` (0x61); a locale collation would put 'a0' first. + expect(await orderedNames(table.id)).toEqual(['Zz', 'a0']) + }) + it('lets every insert path commit while another transaction holds the retired row-order lock', async () => { const table = await createTable([ { id: 'name', name: 'name', type: 'string' }, diff --git a/packages/db/schema.ts b/packages/db/schema.ts index ba19876a872..45d1f771d58 100644 --- a/packages/db/schema.ts +++ b/packages/db/schema.ts @@ -8288,7 +8288,8 @@ export const userTableRows = pgTable( * * Stored with `COLLATE "C"` (migration 0228) so Postgres compares it bytewise, * matching the fractional-indexing library's ASCII ordering. drizzle can't - * express column collation, so the collation lives only in the migration. + * express column collation, so push-managed databases get it from + * `scripts/apply-dev-table-triggers.ts`. */ orderKey: text('order_key'), secretProvenanceVersion: integer('secret_provenance_version'), diff --git a/packages/db/scripts/apply-dev-table-triggers.ts b/packages/db/scripts/apply-dev-table-triggers.ts index 34b95e60032..0c062bb7272 100644 --- a/packages/db/scripts/apply-dev-table-triggers.ts +++ b/packages/db/scripts/apply-dev-table-triggers.ts @@ -7,9 +7,11 @@ import postgres from 'postgres' * table's `row_count` and `rows_version` sat at 0 forever there. This applies the CURRENT * definitions idempotently: the statement-level INSERT/DELETE row-count triggers (0224, bodies * from 0402), the deferred UPDATE trigger that bumps `rows_version` once per transaction (0390, - * body from 0402) and the deferred `table_id` foreign key (0390). Each logs to - * `user_table_row_changes`. It then reconciles the stored counts so stored + unfolded log matches - * reality. On a migrated database every statement replaces an object with its identical self. + * body from 0402), the deferred `table_id` foreign key (0390) and the bytewise `order_key` + * collation (0228), without which rows sort out of their fractional-key order. Each trigger logs + * to `user_table_row_changes`. It then reconciles the stored counts so stored + unfolded log + * matches reality. On a migrated database every statement replaces an object with its identical + * self, and the collation is left untouched. */ const logger = createLogger('DevTableTriggers') @@ -89,6 +91,18 @@ END; $$ LANGUAGE plpgsql; DROP TRIGGER IF EXISTS user_table_rows_version_update_trigger ON user_table_rows; + +-- Between the drop and the create: the trigger's WHEN reads order_key, which blocks retyping it. +-- Only when the column still has the default collation, since the ALTER rebuilds its index. +DO $$ +BEGIN + IF (SELECT collation_name FROM information_schema.columns + WHERE table_schema = current_schema() AND table_name = 'user_table_rows' + AND column_name = 'order_key') IS DISTINCT FROM 'C' THEN + ALTER TABLE user_table_rows ALTER COLUMN order_key SET DATA TYPE text COLLATE "C"; + END IF; +END $$; + CREATE CONSTRAINT TRIGGER user_table_rows_version_update_trigger AFTER UPDATE OF data, order_key ON user_table_rows DEFERRABLE INITIALLY DEFERRED