brackt/plans/completed/phase-3-server-js-changes.md
Chris Parsons 35fe84a1dd
Update claude.md to push more tests. (#84)
* Update claude.md to push more tests.

* Archiving old plans.

* fix: run DB migrations programmatically on server startup

Replace unreliable drizzle-kit CLI migration with drizzle-orm's built-in
migrator running in the server process before accepting connections. This
ensures migrations are always applied on deploy and fails fast if they error.

- Add runMigrations() to server.ts using drizzle-orm/postgres-js/migrator
- Skip migrations in development (handled manually via db:migrate)
- Add DATABASE_URL guard with a clear error message
- Remove start:production script (now identical to start)
- Update Dockerfile CMD to use npm run start

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-07 22:31:04 -08:00

130 lines
3.3 KiB
Markdown

# Phase 3: Exact Changes to server.js
## Current server.js (lines 1-10)
```javascript
import compression from "compression";
import express from "express";
import morgan from "morgan";
// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");
const app = express();
```
## Add at top (after imports)
```javascript
import compression from "compression";
import express from "express";
import morgan from "morgan";
import { createServer } from "http"; // ← ADD THIS LINE
// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");
const app = express();
```
## Current server.js (end of file)
```javascript
app.use(await import(BUILD_PATH).then((mod) => mod.app));
}
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
## Replace with (end of file)
```javascript
app.use(await import(BUILD_PATH).then((mod) => mod.app));
}
// ← ADD THESE LINES ↓
const httpServer = createServer(app);
// Initialize Socket.IO
const { initializeSocketIO } = await import("./server/socket.js");
initializeSocketIO(httpServer);
httpServer.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// ← END OF ADDITIONS
```
## Complete Diff
```diff
import compression from "compression";
import express from "express";
import morgan from "morgan";
+import { createServer } from "http";
// Short-circuit the type-checking of the built output.
const BUILD_PATH = "./build/server/index.js";
const DEVELOPMENT = process.env.NODE_ENV === "development";
const PORT = Number.parseInt(process.env.PORT || "3000");
const app = express();
app.use(compression());
app.disable("x-powered-by");
if (DEVELOPMENT) {
console.log("Starting development server");
const viteDevServer = await import("vite").then((vite) =>
vite.createServer({
server: { middlewareMode: true },
}),
);
app.use(viteDevServer.middlewares);
app.use(async (req, res, next) => {
try {
const source = await viteDevServer.ssrLoadModule("./server/app.ts");
return await source.app(req, res, next);
} catch (error) {
if (typeof error === "object" && error instanceof Error) {
viteDevServer.ssrFixStacktrace(error);
}
next(error);
}
});
} else {
console.log("Starting production server");
app.use(
"/assets",
express.static("build/client/assets", { immutable: true, maxAge: "1y" }),
);
app.use(morgan("tiny"));
app.use(express.static("build/client", { maxAge: "1h" }));
app.use(await import(BUILD_PATH).then((mod) => mod.app));
}
-app.listen(PORT, () => {
+const httpServer = createServer(app);
+
+// Initialize Socket.IO
+const { initializeSocketIO } = await import("./server/socket.js");
+initializeSocketIO(httpServer);
+
+httpServer.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
## Summary
**Lines Added:** 5
**Lines Changed:** 1
**Total Changes:** 6 lines
This is the absolute minimum change needed to support Socket.IO while keeping the existing architecture intact.