Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b787120461 | |||
| 1326725995 |
@@ -1,7 +1,26 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
# Generate diff SQL between current migrations and the Prisma schema
|
# ---------------------------------------------------------------------------
|
||||||
|
# 1. Resolve any previously failed migrations so deploy can proceed.
|
||||||
|
# Prisma marks failed migrations in _prisma_migrations; we roll them back
|
||||||
|
# so the current run can re-apply them cleanly.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
echo "[migrate] Checking for failed migrations..."
|
||||||
|
FAILED=$(bunx prisma migrate status 2>&1 || true)
|
||||||
|
|
||||||
|
# Extract failed migration names and mark them as rolled back
|
||||||
|
echo "$FAILED" | grep -oE '[0-9]{14}_[a-z_]+' | while read -r MIGRATION; do
|
||||||
|
# Only resolve if the status output says it failed
|
||||||
|
if echo "$FAILED" | grep -q "failed"; then
|
||||||
|
echo "[migrate] Resolving failed migration: $MIGRATION"
|
||||||
|
bunx prisma migrate resolve --rolled-back "$MIGRATION" 2>/dev/null || true
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 2. Generate diff SQL between current migrations and the Prisma schema.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
DIFF_SQL=$(bunx prisma migrate diff \
|
DIFF_SQL=$(bunx prisma migrate diff \
|
||||||
--from-migrations prisma/migrations \
|
--from-migrations prisma/migrations \
|
||||||
--to-schema-datamodel prisma/schema.prisma \
|
--to-schema-datamodel prisma/schema.prisma \
|
||||||
@@ -18,6 +37,8 @@ else
|
|||||||
echo "[migrate] Schema and migrations are in sync — no migration needed."
|
echo "[migrate] Schema and migrations are in sync — no migration needed."
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Deploy all pending migrations
|
# ---------------------------------------------------------------------------
|
||||||
|
# 3. Deploy all pending migrations.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
echo "[migrate] Running prisma migrate deploy..."
|
echo "[migrate] Running prisma migrate deploy..."
|
||||||
bunx prisma migrate deploy
|
bunx prisma migrate deploy
|
||||||
|
|||||||
+46
-31
@@ -21,13 +21,50 @@ import cuid from "cuid";
|
|||||||
// Setup global event debugger in non-production environments
|
// Setup global event debugger in non-production environments
|
||||||
if (Bun.env.NODE_ENV == "development") setupEventDebugger();
|
if (Bun.env.NODE_ENV == "development") setupEventDebugger();
|
||||||
|
|
||||||
// Ensure administrator role exists
|
// Helper to run a startup sync safely — failures are logged but never crash the process.
|
||||||
const existingAdmin = await prisma.role.findFirst({
|
const safeStartup = async (label: string, fn: () => Promise<void>) => {
|
||||||
where: { moniker: "administrator" },
|
try {
|
||||||
include: { users: { include: { roles: true } } },
|
await fn();
|
||||||
|
} catch (err) {
|
||||||
|
console.error(
|
||||||
|
`[startup] ${label} failed — will retry on next interval`,
|
||||||
|
err,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Start the HTTP server FIRST so the pod is reachable immediately.
|
||||||
|
// All data-sync tasks run afterwards and are non-blocking.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
Bun.serve({
|
||||||
|
port: PORT,
|
||||||
|
websocket: engine.handler().websocket,
|
||||||
|
fetch: (req, server) => {
|
||||||
|
const url = new URL(req.url);
|
||||||
|
|
||||||
|
if (url.pathname.startsWith("/socket.io/")) {
|
||||||
|
return engine.handleRequest(req, server as any);
|
||||||
|
}
|
||||||
|
|
||||||
|
return app.fetch(req, server);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!existingAdmin) {
|
console.log(`[startup] Server listening on port ${PORT}`);
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Background initialisation — none of this blocks the server.
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// Ensure administrator role exists
|
||||||
|
await safeStartup("ensureAdminRole", async () => {
|
||||||
|
const existingAdmin = await prisma.role.findFirst({
|
||||||
|
where: { moniker: "administrator" },
|
||||||
|
include: { users: { include: { roles: true } } },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!existingAdmin) {
|
||||||
const id = cuid();
|
const id = cuid();
|
||||||
const created = await prisma.role.create({
|
const created = await prisma.role.create({
|
||||||
data: {
|
data: {
|
||||||
@@ -43,16 +80,8 @@ if (!existingAdmin) {
|
|||||||
include: { users: { include: { roles: true } } },
|
include: { users: { include: { roles: true } } },
|
||||||
});
|
});
|
||||||
events.emit("role:created", new RoleController(created));
|
events.emit("role:created", new RoleController(created));
|
||||||
}
|
|
||||||
|
|
||||||
// Helper to run a startup sync safely — failures are logged but never crash the process.
|
|
||||||
const safeStartup = async (label: string, fn: () => Promise<void>) => {
|
|
||||||
try {
|
|
||||||
await fn();
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`[startup] ${label} failed — will retry on next interval`, err);
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
|
|
||||||
// Refresh the internal list of companies every minute
|
// Refresh the internal list of companies every minute
|
||||||
await safeStartup("refreshCompanies", refreshCompanies);
|
await safeStartup("refreshCompanies", refreshCompanies);
|
||||||
@@ -91,21 +120,7 @@ setInterval(() => {
|
|||||||
|
|
||||||
await safeStartup("syncSites", () => unifiSites.syncSites());
|
await safeStartup("syncSites", () => unifiSites.syncSites());
|
||||||
setInterval(() => {
|
setInterval(() => {
|
||||||
return unifiSites.syncSites().catch((err) =>
|
return unifiSites
|
||||||
console.error("[interval] syncSites failed", err),
|
.syncSites()
|
||||||
);
|
.catch((err) => console.error("[interval] syncSites failed", err));
|
||||||
}, 60 * 1000);
|
}, 60 * 1000);
|
||||||
|
|
||||||
Bun.serve({
|
|
||||||
port: PORT,
|
|
||||||
websocket: engine.handler().websocket,
|
|
||||||
fetch: (req, server) => {
|
|
||||||
const url = new URL(req.url);
|
|
||||||
|
|
||||||
if (url.pathname.startsWith("/socket.io/")) {
|
|
||||||
return engine.handleRequest(req, server as any);
|
|
||||||
}
|
|
||||||
|
|
||||||
return app.fetch(req, server);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|||||||
Reference in New Issue
Block a user