Skip to content

Commit

Permalink
Set collation order at field level rather than on query
Browse files Browse the repository at this point in the history
  • Loading branch information
kalilsn committed Feb 26, 2025
1 parent f3b06bf commit c021a34
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
3 changes: 1 addition & 2 deletions core/lib/server/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export const getForm = (
"pub_fields.isRelation",
])
.$narrowType<FormElements>()
// Use "C" collation order to ensure uppercase letters sort before lowercase to match mudder
.orderBy(sql`"rank" collate "C"`)
.orderBy("rank")
).as("elements")
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
BEGIN;
CREATE TEMP TABLE "mudder_ranks" (
index SERIAL PRIMARY KEY,
rank TEXT,
rank TEXT
);
/*
* This temp table holds 200 mudder generated keys which we use to assign initial ranks to existing
* form elements and related pubs in the migration.
* Generated with: mudder.base62.mudder(200).map((rank) => `('${rank}')`).join(", ")
*/
INSERT INTO "mudder_ranks"
INSERT INTO "mudder_ranks"("rank")
VALUES ('0J'), ('0c'), ('0v'), ('1'), ('1X'), ('1q'), ('2'), ('2S'), ('2m'), ('3'), ('3O'), ('3h'),
('4'), ('4J'), ('4c'), ('4v'), ('5'), ('5Y'), ('5r'), ('6'), ('6T'), ('6m'), ('7'), ('7O'), ('7i'),
('8'), ('8K'), ('8d'), ('8w'), ('9'), ('9Y'), ('9r'), ('A'), ('AU'), ('An'), ('B'), ('BP'), ('Bi'),
Expand All @@ -27,7 +27,8 @@ BEGIN;
('y'), ('yS'), ('yl'), ('z'), ('zN'), ('zg');

-- Add rank to form_elements
ALTER TABLE "form_elements" ADD COLUMN "rank" TEXT;
-- Uses "C" collation order to ensure uppercase letters sort before lowercase to match mudder
ALTER TABLE "form_elements" ADD COLUMN "rank" TEXT COLLATE "C";

-- Set initial rank values for form elements based on 'order'
UPDATE "form_elements"
Expand Down Expand Up @@ -56,7 +57,7 @@ BEGIN;

-- Add rank to pub_values
-- This one is nullable for now
ALTER TABLE "pub_values" ADD COLUMN "rank" TEXT;
ALTER TABLE "pub_values" ADD COLUMN "rank" TEXT COLLATE "C";

-- Get all pub_values with multiple related pubs, then assign initial ranks ordered by updatedAt
WITH "related_pubs" AS (
Expand Down
4 changes: 2 additions & 2 deletions core/prisma/schema/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ model PubValue {
// Rank is only used for ordering multiple related pubs for a single field, not for ordering
// values within a pub
rank String?
rank String? // Uses "C" collation
@@map(name: "pub_values")
}
Expand Down Expand Up @@ -536,7 +536,7 @@ model FormElement {
field PubField? @relation(fields: [fieldId], references: [id], onDelete: Cascade)
formId String
order Int?
rank String
rank String // Uses "C" collation
// label is only used by elements with type: ElementType.button. Pubfield inputs put everything in config
label String?
element StructuralFormElement?
Expand Down

0 comments on commit c021a34

Please sign in to comment.