From a66fc6db46738cf0d25ee248f2b46fbcfa5dc592 Mon Sep 17 00:00:00 2001 From: Rishit Date: Thu, 4 Jan 2024 18:08:12 +0530 Subject: [PATCH 1/4] add: down migration command --- Makefile | 8 +++++++- docker-compose.yml | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 587b9e5..94e8023 100644 --- a/Makefile +++ b/Makefile @@ -54,9 +54,15 @@ format: ## Format project with eslint and prettier yarn run lint:fix && yarn run prettier:fix .PHONY: gen-migration -gen-migration: ## Generate migration file +gen-migration: ## Generate up migration file yarn run prisma migrate dev --name $(m) --create-only +.PHONY: gen-down-migration +gen-down-migration: ## Generate down migration file + $(eval include .env.test) + $(eval export $(sh sed 's/=.*//' .env.test)) + yarn run prisma migrate diff --from-schema-datamodel prisma/schema.prisma --to-migrations prisma/migrations --shadow-database-url ${SHADOW_DATABASE_URL} --script down.sql + .PHONY: apply-migration apply-migration: ## Apply migration file changes to db yarn run prisma migrate dev diff --git a/docker-compose.yml b/docker-compose.yml index 48d9e6f..3ccdbb6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,15 @@ services: volumes: - ./pgdata:/var/lib/postgresql/data + prisma-shadow-db: + image: postgres:16.0-alpine + environment: + POSTGRES_USER: shadow-user + POSTGRES_PASSWORD: shadow-password + POSTGRES_DB: shadow-db + ports: + - '5434:5432' + postgresql-test: image: postgres:16.0-alpine environment: From 4928ee2ae7053915e858e9eb935818260d61ddf0 Mon Sep 17 00:00:00 2001 From: Rishit Date: Thu, 4 Jan 2024 18:26:38 +0530 Subject: [PATCH 2/4] fix: down migration --- Makefile | 6 +++++- .../20240104124213_api_context_author/down.sql | 2 ++ .../20240104124213_api_context_author/migration.sql | 10 ++++++++++ prisma/schema.prisma | 13 ++++++++----- 4 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 prisma/migrations/20240104124213_api_context_author/down.sql create mode 100644 prisma/migrations/20240104124213_api_context_author/migration.sql diff --git a/Makefile b/Makefile index 94e8023..f6ae0da 100644 --- a/Makefile +++ b/Makefile @@ -61,7 +61,11 @@ gen-migration: ## Generate up migration file gen-down-migration: ## Generate down migration file $(eval include .env.test) $(eval export $(sh sed 's/=.*//' .env.test)) - yarn run prisma migrate diff --from-schema-datamodel prisma/schema.prisma --to-migrations prisma/migrations --shadow-database-url ${SHADOW_DATABASE_URL} --script down.sql + yarn run prisma migrate diff \ + --from-schema-datamodel prisma/schema.prisma \ + --to-migrations prisma/migrations \ + --shadow-database-url ${SHADOW_DATABASE_URL} \ + --script > down.sql .PHONY: apply-migration apply-migration: ## Apply migration file changes to db diff --git a/prisma/migrations/20240104124213_api_context_author/down.sql b/prisma/migrations/20240104124213_api_context_author/down.sql new file mode 100644 index 0000000..cae539d --- /dev/null +++ b/prisma/migrations/20240104124213_api_context_author/down.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "ApiMetadata" DROP COLUMN "someRandomColumn"; diff --git a/prisma/migrations/20240104124213_api_context_author/migration.sql b/prisma/migrations/20240104124213_api_context_author/migration.sql new file mode 100644 index 0000000..01d9cb9 --- /dev/null +++ b/prisma/migrations/20240104124213_api_context_author/migration.sql @@ -0,0 +1,10 @@ +/* + Warnings: + + - Added the required column `authoredById` to the `ApiMetadata` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "ApiMetadata" ADD COLUMN "authoredById" INTEGER NULL; +UPDATE "ApiMetadata" SET "authoredById" = 1; +ALTER TABLE "ApiMetadata" ALTER COLUMN "authoredById" SET NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 722929b..195207c 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -5,14 +5,17 @@ generator client { datasource db { provider = "postgresql" url = env("DATABASE_URL") + shadowDatabaseUrl = env("SHADOW_DATABASE_URL") } model ApiMetadata { - id Int @id @default(autoincrement()) - key String @unique - value String - created DateTime @default(now()) - updated DateTime @updatedAt + id Int @id @default(autoincrement()) + key String @unique + value String + created DateTime @default(now()) + updated DateTime @updatedAt + authoredById Int + someRandomColumn String } model User { From 2e0e3587a199f078c43b7c940999755319918a92 Mon Sep 17 00:00:00 2001 From: Rishit Date: Sat, 13 Jan 2024 22:12:51 +0530 Subject: [PATCH 3/4] fix: api metadata --- src/jobs/apiMaintenanace.job.ts | 2 +- src/repositories/apiMetadata.ts | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/jobs/apiMaintenanace.job.ts b/src/jobs/apiMaintenanace.job.ts index 3747bcb..76a3cd4 100644 --- a/src/jobs/apiMaintenanace.job.ts +++ b/src/jobs/apiMaintenanace.job.ts @@ -10,7 +10,7 @@ export const ApiMaintenanceJob = cron.schedule( let apiStatus = await apiMetadataRepository.getApiMetadata('status'); if (!apiStatus) { logger.warn('Api status is null, adding status immediately.'); - apiStatus = await apiMetadataRepository.createApiMetadata('status', ApiStatus.Maintenance); + apiStatus = await apiMetadataRepository.createApiMetadata('status', ApiStatus.Maintenance, 1); } config.api.status = apiStatus.value; if (config.api.status === ApiStatus.Live) logger.info('Api is live.'); diff --git a/src/repositories/apiMetadata.ts b/src/repositories/apiMetadata.ts index cddc844..5c83cfe 100644 --- a/src/repositories/apiMetadata.ts +++ b/src/repositories/apiMetadata.ts @@ -1,11 +1,16 @@ import prisma from '../core/prisma/client'; import { ApiMetadatReturn, ApiMetadata } from '../types/apiMetadata'; -const createApiMetadata = async (key: string, value: string): Promise => { +const createApiMetadata = async ( + key: string, + value: string, + authoredById: number, +): Promise => { return (await prisma.apiMetadata.create({ data: { key, value, + authoredById, }, })) as unknown as Promise; }; From beba949d145bab7a2129a2b93c75724778816d19 Mon Sep 17 00:00:00 2001 From: Rishit Date: Sat, 13 Jan 2024 22:14:46 +0530 Subject: [PATCH 4/4] fix: remove test column --- prisma/schema.prisma | 1 - 1 file changed, 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 195207c..02f7825 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -15,7 +15,6 @@ model ApiMetadata { created DateTime @default(now()) updated DateTime @updatedAt authoredById Int - someRandomColumn String } model User {