From cb0885dd713bd4b6d1de9a494a69eba71992bfc3 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 4 Oct 2021 23:14:03 +0200 Subject: :sparkles: db migrate script --- util/src/migrations/migrate_db_engine.ts | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 util/src/migrations/migrate_db_engine.ts (limited to 'util/src/migrations/migrate_db_engine.ts') 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(); -- cgit 1.5.1