From 2e15bb4cdbeff658a9e571364883cd74ee080a5a Mon Sep 17 00:00:00 2001 From: Alex Kwiatkowski Date: Wed, 24 Dec 2025 16:35:24 -0800 Subject: [PATCH] fix: use D1 migrations instead of raw schema execution - Create 0001_initial.sql with base schema (tables without interview fields) - Keep 0002_interview_fields.sql for adding new columns - Update deploy-worker.yml to use 'd1 migrations apply' - Update justfile worker-migrate to use migrations This fixes the 'no such column: lead_tier' error when deploying to a production database that was created before interview fields were added. --- .github/workflows/deploy-worker.yml | 2 +- justfile | 6 +-- workers/chat-api/migrations/0001_initial.sql | 43 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 workers/chat-api/migrations/0001_initial.sql diff --git a/.github/workflows/deploy-worker.yml b/.github/workflows/deploy-worker.yml index c300008..3730278 100644 --- a/.github/workflows/deploy-worker.yml +++ b/.github/workflows/deploy-worker.yml @@ -44,4 +44,4 @@ jobs: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} workingDirectory: workers/chat-api - command: d1 execute ${{ inputs.environment == 'staging' && 'vibes-chat-staging' || 'vibes-chat' }} --remote --config wrangler.toml --file=./schema.sql + command: d1 migrations apply ${{ inputs.environment == 'staging' && 'vibes-chat-staging' || 'vibes-chat' }} --remote --config wrangler.toml diff --git a/justfile b/justfile index 23ab392..c6a6af6 100644 --- a/justfile +++ b/justfile @@ -110,11 +110,11 @@ worker-db-create-staging: worker-migrate env: #!/usr/bin/env bash if [ "{{env}}" = "production" ]; then - cd workers/chat-api && wrangler d1 execute vibes-chat --file=./schema.sql + cd workers/chat-api && wrangler d1 migrations apply vibes-chat --remote elif [ "{{env}}" = "staging" ]; then - cd workers/chat-api && wrangler d1 execute vibes-chat-staging --file=./schema.sql + cd workers/chat-api && wrangler d1 migrations apply vibes-chat-staging --remote elif [ "{{env}}" = "local" ]; then - cd workers/chat-api && wrangler d1 execute vibes-chat --local --file=./schema.sql + cd workers/chat-api && wrangler d1 migrations apply vibes-chat --local else echo "Error: env must be 'staging', 'production', or 'local'" exit 1 diff --git a/workers/chat-api/migrations/0001_initial.sql b/workers/chat-api/migrations/0001_initial.sql new file mode 100644 index 0000000..9de8c8b --- /dev/null +++ b/workers/chat-api/migrations/0001_initial.sql @@ -0,0 +1,43 @@ +-- Sessions for rate limiting and conversation tracking +CREATE TABLE IF NOT EXISTS sessions ( + id TEXT PRIMARY KEY, + ip_hash TEXT NOT NULL, + message_count INTEGER DEFAULT 0, + created_at TEXT DEFAULT (datetime('now')), + updated_at TEXT DEFAULT (datetime('now')) +); + +-- Conversation messages +CREATE TABLE IF NOT EXISTS messages ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + session_id TEXT NOT NULL, + role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system')), + content TEXT NOT NULL, + created_at TEXT DEFAULT (datetime('now')), + FOREIGN KEY (session_id) REFERENCES sessions(id) +); + +-- Extracted lead data (original columns only) +CREATE TABLE IF NOT EXISTS leads ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + session_id TEXT NOT NULL UNIQUE, + name TEXT, + email TEXT, + company TEXT, + project_summary TEXT, + problem TEXT, + vision TEXT, + users TEXT, + capabilities TEXT, + constraints TEXT, + prd_draft TEXT, + created_at TEXT DEFAULT (datetime('now')), + FOREIGN KEY (session_id) REFERENCES sessions(id) +); + +-- Indexes for performance +CREATE INDEX IF NOT EXISTS idx_sessions_ip_hash ON sessions(ip_hash); +CREATE INDEX IF NOT EXISTS idx_sessions_created_at ON sessions(created_at); +CREATE INDEX IF NOT EXISTS idx_messages_session_id ON messages(session_id); +CREATE INDEX IF NOT EXISTS idx_leads_email ON leads(email); +CREATE INDEX IF NOT EXISTS idx_leads_created_at ON leads(created_at);