|
|
| import "dotenv/config"; |
| import { db } from "../db"; |
| import { sql } from "drizzle-orm"; |
|
|
| async function manualMigrate() { |
| console.log("Starting manual migration..."); |
| try { |
| |
| console.log("Checking 'users.role'..."); |
| await db.execute(sql` |
| ALTER TABLE "users" |
| ADD COLUMN IF NOT EXISTS "role" text DEFAULT 'user' NOT NULL; |
| `); |
| console.log("✅ 'users.role' handled."); |
|
|
| |
| console.log("Checking 'system_settings' table..."); |
| await db.execute(sql` |
| CREATE TABLE IF NOT EXISTS "system_settings" ( |
| "id" text PRIMARY KEY, |
| "feature_flags" jsonb DEFAULT '{"betaFeatures":false,"registration":true,"maintenance":false}'::jsonb NOT NULL, |
| "email_config" jsonb DEFAULT '{"dailyLimit":10000,"userRateLimit":50}'::jsonb NOT NULL, |
| "updated_at" timestamp DEFAULT now() NOT NULL |
| ); |
| `); |
| console.log("✅ 'system_settings' table handled."); |
|
|
| |
| |
| |
| console.log("Checking 'notifications.category'..."); |
|
|
| |
| const typeCol = await db.execute(sql` |
| SELECT column_name FROM information_schema.columns |
| WHERE table_name = 'notifications' AND column_name = 'type'; |
| `); |
|
|
| |
| const catCol = await db.execute(sql` |
| SELECT column_name FROM information_schema.columns |
| WHERE table_name = 'notifications' AND column_name = 'category'; |
| `); |
|
|
| if (typeCol.rows.length > 0 && catCol.rows.length === 0) { |
| console.log("Renaming 'type' to 'category' in notifications..."); |
| await db.execute(sql`ALTER TABLE "notifications" RENAME COLUMN "type" TO "category";`); |
| } else if (catCol.rows.length === 0) { |
| console.log("Adding 'category' column to notifications..."); |
| await db.execute(sql`ALTER TABLE "notifications" ADD COLUMN "category" varchar(20) DEFAULT 'system' NOT NULL;`); |
| } else { |
| console.log("✅ 'notifications.category' already exists."); |
| } |
|
|
| |
| const levelCol = await db.execute(sql` |
| SELECT column_name FROM information_schema.columns |
| WHERE table_name = 'notifications' AND column_name = 'level'; |
| `); |
|
|
| if (levelCol.rows.length === 0) { |
| console.log("Adding 'level' column to notifications..."); |
| await db.execute(sql`ALTER TABLE "notifications" ADD COLUMN IF NOT EXISTS "level" varchar(10) DEFAULT 'info' NOT NULL;`); |
| } else { |
| console.log("✅ 'notifications.level' already exists."); |
| } |
|
|
| |
| console.log("Checking WhatsApp columns in 'users' table..."); |
|
|
| await db.execute(sql` |
| ALTER TABLE "users" |
| ADD COLUMN IF NOT EXISTS "whatsapp_business_phone" text, |
| ADD COLUMN IF NOT EXISTS "whatsapp_access_token" text, |
| ADD COLUMN IF NOT EXISTS "whatsapp_verify_token" text; |
| `); |
| console.log("✅ WhatsApp columns handled."); |
|
|
| |
| console.log("Cleaning up invalid notifications..."); |
| await db.execute(sql`DELETE FROM "notifications" WHERE "user_id" IS NULL;`); |
| console.log("✅ Invalid notifications cleaned."); |
|
|
| console.log("Migration complete."); |
| process.exit(0); |
| } catch (error) { |
| console.error("Migration failed:", error); |
| process.exit(1); |
| } |
| } |
|
|
| manualMigrate(); |
|
|