mirror of
https://github.com/R0m1k3/Socialflow.git
synced 2026-10-18 12:46:43 +02:00
feat(db): Add automated safe migration on startup
This commit is contained in:
1 parent
60e89cb2bf
commit
4c06e5951d
6 files changed
+1283
-27
No files matched your search
@@ -11,6 +11,7 @@ import { setupVite, serveStatic, log } from "./vite";
|
||||
import { schedulerService } from "./services/scheduler";
|
||||
import { ensureAdminUserExists } from "./init-admin";
|
||||
import { startTokenCron } from "./cron";
|
||||
import { migrate } from "./migrate";
|
||||
|
||||
const app = express();
|
||||
const PgSession = connectPgSimple(session);
|
||||
@@ -147,6 +148,9 @@ app.use((req, res, next) => {
|
||||
});
|
||||
|
||||
(async () => {
|
||||
// Run database migration first
|
||||
await migrate();
|
||||
|
||||
const server = await registerRoutes(app);
|
||||
|
||||
app.use((err: any, _req: Request, res: Response, _next: NextFunction) => {
|
||||
|
||||
+66
-26
@@ -1,31 +1,71 @@
|
||||
import { Pool } from 'pg';
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { migrate } from 'drizzle-orm/node-postgres/migrator';
|
||||
import * as schema from '../shared/schema.js';
|
||||
|
||||
async function runMigrations() {
|
||||
const connectionString = process.env.DATABASE_URL;
|
||||
|
||||
if (!connectionString) {
|
||||
console.error('DATABASE_URL environment variable is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log('Running database migrations...');
|
||||
|
||||
const pool = new Pool({ connectionString });
|
||||
const db = drizzle(pool, { schema });
|
||||
import { pool } from "./db";
|
||||
|
||||
export async function migrate() {
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await migrate(db, { migrationsFolder: './drizzle' });
|
||||
console.log('Migrations completed successfully');
|
||||
await pool.end();
|
||||
process.exit(0);
|
||||
console.log("[Migration] Starting safe migration Check...");
|
||||
|
||||
// 1. Create Enums if not exist
|
||||
await client.query(`
|
||||
DO $$ BEGIN
|
||||
CREATE TYPE "token_status" AS ENUM('valid', 'expiring', 'expired', 'error');
|
||||
EXCEPTION
|
||||
WHEN duplicate_object THEN null;
|
||||
END $$;
|
||||
`);
|
||||
|
||||
// 2. Create Tables if not exist
|
||||
|
||||
// post_analytics
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS "post_analytics" (
|
||||
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"post_id" varchar NOT NULL REFERENCES "posts"("id") ON DELETE cascade,
|
||||
"fetched_at" timestamp DEFAULT now(),
|
||||
"impressions" integer DEFAULT 0,
|
||||
"reach" integer DEFAULT 0,
|
||||
"engagement" integer DEFAULT 0,
|
||||
"reactions" integer DEFAULT 0,
|
||||
"comments" integer DEFAULT 0,
|
||||
"shares" integer DEFAULT 0,
|
||||
"clicks" integer DEFAULT 0,
|
||||
"raw_data" jsonb
|
||||
);
|
||||
`);
|
||||
|
||||
// page_analytics_history
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS "page_analytics_history" (
|
||||
"id" varchar PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"page_id" varchar NOT NULL REFERENCES "social_pages"("id") ON DELETE cascade,
|
||||
"date" timestamp NOT NULL,
|
||||
"followers_count" integer DEFAULT 0,
|
||||
"page_views" integer DEFAULT 0,
|
||||
"page_reach" integer DEFAULT 0,
|
||||
"created_at" timestamp DEFAULT now()
|
||||
);
|
||||
`);
|
||||
|
||||
// 3. Add Columns to existing tables if not exist
|
||||
|
||||
// social_pages.token_status
|
||||
await client.query(`
|
||||
ALTER TABLE "social_pages"
|
||||
ADD COLUMN IF NOT EXISTS "token_status" "token_status" DEFAULT 'valid';
|
||||
`);
|
||||
|
||||
// social_pages.last_token_check
|
||||
await client.query(`
|
||||
ALTER TABLE "social_pages"
|
||||
ADD COLUMN IF NOT EXISTS "last_token_check" timestamp;
|
||||
`);
|
||||
|
||||
console.log("[Migration] Safe migration completed.");
|
||||
} catch (error) {
|
||||
console.error('Migration failed:', error);
|
||||
await pool.end();
|
||||
process.exit(1);
|
||||
console.error("[Migration] Error during migration:", error);
|
||||
// Don't kill the process, let the app try to start even if migration failed
|
||||
// (It might have failed because tables already existed in a different state)
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
runMigrations();
|
||||
Reference in new issue
Block a user