From cbbda36a1d76576730b9d69e4648513faeeedb1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 6 Dec 2024 12:08:14 +0000 Subject: [PATCH 1/3] ci: Test Prisma migrations --- package.json | 7 ++++--- .../20241206120523_rename_buildprices/migration.sql | 3 +++ prisma/schema.prisma | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 prisma/migrations/20241206120523_rename_buildprices/migration.sql diff --git a/package.json b/package.json index cb18dab..7dec9d7 100644 --- a/package.json +++ b/package.json @@ -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", + "prisma:studio": "dotenv -e .env.local -- npx prisma studio", "postinstall": "npm run prisma:generate", "prettier": "prettier --write .", "prepare": "husky", diff --git a/prisma/migrations/20241206120523_rename_buildprices/migration.sql b/prisma/migrations/20241206120523_rename_buildprices/migration.sql new file mode 100644 index 0000000..8e9baca --- /dev/null +++ b/prisma/migrations/20241206120523_rename_buildprices/migration.sql @@ -0,0 +1,3 @@ +-- AlterTable +ALTER TABLE "buildprices" +RENAME COLUMN "housetypedescription" TO "house_type_description"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index a71acab..86e4c55 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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") From 0286a89b74dcab088d4e3976b34bf9870bda508a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 6 Dec 2024 14:05:34 +0000 Subject: [PATCH 2/3] docs: Explain migration progress --- prisma/docs/how-to-update-prisma-schema.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 prisma/docs/how-to-update-prisma-schema.md diff --git a/prisma/docs/how-to-update-prisma-schema.md b/prisma/docs/how-to-update-prisma-schema.md new file mode 100644 index 0000000..a3ccb3c --- /dev/null +++ b/prisma/docs/how-to-update-prisma-schema.md @@ -0,0 +1,22 @@ +## 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 (thus losing data). Manually generating an `ALTER table UPDATE column` SQL command 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 + +3. 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. + +4. 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`). + From 4df94c7fa61601d47a262c1f4a33c628cf1ba3f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dafydd=20Ll=C5=B7r=20Pearson?= Date: Fri, 6 Dec 2024 14:17:26 +0000 Subject: [PATCH 3/3] docs: Clarify create-only migration --- prisma/docs/how-to-update-prisma-schema.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/prisma/docs/how-to-update-prisma-schema.md b/prisma/docs/how-to-update-prisma-schema.md index a3ccb3c..faf38bd 100644 --- a/prisma/docs/how-to-update-prisma-schema.md +++ b/prisma/docs/how-to-update-prisma-schema.md @@ -10,13 +10,15 @@ Make a change to this file which describes the change you would like to make (e. 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 (thus losing data). Manually generating an `ALTER table UPDATE column` SQL command allows us to work around this. You'll then need to run `npm run prisma:migrate` to apply your changes. +> 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 -3. Update Prisma client +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. -4. Open PR, merge to `main` +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`).