Skip to content
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

test(ci): Test Prisma migrations #160

Merged
merged 3 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest; open ./coverage/lcov-report/index.html",
"prisma:pull": "dotenv -e .env.local npx prisma db pull",
"prisma:pull": "dotenv -e .env.local -- npx prisma db pull",
"prisma:generate": "prisma generate",
"prisma:migrate": "dotenv -e .env.local npx prisma migrate dev",
"prisma:studio": "dotenv -e .env.local npx prisma studio",
"prisma:migrate": "dotenv -e .env.local -- npx prisma migrate dev",
"prisma:migrate-create-only": "dotenv -e .env.local -- npx prisma migrate dev --create-only",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spent way too long trying to get the --create-only flag to be picked up here.

Classic case of RTFM 🤦 - explainer in docs here why the additional -- is required https://www.prisma.io/docs/orm/more/development-environment/environment-variables/managing-env-files-and-setting-variables#using-dotenv-cli-via-command-line

"prisma:studio": "dotenv -e .env.local -- npx prisma studio",
"postinstall": "npm run prisma:generate",
"prettier": "prettier --write .",
"prepare": "husky",
Expand Down
24 changes: 24 additions & 0 deletions prisma/docs/how-to-update-prisma-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## How to update Fairhold's Prisma schema

This guide explains how we can update our Prisma schema and manage migrations to the production database. This highlights the steps we need to take within this repo, for a wider primer please see the Prisma docs: https://www.prisma.io/docs/orm/prisma-migrate


1. Update `schema.prisma`
Make a change to this file which describes the change you would like to make (e.g. add column, add table).

2. Generate migration
Run `npm run prisma:migrate` - this will apply to the change to your local dev database, and generate a migration file in `/prisma/migrations`

> [!TIP]
> The command `npm run prisma:migrate-create-only` will generate a migration file without running it. This is helpful if you want to add any additional changes, and manually configure the migration.
>
> For example when renaming a column Prisma may drop and recreate the column (thus losing data). Manually writing the correct SQL in the generated migration file (e.g. `ALTER table UPDATE column`) allows us to work around this. You'll then need to run `npm run prisma:migrate` to apply your changes.
>
> Docs: https://www.prisma.io/docs/orm/prisma-migrate/workflows/development-and-production#customizing-migrations

1. Update Prisma client
Run `npm run prisma:generate` to update your local client. You should now be able to both interact with the updated Prisma client as well as view the change in your local dev database.

1. Open PR, merge to `main`
From here, we can open the changes up to peer-review on GitHub. Once approved and merged, CI will apply our new migration to the production database using the "Deploy migrations" action (dir: `/.github/workflows/deploy-migrations.yaml`).

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "buildprices"
RENAME COLUMN "housetypedescription" TO "house_type_description";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ datasource db {

model BuildPrices {
id Int @id @default(autoincrement())
houseTypeDescription String? @map("housetypedescription") @db.VarChar(250)
houseTypeDescription String? @map("house_type_description") @db.VarChar(250)
houseType String? @map("housetype") @db.VarChar(1)
priceRange String? @map("pricerange") @db.VarChar(50)
priceMid Float? @map("pricemid")
Expand Down
Loading