mirror of
https://github.com/fosrl/pangolin.git
synced 2025-05-19 08:37:48 +01:00
run migrations if no db
This commit is contained in:
parent
25224e0343
commit
2d5ff17515
4 changed files with 28 additions and 3 deletions
|
@ -8,6 +8,9 @@ RUN npm install --legacy-peer-deps
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
RUN npx drizzle-kit generate --dialect sqlite --schema ./server/db/schema.ts --out migrations
|
||||||
|
RUN ls migrations
|
||||||
|
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
FROM node:20-alpine AS runner
|
FROM node:20-alpine AS runner
|
||||||
|
@ -22,6 +25,7 @@ RUN npm install --omit=dev --legacy-peer-deps
|
||||||
|
|
||||||
COPY --from=builder /app/.next ./.next
|
COPY --from=builder /app/.next ./.next
|
||||||
COPY --from=builder /app/dist ./dist
|
COPY --from=builder /app/dist ./dist
|
||||||
|
COPY --from=builder /app/migrations ./dist/migrations
|
||||||
|
|
||||||
COPY config.example.yml ./dist/config.example.yml
|
COPY config.example.yml ./dist/config.example.yml
|
||||||
COPY server/db/names.json ./dist/names.json
|
COPY server/db/names.json ./dist/names.json
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -8,7 +8,7 @@ push:
|
||||||
docker push fossorial/pangolin:latest
|
docker push fossorial/pangolin:latest
|
||||||
|
|
||||||
test:
|
test:
|
||||||
docker run -it -p 3000:3000 -p 3001:3001 --env-file=.env -v ./config:/app/config pangolin
|
docker run -it -p 3000:3000 -p 3001:3001 -v ./config:/app/config fossorial/pangolin:latest
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
docker rmi pangolin
|
docker rmi pangolin
|
||||||
|
|
|
@ -68,6 +68,7 @@
|
||||||
"zod-validation-error": "3.4.0"
|
"zod-validation-error": "3.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"drizzle-kit": "0.24.2",
|
||||||
"esbuild": "0.20.1",
|
"esbuild": "0.20.1",
|
||||||
"esbuild-node-externals": "1.13.0",
|
"esbuild-node-externals": "1.13.0",
|
||||||
"yargs": "17.7.2",
|
"yargs": "17.7.2",
|
||||||
|
@ -83,7 +84,6 @@
|
||||||
"@types/react": "npm:types-react@19.0.0-rc.1",
|
"@types/react": "npm:types-react@19.0.0-rc.1",
|
||||||
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
|
"@types/react-dom": "npm:types-react-dom@19.0.0-rc.1",
|
||||||
"@types/yargs": "17.0.33",
|
"@types/yargs": "17.0.33",
|
||||||
"drizzle-kit": "0.24.2",
|
|
||||||
"eslint": "^8",
|
"eslint": "^8",
|
||||||
"eslint-config-next": "15.0.1",
|
"eslint-config-next": "15.0.1",
|
||||||
"postcss": "^8",
|
"postcss": "^8",
|
||||||
|
|
|
@ -1,12 +1,33 @@
|
||||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||||
import Database from "better-sqlite3";
|
import Database from "better-sqlite3";
|
||||||
import * as schema from "@server/db/schema";
|
import * as schema from "@server/db/schema";
|
||||||
import { APP_PATH } from "@server/config";
|
import { __DIRNAME, APP_PATH } from "@server/config";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
|
import fs from "fs";
|
||||||
|
import logger from "@server/logger";
|
||||||
|
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||||
|
|
||||||
const location = path.join(APP_PATH, "db", "db.sqlite");
|
const location = path.join(APP_PATH, "db", "db.sqlite");
|
||||||
|
|
||||||
|
let dbExists = true;
|
||||||
|
if (!fs.existsSync(location)) {
|
||||||
|
dbExists = false;
|
||||||
|
}
|
||||||
|
|
||||||
const sqlite = new Database(location);
|
const sqlite = new Database(location);
|
||||||
export const db = drizzle(sqlite, { schema });
|
export const db = drizzle(sqlite, { schema });
|
||||||
|
|
||||||
|
if (!dbExists && process.env.ENVIRONMENT === "prod") {
|
||||||
|
logger.info("Running migrations...");
|
||||||
|
try {
|
||||||
|
migrate(db, {
|
||||||
|
migrationsFolder: path.join(__DIRNAME, "migrations"),
|
||||||
|
});
|
||||||
|
logger.info("Migrations completed successfully.");
|
||||||
|
} catch (error) {
|
||||||
|
logger.error("Error running migrations:", error);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default db;
|
export default db;
|
||||||
|
|
Loading…
Reference in a new issue