Skip to content

Commit 5c68b66

Browse files
committed
fix(docker): 🐛 move migrations to application layer
1 parent d091b79 commit 5c68b66

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.react-router
22
build
33
node_modules
4-
README.md
4+
README.md
5+
.env

Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ FROM dependencies-env
2121
COPY ./package.json bun.lock /arcarchiver/
2222
COPY --from=production-dependencies-env /arcarchiver/node_modules /arcarchiver/node_modules
2323
COPY --from=build-env /arcarchiver/build /arcarchiver/build
24-
WORKDIR /app
25-
CMD ["bun", "run", "start"]
24+
WORKDIR /arcarchiver
25+
CMD ["bun", "run", "start-docker"]

docker-compose.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ services:
1111
POSTGRES_DB: arcarchiver
1212
volumes:
1313
- postgres_data:/var/lib/postgresql/data
14-
- ./drizzle:/docker-entrypoint-initdb.d
1514
ports:
1615
- "5432:5432"
1716
healthcheck:
@@ -30,13 +29,10 @@ services:
3029
NODE_ENV: production
3130
DATABASE_URL: postgresql://arcarchiver:arcarchiver_password@postgres:5432/arcarchiver
3231
ports:
33-
- "3000:3000"
32+
- "3001:3000"
3433
depends_on:
3534
postgres:
3635
condition: service_healthy
37-
volumes:
38-
# [AI] Mount logs directory for easier debugging if needed
39-
- ./logs:/app/logs
4036
healthcheck:
4137
test: ["CMD-SHELL", "curl -f http://localhost:3000 || exit 1"]
4238
interval: 30s

drizzle/migrate.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// .drizzle/migrate.ts:
2+
import { drizzle } from "drizzle-orm/postgres-js";
3+
import { migrate } from "drizzle-orm/postgres-js/migrator";
4+
import postgres from "postgres";
5+
6+
async function runMigration() {
7+
console.log("Migration started ⌛");
8+
9+
// Not using the getDbUrl helper function because we aren't copying that into our runtime app prior to deployment in our Dockerfile. We'll live with the code duplication.
10+
const dbUrl = (
11+
process.env.NODE_ENV === "production"
12+
? process.env.DATABASE_URL
13+
: process.env.DEV_DATABASE_URL
14+
) as string;
15+
16+
if (!dbUrl) throw new Error("No database url found");
17+
18+
const client = postgres(dbUrl, {
19+
max: 1,
20+
// SSL must be `require`. `true` or `verify-full` do not work since Railway uses self-signed certificates.
21+
ssl: process.env.NODE_ENV === "production" ? "require" : undefined,
22+
});
23+
24+
const db = drizzle(client);
25+
try {
26+
await migrate(db, { migrationsFolder: "./.drizzle/migrations" });
27+
console.log("Migration completed ✅");
28+
} catch (error) {
29+
console.error("Migration failed 🚨:", error);
30+
} finally {
31+
await client.end();
32+
}
33+
}
34+
35+
runMigration().catch((error) =>
36+
console.error("Error in migration process 🚨:", error),
37+
);

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
"build": "cross-env NODE_ENV=production react-router build",
1717
"dev": "bunx --bun vite",
1818
"start": "cross-env NODE_ENV=production bun ./build/server/index.js",
19+
"start:docker": "bun run db:migrateprod && cross-env NODE_ENV=production bun ./build/server/index.js",
20+
"db:migrateprod": "NODE_ENV=production bun .drizzle/migrate.ts",
1921
"test": "bun test",
2022
"test:watch": "bun test --watch",
2123
"test:coverage": "bun test --coverage",

0 commit comments

Comments
 (0)