summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-04 23:14:03 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-04 23:14:03 +0200
commit17e31a00f0f88102a686eb7bfccb859ad530b34e (patch)
tree04140706b515e26aa37251416521e7d1781ec1be
parent:sparkles: generalize primary column to work with mongodb object id (diff)
downloadserver-17e31a00f0f88102a686eb7bfccb859ad530b34e.tar.xz
:sparkles: db migrate script
Diffstat (limited to '')
-rw-r--r--util/src/entities/BaseClass.ts3
-rw-r--r--util/src/migrations/migrate_db_engine.ts47
-rw-r--r--util/src/util/Database.ts6
3 files changed, 52 insertions, 4 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts
index f105086a..beccf04b 100644
--- a/util/src/entities/BaseClass.ts
+++ b/util/src/entities/BaseClass.ts
@@ -15,7 +15,7 @@ import "missing-native-js-functions";
 // btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects
 
 export class BaseClassWithoutId extends BaseEntity {
-	constructor(private props?: any) {
+	constructor(props?: any) {
 		super();
 		this.assign(props);
 	}
@@ -56,7 +56,6 @@ export class BaseClassWithoutId extends BaseEntity {
 	@BeforeUpdate()
 	@BeforeInsert()
 	validate() {
-		this.assign(this.props);
 		return this;
 	}
 
diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts
new file mode 100644
index 00000000..b2e71f94
--- /dev/null
+++ b/util/src/migrations/migrate_db_engine.ts
@@ -0,0 +1,47 @@
+import { config } from "dotenv";
+config();
+import * as Models from "../entities";
+import { User } from "../entities/User";
+import { createConnection, Connection } from "typeorm";
+import { initDatabase } from "../util/Database";
+import "missing-native-js-functions";
+
+async function main() {
+	if (!process.env.FROM) throw new Error("FROM database env connection string not set");
+
+	// @ts-ignore
+	const entities = Object.values(Models).filter((x) => x.constructor.name !== "Object" && x.name);
+
+	const newDB = await initDatabase();
+
+	// @ts-ignore
+	const oldDB = await createConnection({
+		type: process.env.FROM.split(":")[0]?.replace("+srv", ""),
+		url: process.env.FROM,
+		entities,
+		name: "old",
+	});
+
+	await Promise.all(
+		entities.map(async (x) => {
+			const data = await oldDB.manager.find(User);
+
+			await Promise.all(
+				data.map(async (x) => {
+					try {
+						await newDB.manager.insert(User, x);
+					} catch (error) {
+						if (!x.id) throw new Error("object doesn't have a unique id: " + x);
+						await newDB.manager.update(User, { id: x.id }, x);
+					}
+				})
+			);
+			// @ts-ignore
+			console.log("migrated all " + x.name);
+		})
+	);
+
+	console.log("SUCCESS migrated all data");
+}
+
+main().caught();
diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts
index 6445bd60..5f057053 100644
--- a/util/src/util/Database.ts
+++ b/util/src/util/Database.ts
@@ -11,7 +11,7 @@ var promise: Promise<any>;
 var dbConnection: Connection | undefined;
 let dbConnectionString = process.env.DATABASE || path.join(process.cwd(), "database.db");
 
-export function initDatabase() {
+export function initDatabase(): Promise<Connection> {
 	if (promise) return promise; // prevent initalizing multiple times
 
 	const type = dbConnectionString.includes(":") ? dbConnectionString.split(":")[0]?.replace("+srv", "") : "sqlite";
@@ -23,7 +23,8 @@ export function initDatabase() {
 		type,
 		url: isSqlite ? undefined : dbConnectionString,
 		database: isSqlite ? dbConnectionString : undefined,
-		entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
+		// @ts-ignore
+		entities: Object.values(Models).filter((x) => x.constructor.name !== "Object" && x.name),
 		synchronize: type !== "mongodb",
 		logging: false,
 		cache: {
@@ -31,6 +32,7 @@ export function initDatabase() {
 		},
 		bigNumberStrings: false,
 		supportBigNumbers: true,
+		name: "default",
 	});
 
 	promise.then((connection) => {