-- Link Shortener database schema -- Source: https://personalsoftwarefactory.com/blueprints/link-shortener (data model, section 5 of the blueprint) -- Verified on SQLite 3.53.4 and PostgreSQL 19.0 (PGlite). All timestamps are UTC. -- License: MIT. Free to copy and adapt. -- The single settings row for the short domain and the team. CREATE TABLE settings ( id INTEGER PRIMARY KEY DEFAULT 1 CHECK (id = 1), -- always 1, so there can only be one row short_domain TEXT NOT NULL, -- short links are always https://short_domain/slug main_site_url TEXT NOT NULL, -- the root redirect and the not-found page's link expired_fallback_url TEXT NOT NULL, allowed_destination_domains TEXT, -- optional comma-separated list; null or empty allows every domain (R2) time_zone TEXT NOT NULL, -- IANA name, e.g. America/New_York email_domain TEXT NOT NULL, -- the only domain team members sign in with app_name TEXT NOT NULL -- the name in email subjects and page titles ); -- A team member who can sign in. Never deleted, only deactivated. -- App rule (section 9): there is always at least one active admin; admins can't demote or deactivate themselves. CREATE TABLE users ( id TEXT PRIMARY KEY, name TEXT NOT NULL, email TEXT NOT NULL UNIQUE CHECK (email = LOWER(email)), role TEXT NOT NULL CHECK (role IN ('admin', 'member')), active BOOLEAN NOT NULL DEFAULT TRUE ); -- An allowed UTM source or medium. Can be turned on or off, never renamed or deleted. -- App rule (section 5): value uses only lowercase letters, digits and hyphens. CREATE TABLE utm_values ( id TEXT PRIMARY KEY, kind TEXT NOT NULL CHECK (kind IN ('source', 'medium')), value TEXT NOT NULL CHECK (value <> '' AND value = LOWER(value)), active BOOLEAN NOT NULL DEFAULT TRUE, -- inactive values can't be used on new links UNIQUE (kind, value) ); -- A named shortcut that fills source, medium and content on the link form. Links copy its values. CREATE TABLE presets ( id TEXT PRIMARY KEY, name TEXT NOT NULL, source TEXT NOT NULL, -- a source value, copied onto links medium TEXT NOT NULL, -- a medium value, copied onto links content TEXT -- optional utm_content ); -- A campaign; its value becomes utm_campaign. CREATE TABLE campaigns ( id TEXT PRIMARY KEY, value TEXT NOT NULL UNIQUE, -- e.g. spring-sale-2026 name TEXT NOT NULL, owner_id TEXT NOT NULL REFERENCES users(id), archived BOOLEAN NOT NULL DEFAULT FALSE -- archived campaigns are not offered for new links ); -- A short link: the slug, the destination it redirects to and the UTM values used to build it. -- App rule (R1): slug uses only lowercase letters, digits and hyphens, and is permanent once the link has a counted click or scan. -- App rule (R3, section 6): source and medium are active utm_values when set or changed; destination_url = base_url plus the UTM parameters. -- App rule (R8): a link can be deleted only when it has no clicks of any kind and no link_changes; otherwise it is archived. CREATE TABLE links ( id TEXT PRIMARY KEY, slug TEXT NOT NULL UNIQUE CHECK ( LENGTH(slug) BETWEEN 3 AND 50 AND slug = LOWER(slug) AND slug NOT IN ('admin', 'api', 'auth', 'login', 'logout', 'qr', 'static', 'assets', 'health', 'robots.txt', 'favicon.ico') ), -- the back-half (R1) title TEXT NOT NULL, base_url TEXT NOT NULL, -- stored with any utm_ parameters removed utm_source TEXT NOT NULL, utm_medium TEXT NOT NULL, campaign_id TEXT NOT NULL REFERENCES campaigns(id), -- its value is utm_campaign utm_term TEXT, -- lowercased, spaces as hyphens utm_content TEXT, -- lowercased, spaces as hyphens destination_url TEXT NOT NULL CHECK ( LENGTH(destination_url) <= 2000 AND (SUBSTR(destination_url, 1, 7) = 'http://' OR SUBSTR(destination_url, 1, 8) = 'https://') ), -- base URL with UTM parameters applied (R2) owner_id TEXT NOT NULL REFERENCES users(id), created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMP, -- optional; after it the link redirects to the expired fallback status TEXT NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'expired', 'archived')), notes TEXT ); CREATE INDEX links_owner ON links (owner_id); CREATE INDEX links_campaign ON links (campaign_id); -- One changed field in one edit of a link. Append-only (R4, R9). CREATE TABLE link_changes ( id TEXT PRIMARY KEY, link_id TEXT NOT NULL REFERENCES links(id), at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, user_id TEXT NOT NULL REFERENCES users(id), -- who made the edit field TEXT NOT NULL, -- e.g. destination_url old_value TEXT, new_value TEXT ); CREATE INDEX link_changes_link ON link_changes (link_id, at); -- One request for a short link. Append-only (R9). No IP address is ever stored (R7). -- App rule (R6): a click or scan is not counted when the same visitor had a counted click or scan on the link in the previous 30 seconds. CREATE TABLE clicks ( id TEXT PRIMARY KEY, link_id TEXT NOT NULL REFERENCES links(id), at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, kind TEXT NOT NULL CHECK (kind IN ('click', 'scan', 'preview', 'bot', 'expired')), -- scan: opened with ?qr=1 counted BOOLEAN NOT NULL, -- false for repeats; only clicks and scans can count country TEXT NOT NULL DEFAULT '' CHECK (country = '' OR (LENGTH(country) = 2 AND country = UPPER(country))), -- two letters or empty referrer_domain TEXT NOT NULL DEFAULT '', -- Referer host, lowercased, without www.; empty when none visitor_hash TEXT NOT NULL, -- SHA-256 of a daily in-memory salt + IP + user agent CHECK (counted = FALSE OR kind IN ('click', 'scan')) ); CREATE INDEX clicks_link_at ON clicks (link_id, at); CREATE INDEX clicks_repeat ON clicks (link_id, visitor_hash, at); -- One entry in the activity history. Append-only (R9). CREATE TABLE events ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, actor TEXT NOT NULL, -- a user id, or system (e.g. when a link becomes expired) type TEXT NOT NULL, data TEXT -- JSON text with the details ); -- One email in the outbox, written before it is sent. -- App rule (section 10): one expiry reminder per link per expiry date and one digest per user per week, even if two job runs overlap. CREATE TABLE notifications ( id TEXT PRIMARY KEY, at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, -- when it was queued kind TEXT NOT NULL, -- e.g. expiry reminder, weekly digest, user added, sign-in link to_email TEXT NOT NULL, subject TEXT NOT NULL, body TEXT NOT NULL, status TEXT NOT NULL DEFAULT 'queued' CHECK (status IN ('queued', 'sent', 'failed', 'logged')), -- logged: no email provider configured error TEXT );