Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions apps/sim/lib/table/rows/writes.integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
3 changes: 2 additions & 1 deletion packages/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
20 changes: 17 additions & 3 deletions packages/db/scripts/apply-dev-table-triggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down
Loading