2021-10-04 23:14:03 +02:00
|
|
|
import { config } from "dotenv";
|
|
|
|
config();
|
2021-10-05 17:00:58 +02:00
|
|
|
import { BaseEntity, createConnection, EntityTarget } from "typeorm";
|
2021-10-04 23:14:03 +02:00
|
|
|
import { initDatabase } from "../util/Database";
|
|
|
|
import "missing-native-js-functions";
|
2021-10-05 17:00:58 +02:00
|
|
|
import {
|
|
|
|
Application,
|
|
|
|
Attachment,
|
|
|
|
Ban,
|
|
|
|
Channel,
|
|
|
|
ConnectedAccount,
|
|
|
|
defaultSettings,
|
|
|
|
Emoji,
|
|
|
|
Guild,
|
|
|
|
Invite,
|
|
|
|
Member,
|
|
|
|
Message,
|
|
|
|
RateLimit,
|
|
|
|
ReadState,
|
|
|
|
Recipient,
|
|
|
|
Relationship,
|
|
|
|
Role,
|
|
|
|
Sticker,
|
|
|
|
Team,
|
|
|
|
TeamMember,
|
|
|
|
Template,
|
|
|
|
User,
|
|
|
|
VoiceState,
|
|
|
|
Webhook,
|
|
|
|
} from "..";
|
2021-10-04 23:14:03 +02:00
|
|
|
|
|
|
|
async function main() {
|
|
|
|
if (!process.env.FROM) throw new Error("FROM database env connection string not set");
|
|
|
|
|
2021-10-05 17:00:58 +02:00
|
|
|
// manually arrange them because of foreign key
|
|
|
|
const entities = [
|
|
|
|
User,
|
|
|
|
Guild,
|
|
|
|
Channel,
|
|
|
|
Invite,
|
|
|
|
Role,
|
|
|
|
Ban,
|
|
|
|
Application,
|
|
|
|
Emoji,
|
|
|
|
ConnectedAccount,
|
|
|
|
Member,
|
|
|
|
ReadState,
|
|
|
|
Recipient,
|
|
|
|
Relationship,
|
|
|
|
Sticker,
|
|
|
|
Team,
|
|
|
|
TeamMember,
|
|
|
|
Template,
|
|
|
|
VoiceState,
|
|
|
|
Webhook,
|
|
|
|
Message,
|
|
|
|
Attachment,
|
|
|
|
];
|
2021-10-04 23:14:03 +02:00
|
|
|
|
|
|
|
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",
|
|
|
|
});
|
2021-10-05 17:00:58 +02:00
|
|
|
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("migrated " + entries.length + " " + entity.name);
|
|
|
|
|
|
|
|
for (const entry of entries) {
|
|
|
|
console.log(i++);
|
2021-10-04 23:14:03 +02:00
|
|
|
|
2021-10-05 17:00:58 +02:00
|
|
|
if (entry instanceof User) {
|
|
|
|
console.log("instance of 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,
|
|
|
|
};
|
|
|
|
}
|
2021-10-04 23:14:03 +02:00
|
|
|
}
|
2021-10-05 17:00:58 +02:00
|
|
|
// @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);
|
|
|
|
// }
|
|
|
|
}
|
2021-10-04 23:14:03 +02:00
|
|
|
// @ts-ignore
|
2021-10-05 17:00:58 +02:00
|
|
|
console.log("migrated all " + entity.name);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error((error as any).message);
|
|
|
|
}
|
2021-10-04 23:14:03 +02:00
|
|
|
|
|
|
|
console.log("SUCCESS migrated all data");
|
2021-10-05 17:00:58 +02:00
|
|
|
await newDB.close();
|
2021-10-04 23:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
main().caught();
|