From ed5e213b1a2520b0242d2e47462d147e83035cb0 Mon Sep 17 00:00:00 2001 From: M1 Date: Tue, 17 Mar 2026 07:17:54 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20migrate()=20uses=20CREATE=20IF=20NOT=20E?= =?UTF-8?q?XISTS=20=E2=80=94=20no=20more=20data=20wipe=20on=20restart?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/web/src/db.ts | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/apps/web/src/db.ts b/apps/web/src/db.ts index 539d588..a1d8342 100644 --- a/apps/web/src/db.ts +++ b/apps/web/src/db.ts @@ -4,15 +4,9 @@ const sql = postgres(process.env.DATABASE_URL ?? "postgres://pingql:pingql@local export default sql; -// Run migrations on startup — full rebuild (no real users) export async function migrate() { - await sql`DROP TABLE IF EXISTS pings CASCADE`; - await sql`DROP TABLE IF EXISTS api_keys CASCADE`; - await sql`DROP TABLE IF EXISTS monitors CASCADE`; - await sql`DROP TABLE IF EXISTS accounts CASCADE`; - await sql` - CREATE TABLE accounts ( + CREATE TABLE IF NOT EXISTS accounts ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), key TEXT NOT NULL UNIQUE, email_hash TEXT, @@ -21,7 +15,7 @@ export async function migrate() { `; await sql` - CREATE TABLE monitors ( + CREATE TABLE IF NOT EXISTS monitors ( id TEXT PRIMARY KEY DEFAULT gen_random_uuid()::text, account_id UUID NOT NULL REFERENCES accounts(id) ON DELETE CASCADE, name TEXT NOT NULL, @@ -38,7 +32,7 @@ export async function migrate() { `; await sql` - CREATE TABLE pings ( + CREATE TABLE IF NOT EXISTS pings ( id BIGSERIAL PRIMARY KEY, monitor_id TEXT NOT NULL REFERENCES monitors(id) ON DELETE CASCADE, checked_at TIMESTAMPTZ NOT NULL DEFAULT now(), @@ -54,7 +48,7 @@ export async function migrate() { await sql`CREATE INDEX IF NOT EXISTS idx_pings_checked_at ON pings(checked_at)`; await sql` - CREATE TABLE api_keys ( + CREATE TABLE IF NOT EXISTS api_keys ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), key TEXT NOT NULL UNIQUE, account_id UUID NOT NULL REFERENCES accounts(id) ON DELETE CASCADE,