Skip to content

Commit

Permalink
build: add database migration step
Browse files Browse the repository at this point in the history
  • Loading branch information
omermecitoglu committed Dec 1, 2024
1 parent 36cba17 commit d70ea3d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ jobs:
# with:
# token: ${{ secrets.CODECOV_TOKEN }}

- name: Create database
run: npm run build:migration

- name: Build package
run: npm run build

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"lint": "next lint",
"lint:fix": "next lint --fix",
"dev": "next dev",
"build:migration": "tsx scripts/migrate.ts",
"build:ui": "next build",
"postbuild:ui": "rimraf ui && copyfiles -u 2 -a '.next/standalone/**/*' ui && copyfiles -a '.next/static/**/*' ui && copyfiles -a 'public/**/*' ui && rimraf ui/node_modules",
"build": "npm run build:cli && npm run build:ui"
Expand Down
3 changes: 3 additions & 0 deletions scripts/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import migrateAppDatabase from "~/database/migrate";

migrateAppDatabase();
16 changes: 16 additions & 0 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from "node:path";
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";

function migrateAppDatabase() {
const cwd = process.env.CURRENT_WORKING_DIRECTORY || process.cwd();
const migrationsFolder = process.env.DRIZZLE_DIR || path.resolve(cwd, "drizzle");
const sqlite = new Database(path.resolve(cwd, "buttler.db"));
const db = drizzle(sqlite);

migrate(db, { migrationsFolder });
sqlite.close();
}

export default migrateAppDatabase;
14 changes: 2 additions & 12 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
process.title = "buttler";
const path = await import("node:path");
const { default: Database } = await import("better-sqlite3");
const { drizzle } = await import("drizzle-orm/better-sqlite3");
const { migrate } = await import("drizzle-orm/better-sqlite3/migrator");

const cwd = process.env.CURRENT_WORKING_DIRECTORY || process.cwd();
const migrationsFolder = process.env.DRIZZLE_DIR || path.resolve(cwd, "drizzle");
const sqlite = new Database(path.resolve(cwd, "buttler.db"));
const db = drizzle(sqlite);

migrate(db, { migrationsFolder });
sqlite.close();
const { default: migrate } = await import("./database/migrate");
migrate();
}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"scripts/**/*.ts",
"drizzle.config.ts",
"jest.config.js",
"next.config.js",
Expand Down

0 comments on commit d70ea3d

Please sign in to comment.