Skip to content

Commit

Permalink
Merge pull request #161 from lil-lab/recnet-api/create-migration-down
Browse files Browse the repository at this point in the history
[recnet-api] Create migration down
  • Loading branch information
joannechen1223 authored Mar 11, 2024
2 parents 2165f30 + 55e19b9 commit a014ca7
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
39 changes: 39 additions & 0 deletions apps/recnet-api/prisma/migrations/20240306064817_init/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- DropForeignKey
ALTER TABLE "FollowingRecord" DROP CONSTRAINT "FollowingRecord_userId_fkey";

-- DropForeignKey
ALTER TABLE "FollowingRecord" DROP CONSTRAINT "FollowingRecord_followerId_fkey";

-- DropForeignKey
ALTER TABLE "Recommendation" DROP CONSTRAINT "Recommendation_userId_fkey";

-- DropForeignKey
ALTER TABLE "Recommendation" DROP CONSTRAINT "Recommendation_articleId_fkey";

-- DropForeignKey
ALTER TABLE "InviteCode" DROP CONSTRAINT "InviteCode_ownerId_fkey";

-- DropForeignKey
ALTER TABLE "InviteCode" DROP CONSTRAINT "InviteCode_usedById_fkey";

-- DropTable
DROP TABLE "User";

-- DropTable
DROP TABLE "FollowingRecord";

-- DropTable
DROP TABLE "Recommendation";

-- DropTable
DROP TABLE "Article";

-- DropTable
DROP TABLE "InviteCode";

-- DropEnum
DROP TYPE "Provider";

-- DropEnum
DROP TYPE "Role";

4 changes: 2 additions & 2 deletions apps/recnet-api/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("PRISMA_DATABASE_URL")
provider = "postgresql"
url = env("PRISMA_DATABASE_URL")
}

// Define enum types
Expand Down
18 changes: 18 additions & 0 deletions apps/recnet-api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@
"forwardAllArgs": false
}
},
"prisma:migrate": {
"executor": "nx:run-commands",
"options": {
"commands": ["sh apps/recnet-api/scripts/dbMigrate.sh"],
"cwd": ".",
"forwardAllArgs": true
}
},
"prisma:deploy": {
"executor": "nx:run-commands",
"options": {
"commands": [
"pnpx prisma migrate deploy --schema=apps/recnet-api/prisma/schema.prisma"
],
"cwd": ".",
"forwardAllArgs": false
}
},
"deploy": {
"executor": "nx:run-commands",
"options": {
Expand Down
32 changes: 32 additions & 0 deletions apps/recnet-api/scripts/dbMigrate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

PRISMA_SCHEMA="apps/recnet-api/prisma/schema.prisma"
PRISMA_MIGRATIONS_DIR="apps/recnet-api/prisma/migrations"

MIGRATION_NAME="$1"

perform_migration_up() {
pnpx prisma migrate dev --name $MIGRATION_NAME --schema="$PRISMA_SCHEMA"
}

perform_migration_down() {
npx prisma migrate diff \
--from-schema-datamodel $PRISMA_SCHEMA \
--to-schema-datasource $PRISMA_SCHEMA \
--script > down.sql
}

move_down_sql_file() {
DIRECTORY=$(find $PRISMA_MIGRATIONS_DIR -type d -name "*$MIGRATION_NAME*" | tail -n 1)
mv down.sql $DIRECTORY/down.sql
}

echo "Migrating database to $MIGRATION_NAME"
echo "Performing migration down"
perform_migration_down

echo "Performing migration up"
perform_migration_up

echo "Moving down.sql to migration directory"
move_down_sql_file

0 comments on commit a014ca7

Please sign in to comment.