summary refs log tree commit diff
path: root/util/src/migrations/migrate_db_engine.ts
blob: b2e71f945686212ae3ae06e8b386a2714ea48389 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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();