Prepare application for production deployment with Docker and PostgreSQL

Integrate PostgreSQL database with Drizzle ORM, implement database migrations, and enhance Dockerfile for production build.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ceced2fc-aa46-458d-ba87-ddd4b7bb1518
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/659922a9-0087-461c-90dd-6d9a58b81d4d/ceced2fc-aa46-458d-ba87-ddd4b7bb1518/ahHWEFX
This commit is contained in:
paul-nothaft
2025-10-23 13:31:02 +00:00
parent 4133052165
commit 784968521a
6 changed files with 211 additions and 114 deletions
+73 -4
View File
@@ -1,8 +1,11 @@
import { drizzle } from 'drizzle-orm/neon-http';
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
import { migrate } from 'drizzle-orm/node-postgres/migrator';
import * as schema from '@shared/schema';
import { sql } from 'drizzle-orm';
let db: ReturnType<typeof drizzle> | null = null;
let pool: Pool | null = null;
export function getDatabase() {
if (!db) {
@@ -12,15 +15,72 @@ export function getDatabase() {
throw new Error('DATABASE_URL environment variable is not set');
}
const sql = neon(databaseUrl);
db = drizzle(sql, { schema });
pool = new Pool({
connectionString: databaseUrl,
});
db = drizzle(pool, { schema });
}
return db;
}
export async function runMigrations() {
try {
const database = getDatabase();
// Push schema to database (creates/updates tables as needed)
// This is equivalent to running `drizzle-kit push`
console.log('Checking database schema...');
// Enable pgcrypto extension for gen_random_uuid()
await database.execute(sql`CREATE EXTENSION IF NOT EXISTS "pgcrypto"`);
// Create tables if they don't exist
await database.execute(sql`
CREATE TABLE IF NOT EXISTS users (
id VARCHAR PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
)
`);
await database.execute(sql`
CREATE TABLE IF NOT EXISTS labels (
id VARCHAR PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
color TEXT NOT NULL
)
`);
await database.execute(sql`
CREATE TABLE IF NOT EXISTS tasks (
id VARCHAR PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
description TEXT,
status TEXT NOT NULL DEFAULT 'todo',
priority TEXT NOT NULL DEFAULT 'medium',
due_date TIMESTAMP,
time_tracked INTEGER NOT NULL DEFAULT 0,
is_tracking BOOLEAN NOT NULL DEFAULT false,
project_id TEXT,
notes TEXT,
label_id VARCHAR REFERENCES labels(id)
)
`);
console.log('✓ Database schema is up to date');
} catch (error) {
console.error('Failed to run migrations:', error);
throw error;
}
}
export async function initializeDatabase() {
try {
// Run migrations first
await runMigrations();
const database = getDatabase();
// Create default labels if they don't exist
@@ -44,3 +104,12 @@ export async function initializeDatabase() {
throw error;
}
}
export async function closeDatabase() {
if (pool) {
await pool.end();
pool = null;
db = null;
console.log('✓ Database connection closed');
}
}
+20 -1
View File
@@ -1,7 +1,7 @@
import express, { type Request, Response, NextFunction } from "express";
import { registerRoutes } from "./routes";
import { setupVite, serveStatic, log } from "./vite";
import { initializeDatabase } from "./db";
import { initializeDatabase, closeDatabase } from "./db";
const app = express();
app.use(express.json());
@@ -79,4 +79,23 @@ app.use((req, res, next) => {
}, () => {
log(`serving on port ${port}`);
});
// Graceful shutdown
const gracefulShutdown = async (signal: string) => {
log(`${signal} received, closing server gracefully...`);
server.close(async () => {
log('HTTP server closed');
await closeDatabase();
process.exit(0);
});
// Force close after 30 seconds
setTimeout(() => {
console.error('Forced shutdown after timeout');
process.exit(1);
}, 30000);
};
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
})();