-- pages.elijah.run — incremental migration: page aliases
--
-- pages-9lsk: adds removable, long-lived **aliases** — extra names a page can
-- be reached at, in the same URL namespace as `pages.slug`. Serving is
-- serve-direct (an alias request serves the canonical page's R2 content with
-- no redirect); namespace uniqueness across `pages.slug` and `aliases.alias`
-- is enforced in the route handlers (src/routes/api.rs). See design §4.1.
--
-- Unlike 0002-trust.sql (an in-place `ALTER TABLE`), this adds a brand-new
-- table, so every statement is `IF NOT EXISTS` and the file is fully
-- idempotent — re-running it against a database that already has the table is
-- a safe no-op. `migrations/schema.sql` already contains the same definitions
-- for fresh installs; this file is the isolated change for the *existing*
-- production D1 database.
--
-- Apply with (a separate, human-triggered step):
--   wrangler d1 execute <DB_NAME> --file=migrations/0003-aliases.sql          (remote)
--   wrangler d1 execute <DB_NAME> --local --file=migrations/0003-aliases.sql  (local dev)

CREATE TABLE IF NOT EXISTS aliases (
  alias      TEXT PRIMARY KEY,
  slug       TEXT    NOT NULL REFERENCES pages(slug),
  owner_id   INTEGER NOT NULL REFERENCES users(id),
  created_at TEXT    NOT NULL DEFAULT (datetime('now'))
);

CREATE INDEX IF NOT EXISTS idx_aliases_slug ON aliases(slug);
