summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-03 22:39:10 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-02-03 22:39:10 +1100
commit8c56c27d1c305e0f7d2b67c613bdd1a087def7a0 (patch)
treeb8d50048363cab714d7f6e6ca64eb66fe885bd67
parentMerge branch 'master' of github.com:fosscord/fosscord-server (diff)
downloadserver-8c56c27d1c305e0f7d2b67c613bdd1a087def7a0.tar.xz
On db sync/first run, add all the current migrations to migrations table to prevent running them on restarts
-rw-r--r--src/util/entities/Migration.ts4
-rw-r--r--src/util/util/Database.ts12
2 files changed, 14 insertions, 2 deletions
diff --git a/src/util/entities/Migration.ts b/src/util/entities/Migration.ts
index e8351650..57097d8c 100644
--- a/src/util/entities/Migration.ts
+++ b/src/util/entities/Migration.ts
@@ -21,8 +21,8 @@ import {
 	Entity,
 	ObjectIdColumn,
 	PrimaryGeneratedColumn,
+	BaseEntity,
 } from "typeorm";
-import { BaseClassWithoutId } from "./BaseClass";
 
 export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith(
 	"mongodb",
@@ -31,7 +31,7 @@ export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith(
 	: PrimaryGeneratedColumn;
 
 @Entity("migrations")
-export class Migration extends BaseClassWithoutId {
+export class Migration extends BaseEntity {
 	@PrimaryIdAutoGenerated()
 	id: number;
 
diff --git a/src/util/util/Database.ts b/src/util/util/Database.ts
index 492fb4e4..d4509f67 100644
--- a/src/util/util/Database.ts
+++ b/src/util/util/Database.ts
@@ -18,6 +18,7 @@
 
 import { DataSource } from "typeorm";
 import { yellow, green, red } from "picocolors";
+import { Migration } from "../entities/Migration";
 import { ConfigEntity } from "../entities/Config";
 import { config } from "dotenv";
 import path from "path";
@@ -107,6 +108,17 @@ export async function initDatabase(): Promise<DataSource> {
 			"[Database] This appears to be a fresh database. Synchronising.",
 		);
 		await dbConnection.synchronize();
+
+		// On next start, typeorm will try to run all the migrations again from beginning.
+		// Manually insert every current migration to prevent this:
+		await Promise.all(
+			dbConnection.migrations.map((migration) =>
+				Migration.insert({
+					name: migration.name,
+					timestamp: Date.now(),
+				}),
+			),
+		);
 	} else {
 		console.log("[Database] Applying missing migrations, if any.");
 		await dbConnection.runMigrations();