fix(share): keep exactly one custom address per page on alias edit (#226)
Editing an existing share alias (e.g. slug `te` -> `ted`) failed to update the displayed `/l/<alias>` link: `setAlias()` looked the requested slug up by name and, if free, INSERTed a brand-new row, leaving the page with multiple alias rows. The modal then read via `findByPageId().executeTakeFirst()` with no `ORDER BY`, so Postgres returned an arbitrary (in practice the oldest, stale) row. Every edit also spawned an orphan row that kept a live `/l/<old>` link forever. Regression of #205. Enforce the invariant "a page has EXACTLY ONE custom address": - `setAlias()` now resolves the page's current alias row and RENAMES it in place when the requested name is free (insert only when the page has none), keeps the same-name no-op and the cross-page 409 `ALIAS_REASSIGN_REQUIRED` + confirmed-retarget flow, and after any successful write DELETEs all other alias rows for the page (self-heal). Runs in one transaction so the page is never transiently empty or duplicated. - repo: add `updateAlias` (rename) and `deleteOthersForPage`; make `findByPageId` deterministic with `ORDER BY created_at DESC, id DESC`. - migration: dedup existing rows (keep newest per page) + a PARTIAL unique index `(workspace_id, page_id) WHERE page_id IS NOT NULL` so dangling aliases still coexist while live ones are one-per-page. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import { type Kysely, sql } from 'kysely';
|
||||
|
||||
/**
|
||||
* Enforce "a page has EXACTLY ONE custom address" at the DB level. The original
|
||||
* `share_aliases` table only had a unique index on `(workspace_id, alias)`, so a
|
||||
* page could accumulate several alias rows (every slug edit used to INSERT a new
|
||||
* one), leaving orphan `/l/<old>` links live forever and making the share
|
||||
* modal's `findByPageId` lookup nondeterministic.
|
||||
*
|
||||
* We first dedup any pre-existing rows (keeping the NEWEST per page — the same
|
||||
* "current" choice the read path now makes), then add a PARTIAL unique index on
|
||||
* `(workspace_id, page_id)`. It is partial (`WHERE page_id IS NOT NULL`) so that
|
||||
* multiple DANGLING aliases (target page deleted -> `page_id` SET NULL) can
|
||||
* still coexist without colliding.
|
||||
*/
|
||||
export async function up(db: Kysely<any>): Promise<void> {
|
||||
// Reap legacy duplicates: for each (workspace_id, page_id) keep only the row
|
||||
// with the greatest (created_at, id) — matches ShareAliasRepo.findByPageId.
|
||||
await sql`
|
||||
DELETE FROM share_aliases sa
|
||||
USING share_aliases keep
|
||||
WHERE sa.page_id IS NOT NULL
|
||||
AND sa.workspace_id = keep.workspace_id
|
||||
AND sa.page_id = keep.page_id
|
||||
AND (keep.created_at, keep.id) > (sa.created_at, sa.id)
|
||||
`.execute(db);
|
||||
|
||||
await db.schema
|
||||
.createIndex('share_aliases_workspace_id_page_id_unique')
|
||||
.on('share_aliases')
|
||||
.columns(['workspace_id', 'page_id'])
|
||||
.unique()
|
||||
.where('page_id', 'is not', null)
|
||||
.execute();
|
||||
}
|
||||
|
||||
export async function down(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.dropIndex('share_aliases_workspace_id_page_id_unique')
|
||||
.execute();
|
||||
}
|
||||
Reference in New Issue
Block a user