-
Notifications
You must be signed in to change notification settings - Fork 4
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
Migrate to Prisma ORM #837
Closed
Closed
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e40a20c
Install prisma-client
tomitheninja 9285d75
Initial prisma schema
tomitheninja 89fea66
Configure env vars for prisma
tomitheninja 7c223ad
Create a script that ports migrations
tomitheninja 26ced0a
Convert knex migrations into prisma migrations
tomitheninja e977d12
Migrations are now head-to-head
tomitheninja 8b7006f
Remove knex migration logs from the database
tomitheninja e87c342
Use the recommended naming conventions
tomitheninja 732d5da
Add default values for created at fields
tomitheninja 1b1f3cf
Refactor db seeding to use prisma
tomitheninja 4b90f0f
Add missing non_nulls and rename virtual fields
tomitheninja 3a75e6a
Fix the seeder
tomitheninja 90f112d
Switch to prisma
tomitheninja 48a896e
Create npm scripts for prisma
tomitheninja 96830d9
Update used npm script for db migration
tomitheninja 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 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 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 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,55 @@ | ||
#!/usr/bin/bash | ||
|
||
source './.env' | ||
|
||
# basenames of knex migration files | ||
# migrations=`ls ./migrations/*.ts | cut -c 14- | rev | cut -c 4- | rev` | ||
|
||
migrations=( | ||
'20200328131352_initial_schema' #1 | ||
'20200328173956_add_owner_id_to_group' #2 | ||
'20200408124626_add_non_nullable' #3 | ||
'20200510001427_add_floor_to_profile' #4 | ||
'20200527121145_rename_column_in_groups' #5 | ||
'20200721213650_update_description_lengths' #6 | ||
'20200828113656_migration_add_status_for_tickets' #7 | ||
'20201012224257_add_max_attendees' #8 | ||
'20201013225752_add_role_to_user' #9 | ||
'20210418152354_add_user_id_to_tickets' #10 | ||
'20210611222535_add_place_and_link_to_groups' #11 | ||
'20210626192847_add_wantemail_to_user' #12 | ||
'20210829032204_insert_mocked_prisma_migration_logs' #13 | ||
) | ||
|
||
if [[ "$1" = "--help" ]] | ||
then | ||
echo 'Convert knex migrations into prisma migrations.' | ||
echo 'Usage:' | ||
echo ' ./migration-porter.sh [start from = 1]' | ||
exit 0 | ||
fi | ||
|
||
START_FROM_IDX="${1:-1}" | ||
|
||
for i in `seq $START_FROM_IDX ${#migrations[@]}` | ||
do | ||
F="${migrations[$i-1]}" | ||
|
||
echo "Porting migration $F" | ||
docker run --name 'tanulo-migration' -e POSTGRES_USER="$DB_USERNAME" -e POSTGRES_PASSWORD="$DB_PASSWORD" -e POSTGRES_DB="$tanulo" --rm --net host -d postgres | ||
|
||
sleep 10 | ||
|
||
for j in `seq $i` | ||
do | ||
npx knex migrate:up | ||
done | ||
|
||
npx prisma db pull | ||
echo 'y' | npx prisma migrate dev --create-only -n "$F" | ||
mv ./prisma/migrations/*"$F" -n ./prisma/migrations/"$F" | ||
echo -e "\n-- Objectionjs migration\nINSERT INTO \"migrationTable\" (\"migration_time\", \"name\", \"batch\")\nVALUES (\n CURRENT_TIMESTAMP,\n '$F.ts',\n 1);\n" >> "./prisma/migrations/$F/migration.sql" | ||
|
||
docker kill 'tanulo-migration' | ||
sleep 3 | ||
done |
127 changes: 127 additions & 0 deletions
127
migrations/20210829032204_insert_mocked_prisma_migration_logs.ts
This file contains 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,127 @@ | ||
import * as Knex from 'knex' | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.schema.createTable('_prisma_migrations', (table) => { | ||
table.string('id', 36).primary() | ||
table.string('checksum', 64).notNullable() | ||
table.timestamp('finished_at') | ||
table.string('migration_name', 255).notNullable() | ||
table.string('logs') | ||
table.timestamp('rolled_back_at') | ||
table.timestamp('started_at').notNullable() | ||
table.integer('applied_steps_count') | ||
}) | ||
|
||
return knex.table('_prisma_migrations').insert([ | ||
{ | ||
id: 'ec605277-3fd7-436b-814d-7a7806f23b3f', | ||
checksum: '66517a5cc09d205d5e0ce783c6a66829c4991ae1aaa29d7b05dc3734aeb709d1', | ||
migration_name: '20200328131352_initial_schema', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '46631492-c804-4f1a-85b8-a909b62837fc', | ||
checksum: 'afee0d4c885c333b31f4b016fa7602d8c6c2420b3fb2a6512f94fec03fd4b5af', | ||
migration_name: '20200328173956_add_owner_id_to_group', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '23daa180-0ef6-4e32-8e8a-3c38d611f0de', | ||
checksum: 'c1d348b0b81e2f43c3bd5e048eb78b03098c51533053ac0b072a50a453a15f2f', | ||
migration_name: '20200408124626_add_non_nullable', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '164d5175-407a-4534-b9b5-74f362d02260', | ||
checksum: '2cbb2f78fd00d6da87010bfc68779afd4fa10e2beba797422789ae6949ab6be1', | ||
migration_name: '20200510001427_add_floor_to_profile', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: 'a2c12db3-ef64-4c7f-9a75-a35df6c8c27d', | ||
checksum: 'adc769d24b44e029aeadfc17e5239bd4dc9fe140830b1d3907df07a103f59a3d', | ||
migration_name: '20200527121145_rename_column_in_groups', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '945f1416-bae3-43ac-8c79-7eba59c6b70e', | ||
checksum: 'b9ea38b901440a825c37b4344d85f6c869a3e32fc021576d4436066aee01b6de', | ||
migration_name: '20200721213650_update_description_lengths', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '62fd4180-f9a4-4763-848b-530c22508a07', | ||
checksum: '9bccdccb9d26d0ce4fbb40cf15e2a96e8b92205db377d9c89d7094e05e00c235', | ||
migration_name: '20200828113656_migration_add_status_for_tickets', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '0cc14462-e876-4ac6-9e3b-f8785df7d61f', | ||
checksum: '64c27a4a9419cffb52cb11fc7b3c1faec6d563f90952663f97563d2b32b78d1a', | ||
migration_name: '20201012224257_add_max_attendees', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '99837065-b062-4da7-be83-f66085485b49', | ||
checksum: '20c03bfcf56907fd44992d0b090fd178e6fd755df0c056aea0988c24b7ee11ca', | ||
migration_name: '20201013225752_add_role_to_user', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '74f11458-b655-4076-9392-586967faafa4', | ||
checksum: '33c7c37d36bd1976edb134eb0e538bed166ccce6cc49dd5a0226444c9f4d948a', | ||
migration_name: '20210418152354_add_user_id_to_tickets', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '07df2122-3d22-43d5-a0b0-439bf656b8f7', | ||
checksum: 'ac35a78a746ad8b4fcbc87c0ed2e7ccbd29e081b4cfb80ee225a67f9695ea936', | ||
migration_name: '20210611222535_add_place_and_link_to_groups', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '70e518c8-91ae-4af0-9570-78094b14ea2b', | ||
checksum: 'e47909787202b22822dd4395b865fcde2eb24b3d43dcc263cd78dde8ae4853c8', | ||
migration_name: '20210626192847_add_wantemail_to_user', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
{ | ||
id: '66fdb87d-2b6a-4567-b455-70e27323de2c', | ||
checksum: '374da65652aaf3e425b0cde5169cfeb799853e9ac958119fc46a3673085406fd', | ||
migration_name: '20210829032204_insert_mocked_prisma_migration_logs', | ||
applied_steps_count: 1, | ||
started_at: new Date(), | ||
finished_at: new Date() | ||
}, | ||
]) | ||
} | ||
|
||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('_prisma_migrations') | ||
} | ||
|
This file contains 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
78 changes: 78 additions & 0 deletions
78
prisma/migrations/20200328131352_initial_schema/migration.sql
This file contains 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,78 @@ | ||
-- CreateTable | ||
CREATE TABLE "groups" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(255), | ||
"subject" VARCHAR(255), | ||
"description" VARCHAR(255), | ||
"start_date" TIMESTAMPTZ(6), | ||
"end_date" TIMESTAMPTZ(6), | ||
"room" INTEGER, | ||
"do_not_disturb" BOOLEAN, | ||
"created_at" TIMESTAMPTZ(6), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "migrationTable" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(255), | ||
"batch" INTEGER, | ||
"migration_time" TIMESTAMPTZ(6), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "migrationTable_lock" ( | ||
"index" SERIAL NOT NULL, | ||
"is_locked" INTEGER, | ||
|
||
PRIMARY KEY ("index") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "tickets" ( | ||
"id" SERIAL NOT NULL, | ||
"description" VARCHAR(255), | ||
"room_number" INTEGER, | ||
"created_at" TIMESTAMPTZ(6), | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "users" ( | ||
"id" SERIAL NOT NULL, | ||
"name" VARCHAR(255), | ||
"email" VARCHAR(255), | ||
"auth_sch_id" VARCHAR(255), | ||
"admin" BOOLEAN, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "users_groups" ( | ||
"id" SERIAL NOT NULL, | ||
"user_id" INTEGER, | ||
"group_id" INTEGER, | ||
|
||
PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "users_groups_groupid_index" ON "users_groups"("group_id"); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "users_groups_userid_index" ON "users_groups"("user_id"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "users_groups" ADD FOREIGN KEY ("group_id") REFERENCES "groups"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "users_groups" ADD FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200328131352_initial_schema.ts', 1); |
12 changes: 12 additions & 0 deletions
12
prisma/migrations/20200328173956_add_owner_id_to_group/migration.sql
This file contains 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,12 @@ | ||
-- AlterTable | ||
ALTER TABLE "groups" ADD COLUMN "owner_id" INTEGER; | ||
|
||
-- CreateIndex | ||
CREATE INDEX "groups_ownerid_index" ON "groups"("owner_id"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "groups" ADD FOREIGN KEY ("owner_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200328173956_add_owner_id_to_group.ts', 1); |
34 changes: 34 additions & 0 deletions
34
prisma/migrations/20200408124626_add_non_nullable/migration.sql
This file contains 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,34 @@ | ||
/* | ||
Warnings: | ||
|
||
- Made the column `name` on table `groups` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `start_date` on table `groups` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `end_date` on table `groups` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `room` on table `groups` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `do_not_disturb` on table `groups` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `description` on table `tickets` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `room_number` on table `tickets` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `name` on table `users` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `email` on table `users` required. This step will fail if there are existing NULL values in that column. | ||
- Made the column `auth_sch_id` on table `users` required. This step will fail if there are existing NULL values in that column. | ||
|
||
*/ | ||
-- AlterTable | ||
ALTER TABLE "groups" ALTER COLUMN "name" SET NOT NULL, | ||
ALTER COLUMN "start_date" SET NOT NULL, | ||
ALTER COLUMN "end_date" SET NOT NULL, | ||
ALTER COLUMN "room" SET NOT NULL, | ||
ALTER COLUMN "do_not_disturb" SET NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "tickets" ALTER COLUMN "description" SET NOT NULL, | ||
ALTER COLUMN "room_number" SET NOT NULL; | ||
|
||
-- AlterTable | ||
ALTER TABLE "users" ALTER COLUMN "name" SET NOT NULL, | ||
ALTER COLUMN "email" SET NOT NULL, | ||
ALTER COLUMN "auth_sch_id" SET NOT NULL; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200408124626_add_non_nullable.ts', 1); |
6 changes: 6 additions & 0 deletions
6
prisma/migrations/20200510001427_add_floor_to_profile/migration.sql
This file contains 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,6 @@ | ||
-- AlterTable | ||
ALTER TABLE "users" ADD COLUMN "floor" INTEGER; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200510001427_add_floor_to_profile.ts', 1); |
7 changes: 7 additions & 0 deletions
7
prisma/migrations/20200527121145_rename_column_in_groups/migration.sql
This file contains 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,7 @@ | ||
-- AlterTable | ||
ALTER TABLE "groups" | ||
RENAME COLUMN "subject" TO "tags"; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200527121145_rename_column_in_groups.ts', 1); |
10 changes: 10 additions & 0 deletions
10
prisma/migrations/20200721213650_update_description_lengths/migration.sql
This file contains 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,10 @@ | ||
-- AlterTable | ||
ALTER TABLE "groups" ALTER COLUMN "description" SET DATA TYPE VARCHAR(500); | ||
|
||
-- AlterTable | ||
ALTER TABLE "tickets" ALTER COLUMN "description" DROP NOT NULL, | ||
ALTER COLUMN "description" SET DATA TYPE VARCHAR(500); | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP,'20200721213650_update_description_lengths.ts', 1); |
9 changes: 9 additions & 0 deletions
9
prisma/migrations/20200828113656_migration_add_status_for_tickets/migration.sql
This file contains 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,9 @@ | ||
-- CreateEnum | ||
CREATE TYPE "status_type" AS ENUM ('SENT', 'IN_PROGRESS', 'DONE', 'ARCHIVED'); | ||
|
||
-- AlterTable | ||
ALTER TABLE "tickets" ADD COLUMN "status" "status_type" NOT NULL DEFAULT E'SENT'; | ||
|
||
-- Objectionjs migration | ||
INSERT INTO "migrationTable" ("migration_time", "name", "batch") | ||
VALUES (CURRENT_TIMESTAMP, '20200828113656_migration_add_status_for_tickets.ts', 1); |
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.
CI crashes, because this is missing.
Can we introduce a new env var, or I should copy this line into the CI config?
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.
I think you'll have to add it to both .env and the CI configs, since we'll need it in both places (CI uses the vars defined in the yaml, while live env uses the .env file).