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

Migrate to Prisma ORM #837

Closed
wants to merge 15 commits into from
Closed
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ DB_PORT=5432
DB_USERNAME=tanulo
DB_PASSWORD=tanulo
DB_DATABASE=tanulo

# Don't touch this
DB_URL="postgresql://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_DATABASE}?schema=public"
Copy link
Contributor Author

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?

Copy link
Member

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).

2 changes: 1 addition & 1 deletion .github/workflows/cypress.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
with:
# Specify Browser since container image is compile with Firefox
browser: chrome
wait-on: yarn migrate
wait-on: yarn prisma:migrate
build: yarn build:prod
start: yarn start
env:
Expand Down
55 changes: 55 additions & 0 deletions migration-porter.sh
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 migrations/20210829032204_insert_mocked_prisma_migration_logs.ts
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')
}

13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
"build": "yarn build:ts && yarn build:css && yarn lint && yarn copy:static-assets",
"build:prod": "yarn build:ts && yarn build:css && yarn copy:static-assets",
"serve": "node dist/src/server.js",
"migrate": "knex migrate:latest",
"seed": "knex seed:run",
"migrate:upgrade": "knex migrate:latest",
"prisma:migrate": "npx prisma migrate deploy",
"prisma:migrate:new": "npx prisma migrate dev --create-only --preview-feature",
"prisma:pull": "npx prisma db pull",
"prisma:build": "npx prisma generate",
"prisma:seed": "npx prisma db seed --preview-feature",
"watch:node": "nodemon",
"watch": "concurrently -k -p \"[{name}]\" -n \"Css,Static,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"yarn watch:css\" \"yarn watch:static-assets\" \"yarn watch:node\"",
"test": "yarn run cypress open",
"build:ts": "etsc",
"build:ts": "yarn prisma:build && etsc",
"build:css": "NODE_ENV=production postcss public/css/tailwind.css -o dist/public/css/styles.css",
"watch:css": "NODE_ENV=development postcss public/css/tailwind.css -o dist/public/css/styles.css -w",
"watch:fe": "concurrently -k -p \"[{name}]\" -n \"Css,Static\" -c \"yellow.bold,cyan.bold\" \"yarn watch:css\" \"yarn watch:static-assets\"",
"lint": "tsc --noEmit && eslint \"**/*.{js,ts}\"",
"copy:static-assets": "node -r esm copyStaticAssets.js",
"watch:static-assets": "chokidar \"views/**/*.pug\" \"public/js/**/*.js\" -c \"yarn copy:static-assets\"",
"debug": "yarn build && yarn watch:debug",
"serve:debug": "nodemon --inspect",
"serve:debug": "nodemon --exec 'tsc && node --inspect dist/src/server.js'",
"watch:debug": "concurrently -k -p \"[{name}]\" -n \"Css,Static,Node\" -c \"yellow.bold,cyan.bold,green.bold\" \"yarn watch:css\" \"yarn watch:static-assets\" \"yarn serve:debug\""
},
"dependencies": {
"@prisma/client": "^2.30.0",
"@tailwindcss/forms": "0.3.3",
"@tailwindcss/typography": "0.4.1",
"autoprefixer": "10.3.1",
Expand Down
78 changes: 78 additions & 0 deletions prisma/migrations/20200328131352_initial_schema/migration.sql
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);
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 prisma/migrations/20200408124626_add_non_nullable/migration.sql
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);
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);
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);
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);
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);
Loading