diff --git a/prisma/migrations/20260529071410_add_natural_sort_collation_to_assets_name/migration.sql b/prisma/migrations/20260529071410_add_natural_sort_collation_to_assets_name/migration.sql index 1ea8bdb9..b864e728 100644 --- a/prisma/migrations/20260529071410_add_natural_sort_collation_to_assets_name/migration.sql +++ b/prisma/migrations/20260529071410_add_natural_sort_collation_to_assets_name/migration.sql @@ -1,16 +1,17 @@ --- Create ICU numeric collation for natural sort (numbers sort by value, not lexicographically) +-- Create ICU collation for natural sort (numbers sort by value, not lexicographically) +-- Named 'natural_sort' to avoid conflict with the 'numeric' reserved keyword / built-in type in PostgreSQL DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_collation WHERE collname = 'numeric') THEN - CREATE COLLATION numeric (provider = icu, locale = 'en@colNumeric=yes'); + IF NOT EXISTS (SELECT 1 FROM pg_collation WHERE collname = 'natural_sort') THEN + CREATE COLLATION natural_sort (provider = icu, locale = 'en@colNumeric=yes'); END IF; END; $$; --- Alter assets.name to use the numeric collation +-- Alter assets.name to use the natural_sort collation -- This makes all ORDER BY name queries (ORM and raw SQL) use natural sort automatically -ALTER TABLE "assets" ALTER COLUMN "name" TYPE TEXT COLLATE numeric; +ALTER TABLE "assets" ALTER COLUMN "name" TYPE TEXT COLLATE natural_sort; --- CreateIndex: B-tree index on name using the numeric collation +-- CreateIndex: B-tree index on name using the natural_sort collation -- Supports efficient ORDER BY name ASC/DESC without seq scans -CREATE INDEX "assets_name_idx" ON "assets" ("name" COLLATE numeric); \ No newline at end of file +CREATE INDEX "assets_name_idx" ON "assets" ("name" COLLATE natural_sort); \ No newline at end of file