diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 15b3138..5a63eb8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/package.json b/package.json index 16cb37b..ec1d8b0 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/scripts/migrate.ts b/scripts/migrate.ts new file mode 100644 index 0000000..1f2d344 --- /dev/null +++ b/scripts/migrate.ts @@ -0,0 +1,3 @@ +import migrateAppDatabase from "~/database/migrate"; + +migrateAppDatabase(); diff --git a/src/database/migrate.ts b/src/database/migrate.ts new file mode 100644 index 0000000..573eb2f --- /dev/null +++ b/src/database/migrate.ts @@ -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; diff --git a/src/instrumentation.ts b/src/instrumentation.ts index ccdb20e..7e464b7 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -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(); } } diff --git a/tsconfig.json b/tsconfig.json index eebf6da..5f39f95 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,6 +23,7 @@ "include": [ "src/**/*.ts", "src/**/*.tsx", + "scripts/**/*.ts", "drizzle.config.ts", "jest.config.js", "next.config.js",