-
-
Notifications
You must be signed in to change notification settings - Fork 465
feat: migrate to prisma-orm #434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rdeisenroth
wants to merge
13
commits into
C4illin:main
Choose a base branch
from
Rdeisenroth:feature/prisma-orm
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ae2cd5e
start migrationg to prisma-orm
Rdeisenroth 132622d
rename db.fileName -> db,file
Rdeisenroth 37d61f0
undo accidental change
Rdeisenroth 882b4db
Merge branch 'main' into feature/prisma-orm
Rdeisenroth f3fa0a1
keep job creation date as text for compatibility
Rdeisenroth b3025bb
rename initial migration
Rdeisenroth 64e33e4
automatically run migrations
Rdeisenroth bc9099a
adjust actions + docker containers for prisma
Rdeisenroth 7c55fde
fix docker build
Rdeisenroth ade5a91
lint
Rdeisenroth aa8b985
adjust eslint ignore rule so linter works when dist dir is present
Rdeisenroth 5b9a803
make prisma work in docker image
Rdeisenroth f6f675d
workaround for #435
Rdeisenroth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| -- CreateTable | ||
| CREATE TABLE "users" ( | ||
| "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
| "email" TEXT NOT NULL, | ||
| "password" TEXT NOT NULL | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "jobs" ( | ||
| "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
| "user_id" INTEGER NOT NULL, | ||
| "date_created" TEXT NOT NULL, | ||
| "status" TEXT NOT NULL DEFAULT 'not started', | ||
| "num_files" INTEGER NOT NULL DEFAULT 0, | ||
| CONSTRAINT "jobs_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
| ); | ||
|
|
||
| -- CreateTable | ||
| CREATE TABLE "file_names" ( | ||
| "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
| "job_id" INTEGER NOT NULL, | ||
| "file_name" TEXT NOT NULL, | ||
| "output_file_name" TEXT NOT NULL, | ||
| "status" TEXT NOT NULL DEFAULT 'not started', | ||
| CONSTRAINT "file_names_job_id_fkey" FOREIGN KEY ("job_id") REFERENCES "jobs" ("id") ON DELETE RESTRICT ON UPDATE CASCADE | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "users_email_key" ON "users"("email"); | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # Please do not edit this file manually | ||
| # It should be added in your version-control system (e.g., Git) | ||
| provider = "sqlite" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| // This is your Prisma schema file, | ||
| // learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
|
||
| generator client { | ||
| provider = "prisma-client-js" | ||
| } | ||
|
|
||
| datasource db { | ||
| provider = "sqlite" | ||
| url = "file:../data/mydb.sqlite" | ||
| } | ||
|
|
||
| /// A user of the application | ||
| model User { | ||
| /// The unique identifier for the user | ||
| id Int @id @default(autoincrement()) | ||
| /// The email address of the user | ||
| email String @unique | ||
| /// The password of the user | ||
| password String | ||
| /// The jobs associated with the user | ||
| jobs Job[] | ||
|
|
||
| @@map("users") | ||
| } | ||
|
|
||
| /// A job created by a user | ||
| model Job { | ||
| /// The unique identifier for the job | ||
| id Int @id @default(autoincrement()) | ||
| /// The ID of the user who created the job | ||
| userId Int @map("user_id") | ||
| /// The date and time when the job was created | ||
| dateCreated String @map("date_created") | ||
| /// The current status of the job | ||
| status String @default("not started") | ||
| /// The number of files associated with the job | ||
| numFiles Int @default(0) @map("num_files") | ||
| /// The files associated with the job | ||
| files File[] | ||
|
|
||
| /// The user who created the job | ||
| user User @relation(fields: [userId], references: [id]) | ||
|
|
||
| @@map("jobs") | ||
| } | ||
|
|
||
| /// A file associated with a job | ||
| model File { | ||
| /// The unique identifier for the file | ||
| id Int @id @default(autoincrement()) | ||
| /// The ID of the job this file belongs to | ||
| jobId Int @map("job_id") | ||
| /// The name of the input file | ||
| fileName String @map("file_name") | ||
| /// The name of the output file | ||
| outputFileName String @map("output_file_name") | ||
| /// The current status of the file | ||
| status String @default("not started") | ||
|
|
||
| /// The job this file belongs to | ||
| job Job @relation(fields: [jobId], references: [id]) | ||
|
|
||
| @@map("file_names") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,38 @@ | ||
| import { mkdirSync } from "node:fs"; | ||
| import { Database } from "bun:sqlite"; | ||
| import fs from "node:fs"; | ||
| import { PrismaClient } from "@prisma/client"; | ||
| import { execSync } from "node:child_process"; | ||
|
|
||
| mkdirSync("./data", { recursive: true }); | ||
| const db = new Database("./data/mydb.sqlite", { create: true }); | ||
|
|
||
| if (!db.query("SELECT * FROM sqlite_master WHERE type='table'").get()) { | ||
| db.exec(` | ||
| CREATE TABLE IF NOT EXISTS users ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| email TEXT NOT NULL, | ||
| password TEXT NOT NULL | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS file_names ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| job_id INTEGER NOT NULL, | ||
| file_name TEXT NOT NULL, | ||
| output_file_name TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| FOREIGN KEY (job_id) REFERENCES jobs(id) | ||
| ); | ||
| CREATE TABLE IF NOT EXISTS jobs ( | ||
| id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
| user_id INTEGER NOT NULL, | ||
| date_created TEXT NOT NULL, | ||
| status TEXT DEFAULT 'not started', | ||
| num_files INTEGER DEFAULT 0, | ||
| FOREIGN KEY (user_id) REFERENCES users(id) | ||
| ); | ||
| PRAGMA user_version = 1;`); | ||
| // ensure db exists | ||
| if (!fs.existsSync("./data/mydb.sqlite")) { | ||
| // run bun prisma migrate deploy with child_process | ||
| console.log("Database not found, creating a new one..."); | ||
| execSync("bun prisma migrate deploy"); | ||
| } | ||
|
|
||
| const dbVersion = (db.query("PRAGMA user_version").get() as { user_version?: number }).user_version; | ||
| if (dbVersion === 0) { | ||
| db.exec("ALTER TABLE file_names ADD COLUMN status TEXT DEFAULT 'not started';"); | ||
| db.exec("PRAGMA user_version = 1;"); | ||
| console.log("Updated database to version 1."); | ||
| // The db version before we switched to Prisma | ||
| const prisma = new PrismaClient(); | ||
| const legacyVersion = await prisma.$queryRaw<{ user_version: bigint }[]>`PRAGMA user_version;`; | ||
| if (legacyVersion[0]?.user_version === 1n) { | ||
| // close prisma connection | ||
| await prisma.$disconnect(); | ||
| // Existing legacy database found, needs migration | ||
| console.log("Legacy database found. Skipping initial migration..."); | ||
| execSync("bun prisma migrate resolve --applied 0_init"); | ||
| // reconnect prisma | ||
| await prisma.$connect(); | ||
| // set user_version to 2 | ||
| await prisma.$executeRaw`PRAGMA user_version = 2;`; | ||
| } | ||
|
|
||
| // enable WAL mode | ||
| db.exec("PRAGMA journal_mode = WAL;"); | ||
| console.log("Running database migrations..."); | ||
|
|
||
| // run any pending migrations | ||
| await prisma.$disconnect(); | ||
| execSync("bun prisma migrate deploy"); | ||
| await prisma.$connect(); | ||
|
|
||
| await prisma.$queryRaw`PRAGMA journal_mode = WAL;`.catch((e) => { | ||
| console.error("Failed to set journal mode to WAL:", e); | ||
| }); | ||
|
|
||
| export default db; | ||
| export default prisma; | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the standard way to do it?
Not that it is anything wrong with it just want to double check