Skip to content
Open
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
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,19 @@ 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
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions prisma/migrations/20240104124213_api_context_author/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "ApiMetadata" DROP COLUMN "someRandomColumn";
10 changes: 10 additions & 0 deletions prisma/migrations/20240104124213_api_context_author/migration.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 7 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ 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
}

model User {
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/apiMaintenanace.job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
7 changes: 6 additions & 1 deletion src/repositories/apiMetadata.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import prisma from '../core/prisma/client';
import { ApiMetadatReturn, ApiMetadata } from '../types/apiMetadata';

const createApiMetadata = async (key: string, value: string): Promise<ApiMetadata> => {
const createApiMetadata = async (
key: string,
value: string,
authoredById: number,
): Promise<ApiMetadata> => {
return (await prisma.apiMetadata.create({
data: {
key,
value,
authoredById,
},
})) as unknown as Promise<ApiMetadata>;
};
Expand Down