mirror of
https://github.com/fosrl/pangolin.git
synced 2025-07-10 05:54:58 +02:00
Initial pass at migrations
This commit is contained in:
parent
29bd88ebdf
commit
2a265e5cdd
9 changed files with 191 additions and 26 deletions
|
@ -1,33 +1,12 @@
|
|||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import Database from "better-sqlite3";
|
||||
import * as schema from "@server/db/schema";
|
||||
import { __DIRNAME, APP_PATH } from "@server/config";
|
||||
import { APP_PATH } from "@server/config";
|
||||
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");
|
||||
|
||||
let dbExists = true;
|
||||
if (!fs.existsSync(location)) {
|
||||
dbExists = false;
|
||||
}
|
||||
export const location = path.join(APP_PATH, "db", "db.sqlite");
|
||||
|
||||
const sqlite = new Database(location);
|
||||
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;
|
||||
|
|
|
@ -356,6 +356,11 @@ export const resourceOtp = sqliteTable("resourceOtp", {
|
|||
expiresAt: integer("expiresAt").notNull()
|
||||
});
|
||||
|
||||
export const versionMigrations = sqliteTable("versionMigrations", {
|
||||
version: text("version").primaryKey(),
|
||||
executedAt: integer("executedAt").notNull()
|
||||
});
|
||||
|
||||
export type Org = InferSelectModel<typeof orgs>;
|
||||
export type User = InferSelectModel<typeof users>;
|
||||
export type Site = InferSelectModel<typeof sites>;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue