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 new file mode 100644 index 00000000..df2c7bf0 --- /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 users(id), + CONSTRAINT fk_pantry_representative_id FOREIGN KEY(pantry_representative_id) REFERENCES users(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 pantries(id) + ); + `, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `DROP TABLE IF EXISTS pantries; DROP TABLE IF EXISTS donations;`, + ); + } +}