From d92dddb2da9894518647bfd0ac3e10fa4a3648a7 Mon Sep 17 00:00:00 2001 From: huang0h Date: Mon, 16 Sep 2024 18:35:57 -0400 Subject: [PATCH 1/2] add pantries + donations tables --- .../src/migrations/1726524792261-addTables.ts | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 apps/backend/src/migrations/1726524792261-addTables.ts diff --git a/apps/backend/src/migrations/1726524792261-addTables.ts b/apps/backend/src/migrations/1726524792261-addTables.ts new file mode 100644 index 00000000..b82bfd89 --- /dev/null +++ b/apps/backend/src/migrations/1726524792261-addTables.ts @@ -0,0 +1,39 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddTables1726524792261 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE IF NOT EXISTS pantries ( + id SERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + address VARCHAR(255) NOT NULL, + approved BOOLEAN NOT NULL, + ssf_representative_id INT NOT NULL, + pantry_representative_id INT NOT NULL, + restrictions TEXT[] NOT NULL, + + CONSTRAINT fk_ssf_representative_id FOREIGN KEY(ssf_representative_id) REFERENCES user(id), + CONSTRAINT fk_pantry_representative_id FOREIGN KEY(pantry_representative_id) REFERENCES user(id), + ); + + CREATE TABLE IF NOT EXISTS donations ( + id SERIAL PRIMARY KEY, + restrictions TEXT[] NOT NULL, + due_date TIMESTAMP NOT NULL, + pantry_id INT NOT NULL, + status VARCHAR(50) NOT NULL, + feedback TEXT, + contents TEXT NOT NULL, + + CONSTRAINT fk_pantry_id FOREIGN KEY(pantry_id) REFERENCES pantry(id), + ); + `, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DROP TABLE IF EXISTS pantries; DROP TABLE IF EXISTS donations;`, + ); + } +} From ed4e0d70e6029e36a1cb63c68e3b5237a2d75d2d Mon Sep 17 00:00:00 2001 From: Avery Huang Date: Mon, 16 Sep 2024 19:10:23 -0400 Subject: [PATCH 2/2] fix migrations --- apps/backend/src/config/typeorm.ts | 3 ++- apps/backend/src/migrations/1726524792261-addTables.ts | 6 +++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/apps/backend/src/config/typeorm.ts b/apps/backend/src/config/typeorm.ts index fe2cb85f..e75f65b3 100644 --- a/apps/backend/src/config/typeorm.ts +++ b/apps/backend/src/config/typeorm.ts @@ -2,6 +2,7 @@ import { registerAs } from '@nestjs/config'; import { PluralNamingStrategy } from '../strategies/plural-naming.strategy'; import { DataSource, DataSourceOptions } from 'typeorm'; import { User1725726359198 } from '../migrations/1725726359198-User'; +import { AddTables1726524792261 } from '../migrations/1726524792261-addTables'; const config = { type: 'postgres', @@ -15,7 +16,7 @@ const config = { namingStrategy: new PluralNamingStrategy(), // Glob patterns (e.g. ../migrations/**.ts) are deprecated, so we have to manually specify each migration // TODO: see if there's still a way to dynamically load all migrations - migrations: [User1725726359198], + migrations: [User1725726359198, AddTables1726524792261], }; export default registerAs('typeorm', () => config); diff --git a/apps/backend/src/migrations/1726524792261-addTables.ts b/apps/backend/src/migrations/1726524792261-addTables.ts index b82bfd89..df2c7bf0 100644 --- a/apps/backend/src/migrations/1726524792261-addTables.ts +++ b/apps/backend/src/migrations/1726524792261-addTables.ts @@ -12,8 +12,8 @@ export class AddTables1726524792261 implements MigrationInterface { pantry_representative_id INT NOT NULL, restrictions TEXT[] NOT NULL, - CONSTRAINT fk_ssf_representative_id FOREIGN KEY(ssf_representative_id) REFERENCES user(id), - CONSTRAINT fk_pantry_representative_id FOREIGN KEY(pantry_representative_id) REFERENCES user(id), + CONSTRAINT fk_ssf_representative_id FOREIGN KEY(ssf_representative_id) REFERENCES users(id), + CONSTRAINT fk_pantry_representative_id FOREIGN KEY(pantry_representative_id) REFERENCES users(id) ); CREATE TABLE IF NOT EXISTS donations ( @@ -25,7 +25,7 @@ export class AddTables1726524792261 implements MigrationInterface { feedback TEXT, contents TEXT NOT NULL, - CONSTRAINT fk_pantry_id FOREIGN KEY(pantry_id) REFERENCES pantry(id), + CONSTRAINT fk_pantry_id FOREIGN KEY(pantry_id) REFERENCES pantries(id) ); `, );