Skip to content

Commit 10824c7

Browse files
committed
fix(db): give push-managed databases the bytewise order_key collation
1 parent dc7c60f commit 10824c7

3 files changed

Lines changed: 31 additions & 6 deletions

File tree

‎apps/sim/lib/table/rows/writes.integration.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,14 +2079,24 @@ describe('table row writes against real PostgreSQL', () => {
20792079
table,
20802080
'lockless-batch'
20812081
)
2082-
// Byte order, as migration 0228 sets on the column: `db:push` leaves the default collation.
20832082
const orderedNames = async (tableId: string) =>
20842083
(
20852084
await control<{ name: string }[]>`
20862085
SELECT data->>'name' AS name FROM user_table_rows
2087-
WHERE table_id = ${tableId} ORDER BY order_key COLLATE "C", id`
2086+
WHERE table_id = ${tableId} ORDER BY order_key, id`
20882087
).map((row) => row.name)
20892088

2089+
it('orders rows by order_key in byte order, the order fractional keys are minted in', async () => {
2090+
const table = await createTable(textColumns('name'))
2091+
await seedRows(table.id, [
2092+
{ id: `${table.id}-a`, data: { name: 'a0' }, orderKey: 'a0' },
2093+
{ id: `${table.id}-z`, data: { name: 'Zz' }, orderKey: 'Zz' },
2094+
])
2095+
2096+
// Bytewise `Z` (0x5A) precedes `a` (0x61); a locale collation would put 'a0' first.
2097+
expect(await orderedNames(table.id)).toEqual(['Zz', 'a0'])
2098+
})
2099+
20902100
it('lets every insert path commit while another transaction holds the retired row-order lock', async () => {
20912101
const table = await createTable([
20922102
{ id: 'name', name: 'name', type: 'string' },

‎packages/db/schema.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8288,7 +8288,8 @@ export const userTableRows = pgTable(
82888288
*
82898289
* Stored with `COLLATE "C"` (migration 0228) so Postgres compares it bytewise,
82908290
* matching the fractional-indexing library's ASCII ordering. drizzle can't
8291-
* express column collation, so the collation lives only in the migration.
8291+
* express column collation, so push-managed databases get it from
8292+
* `scripts/apply-dev-table-triggers.ts`.
82928293
*/
82938294
orderKey: text('order_key'),
82948295
secretProvenanceVersion: integer('secret_provenance_version'),

‎packages/db/scripts/apply-dev-table-triggers.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
1416
const logger = createLogger('DevTableTriggers')
1517

@@ -89,6 +91,18 @@ END;
8991
$$ LANGUAGE plpgsql;
9092
9193
DROP 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+
92106
CREATE 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

Comments
 (0)