summary refs log tree commit diff
path: root/util/src/migrations/migrate_db_engine.ts
blob: cd578fb2e3ef27321352a2c3fa486a1f81bdce10 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { config } from "dotenv";
config();
import { createConnection, EntityTarget } from "typeorm";
import { initDatabase } from "../util/Database";
import "missing-native-js-functions";
import {
	Application,
	Attachment,
	Ban,
	Channel,
	ConnectedAccount,
	defaultSettings,
	Emoji,
	Guild,
	Invite,
	Member,
	Message,
	ReadState,
	Recipient,
	Relationship,
	Role,
	Sticker,
	Team,
	TeamMember,
	Template,
	User,
	VoiceState,
	Webhook,
} from "..";

async function main() {
	if (!process.env.TO) throw new Error("TO database env connection string not set");

	// manually arrange them because of foreign keys
	const entities = [
		User,
		Guild,
		Channel,
		Invite,
		Role,
		Ban,
		Application,
		Emoji,
		ConnectedAccount,
		Member,
		ReadState,
		Recipient,
		Relationship,
		Sticker,
		Team,
		TeamMember,
		Template,
		VoiceState,
		Webhook,
		Message,
		Attachment,
	];

	const oldDB = await initDatabase();

	// @ts-ignore
	const newDB = await createConnection({
		type: process.env.TO.split(":")[0]?.replace("+srv", ""),
		url: process.env.TO,
		entities,
		name: "old",
	});
	let i = 0;

	try {
		for (const e of entities) {
			const entity = e as EntityTarget<any>;
			const entries = await oldDB.manager.find(entity);
			//@ts-ignore
			console.log("migrating " + entries.length + " " + entity.name + " ...");

			for (const entry of entries) {
				console.log(i++);

				if (entry instanceof User) {
					if (entry.bio == null) entry.bio = "";
					if (entry.rights == null) entry.rights = "0";
					if (entry.disabled == null) entry.disabled = false;
					if (entry.fingerprints == null) entry.fingerprints = [];
					if (entry.deleted == null) entry.deleted = false;
					if (entry.data == null) {
						entry.data = {
							valid_tokens_since: new Date(0),
							hash: undefined,
						};
						// @ts-ignore
						if (entry.user_data) {
							// TODO: relationships
							entry.data = {
								// @ts-ignore
								valid_tokens_since: entry.user_data.valid_tokens_since, // @ts-ignore
								hash: entry.user_data.hash,
							};
						}
					}
					// @ts-ignore
					if (entry.settings == null) {
						entry.settings = defaultSettings;
						// @ts-ignore
						if (entry.user_data) entry.settings = entry.user_settings;
					}
				}

				// try {
				await newDB.manager.insert(entity, entry);
				// } catch (error) {
				// 	if (!entry.id) throw new Error("object doesn't have a unique id: " + entry);
				// 	await newDB.manager.update(entity, { id: entry.id }, entry);
				// }
			}

			// @ts-ignore
			console.log("migrating " + entries.length + " " + entity.name + " ...");
		}
	} catch (error) {
		console.error((error as any).message);
	}

	console.log("SUCCESS migrated all data");
	await newDB.close();
}

main().caught();