fix(admin): issue #147 — Catalog Tree city edit/delete, sort order, subcategory ops
- handleRename: city rename via /api/locations/rename-city (all rows), district via PUT /api/locations/{id}
- City-level delete: DELETE /api/locations/by-city (blocks on dependencies, 400 with counts)
- Sort order: idempotent ensureSortOrderColumns (ALTER TABLE try/catch) + PATCH /api/{type}s/{id}/sort + up/down arrows on district/category/subcategory
- Tree queries order by sort_order asc, then name
- Subcategory rename/delete verified
- tsc clean
This commit is contained in:
21
admin-next/src/lib/ensure-sort-order.ts
Normal file
21
admin-next/src/lib/ensure-sort-order.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
/**
|
||||
* Idempotently adds sort_order columns to locations, categories, and subcategories.
|
||||
* Uses raw SQL with try/catch since SQLite doesn't support IF NOT EXISTS for ALTER TABLE ADD COLUMN.
|
||||
*/
|
||||
export async function ensureSortOrderColumns(): Promise<void> {
|
||||
const migrations = [
|
||||
'ALTER TABLE locations ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
|
||||
'ALTER TABLE categories ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
|
||||
'ALTER TABLE subcategories ADD COLUMN sort_order INTEGER NOT NULL DEFAULT 0',
|
||||
];
|
||||
|
||||
for (const sql of migrations) {
|
||||
try {
|
||||
await db.$executeRawUnsafe(sql);
|
||||
} catch {
|
||||
// Column already exists — ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user