mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-12 21:52:40 +01:00
On db sync/first run, add all the current migrations to migrations table to prevent running them on restarts
This commit is contained in:
parent
d899942b1a
commit
6d8e6eca9d
@ -21,8 +21,8 @@ import {
|
|||||||
Entity,
|
Entity,
|
||||||
ObjectIdColumn,
|
ObjectIdColumn,
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
|
BaseEntity,
|
||||||
} from "typeorm";
|
} from "typeorm";
|
||||||
import { BaseClassWithoutId } from "./BaseClass";
|
|
||||||
|
|
||||||
export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith(
|
export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith(
|
||||||
"mongodb",
|
"mongodb",
|
||||||
@ -31,7 +31,7 @@ export const PrimaryIdAutoGenerated = process.env.DATABASE?.startsWith(
|
|||||||
: PrimaryGeneratedColumn;
|
: PrimaryGeneratedColumn;
|
||||||
|
|
||||||
@Entity("migrations")
|
@Entity("migrations")
|
||||||
export class Migration extends BaseClassWithoutId {
|
export class Migration extends BaseEntity {
|
||||||
@PrimaryIdAutoGenerated()
|
@PrimaryIdAutoGenerated()
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
|
|
||||||
import { DataSource } from "typeorm";
|
import { DataSource } from "typeorm";
|
||||||
import { yellow, green, red } from "picocolors";
|
import { yellow, green, red } from "picocolors";
|
||||||
|
import { Migration } from "../entities/Migration";
|
||||||
import { ConfigEntity } from "../entities/Config";
|
import { ConfigEntity } from "../entities/Config";
|
||||||
import { config } from "dotenv";
|
import { config } from "dotenv";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
@ -107,6 +108,17 @@ export async function initDatabase(): Promise<DataSource> {
|
|||||||
"[Database] This appears to be a fresh database. Synchronising.",
|
"[Database] This appears to be a fresh database. Synchronising.",
|
||||||
);
|
);
|
||||||
await dbConnection.synchronize();
|
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 {
|
} else {
|
||||||
console.log("[Database] Applying missing migrations, if any.");
|
console.log("[Database] Applying missing migrations, if any.");
|
||||||
await dbConnection.runMigrations();
|
await dbConnection.runMigrations();
|
||||||
|
Loading…
Reference in New Issue
Block a user