Skip to content

Commit

Permalink
Advanced prisma migrations (#9)
Browse files Browse the repository at this point in the history
* add: migrations

* fix: remove phone from types

* add: migrations

* fix: format
  • Loading branch information
genzyy authored Nov 20, 2023
1 parent adabee2 commit e36c2d0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ check-format: ## Check format using eslint and prettier
format: ## Format project with eslint and prettier
yarn run lint:fix && yarn run prettier:fix

.PHONY: db-generate
db-generate: ## Apply new model changes to db using prisma orm
yarn run prisma generate
.PHONY: gen-migration
gen-migration: ## Generate migration file
yarn run prisma migrate dev --name $(m) --create-only

.PHONY: apply-migration
apply-migration: ## Apply migration file changes to db
yarn run prisma migrate dev

.PHONY: db-push
db-push:
Expand Down
54 changes: 54 additions & 0 deletions prisma/migrations/20231120073318_add_tables/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- CreateEnum
CREATE TYPE "Role" AS ENUM ('USER', 'SUPERADMIN', 'DATAMANAGER');

-- CreateTable
CREATE TABLE "ApiMetadata" (
"id" SERIAL NOT NULL,
"key" TEXT NOT NULL,
"value" TEXT NOT NULL,
"created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated" TIMESTAMP(3) NOT NULL,

CONSTRAINT "ApiMetadata_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"uuid" UUID NOT NULL,
"email" TEXT NOT NULL,
"password" TEXT NOT NULL,
"username" TEXT NOT NULL,
"role" "Role" NOT NULL DEFAULT 'USER',
"name" VARCHAR(255),
"age" INTEGER,
"created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated" TIMESTAMP(3) NOT NULL,
"signedOut" TIMESTAMP(3),

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "Note" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
"uuid" UUID NOT NULL,
"content" VARCHAR(255) NOT NULL,
"created" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated" TIMESTAMP(3) NOT NULL,

CONSTRAINT "Note_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "ApiMetadata_key_key" ON "ApiMetadata"("key");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- AddForeignKey
ALTER TABLE "Note" ADD CONSTRAINT "Note_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
Warnings:
- You are about to drop the column `age` on the `User` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "User" DROP COLUMN "age";
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
2 changes: 0 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ model User {
username String @unique
role Role @default(USER)
name String? @db.VarChar(255)
age Int?
phone String? @db.VarChar(10)
notes Note[]
created DateTime @default(now())
updated DateTime @updatedAt
Expand Down
4 changes: 0 additions & 4 deletions src/types/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export const UserKeys: string[] = [
'username',
'role',
'name',
'age',
'phone',
'created',
'updated',
'signedOut',
Expand All @@ -32,8 +30,6 @@ export const UserReturn = include<UserModel, Key>([
'username',
'email',
'name',
'age',
'phone',
'created',
'password',
'signedOut',
Expand Down
4 changes: 1 addition & 3 deletions tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('test /auth routes', () => {
const newUser = {
email: faker.internet.email(),
username: faker.internet.userName(),
password: faker.internet.password(10),
password: faker.internet.password({ length: 10 }),
};
it('registers new user', async () => {
const stub = sinon.stub(EmailService, 'sendOnboardingEmail');
Expand All @@ -26,8 +26,6 @@ describe('test /auth routes', () => {
'username',
'role',
'name',
'age',
'phone',
'created',
'updated',
]);
Expand Down

0 comments on commit e36c2d0

Please sign in to comment.