Skip to content

Commit fd97ef7

Browse files
authored
Merge pull request #172 from cuappdev/ashley/migrations
Ashley/migrations
2 parents e1c748d + 5276357 commit fd97ef7

3 files changed

Lines changed: 16 additions & 7 deletions

File tree

src/migrations/1767386999887-AddUserStats.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ import {MigrationInterface, QueryRunner} from "typeorm";
33
export class AddUserStats1767386999887 implements MigrationInterface {
44

55
public async up(queryRunner: QueryRunner): Promise<void> {
6-
await queryRunner.query(`CREATE TABLE "user_following_users" ("follower_id" character varying NOT NULL, "following_id" character varying NOT NULL, CONSTRAINT "PK_user_following_users" PRIMARY KEY ("follower_id", "following_id"))`);
7-
await queryRunner.query(`CREATE INDEX "IDX_user_following_users_follower_id" ON "user_following_users" ("follower_id")`);
8-
await queryRunner.query(`CREATE INDEX "IDX_user_following_users_following_id" ON "user_following_users" ("following_id")`);
9-
await queryRunner.query(`ALTER TABLE "user_following_users" ADD CONSTRAINT "FK_user_following_users_follower_id" FOREIGN KEY ("follower_id") REFERENCES "User"("firebaseUid") ON DELETE CASCADE ON UPDATE CASCADE`);
10-
await queryRunner.query(`ALTER TABLE "user_following_users" ADD CONSTRAINT "FK_user_following_users_following_id" FOREIGN KEY ("following_id") REFERENCES "User"("firebaseUid") ON DELETE CASCADE ON UPDATE CASCADE`);
6+
await queryRunner.query(`CREATE TABLE IF NOT EXISTS "user_following_users" ("follower_id" character varying NOT NULL, "following_id" character varying NOT NULL, CONSTRAINT "PK_user_following_users" PRIMARY KEY ("follower_id", "following_id"))`);
7+
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_user_following_users_follower_id" ON "user_following_users" ("follower_id")`);
8+
await queryRunner.query(`CREATE INDEX IF NOT EXISTS "IDX_user_following_users_following_id" ON "user_following_users" ("following_id")`);
9+
10+
// Add fks only if they don't exist
11+
const fk1Exists = await queryRunner.query(`SELECT 1 FROM pg_constraint WHERE conname = 'FK_user_following_users_follower_id'`);
12+
if (!fk1Exists || fk1Exists.length === 0) {
13+
await queryRunner.query(`ALTER TABLE "user_following_users" ADD CONSTRAINT "FK_user_following_users_follower_id" FOREIGN KEY ("follower_id") REFERENCES "User"("firebaseUid") ON DELETE CASCADE ON UPDATE CASCADE`);
14+
}
15+
16+
const fk2Exists = await queryRunner.query(`SELECT 1 FROM pg_constraint WHERE conname = 'FK_user_following_users_following_id'`);
17+
if (!fk2Exists || fk2Exists.length === 0) {
18+
await queryRunner.query(`ALTER TABLE "user_following_users" ADD CONSTRAINT "FK_user_following_users_following_id" FOREIGN KEY ("following_id") REFERENCES "User"("firebaseUid") ON DELETE CASCADE ON UPDATE CASCADE`);
19+
}
1120

1221
await queryRunner.query(`ALTER TABLE "User" ADD COLUMN IF NOT EXISTS "soldPosts" integer NOT NULL DEFAULT 0`);
1322
}

src/migrations/1769500000000-AddConfirmationSentToTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export class AddConfirmationSentToTransaction1769500000000 implements MigrationI
44
name = 'AddConfirmationSentToTransaction1769500000000'
55

66
public async up(queryRunner: QueryRunner): Promise<void> {
7-
await queryRunner.query(`ALTER TABLE "Transaction" ADD "confirmationSent" boolean NOT NULL DEFAULT false`);
7+
await queryRunner.query(`ALTER TABLE "Transaction" ADD COLUMN IF NOT EXISTS "confirmationSent" boolean NOT NULL DEFAULT false`);
88
}
99

1010
public async down(queryRunner: QueryRunner): Promise<void> {

src/utils/DB.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default async function resellConnection(): Promise<Connection> {
99
logging: true,
1010
password: process.env.DB_PASSWORD,
1111
port: +(process.env.DB_PORT || 5432),
12-
synchronize: true,
12+
synchronize: false,
1313
type: "postgres",
1414
username: process.env.DB_USERNAME || "postgres",
1515
extra: {

0 commit comments

Comments
 (0)