fix(db): rename collation from 'numeric' to 'natural_sort'

'numeric' is a reserved keyword and built-in type in PostgreSQL, which
can cause syntax parsing issues and ORM compatibility problems when used
as a collation name. Renamed to 'natural_sort' to be unambiguous and
self-documenting.
This commit is contained in:
yiling
2026-05-29 15:30:35 +08:00
parent ddac22a12f
commit b9e35512e5
@@ -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);
CREATE INDEX "assets_name_idx" ON "assets" ("name" COLLATE natural_sort);