1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-06 19:02:33 +01:00
server/util/scripts/migrate_db_engine.js

110 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-10-10 19:00:50 +02:00
const { config } = require("dotenv");
2021-10-04 23:14:03 +02:00
config();
2021-10-10 19:00:50 +02:00
const { createConnection } = require("typeorm");
const { initDatabase } = require("../../dist/util/Database");
require("missing-native-js-functions");
const {
2021-10-05 17:00:58 +02:00
Application,
Attachment,
Ban,
Channel,
2021-10-14 00:32:15 +02:00
ConfigEntity,
2021-10-05 17:00:58 +02:00
ConnectedAccount,
Emoji,
Guild,
Invite,
Member,
Message,
ReadState,
Recipient,
Relationship,
Role,
Sticker,
Team,
TeamMember,
Template,
User,
VoiceState,
Webhook,
2021-10-10 19:00:50 +02:00
} = require("../../dist/entities/index");
2021-10-04 23:14:03 +02:00
async function main() {
2021-10-10 18:28:50 +02:00
if (!process.env.TO) throw new Error("TO database env connection string not set");
2021-10-04 23:14:03 +02:00
2021-10-10 18:28:50 +02:00
// manually arrange them because of foreign keys
2021-10-05 17:00:58 +02:00
const entities = [
2021-10-14 00:32:15 +02:00
ConfigEntity,
2021-10-05 17:00:58 +02:00
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
2021-10-10 18:28:50 +02:00
const oldDB = await initDatabase();
2021-10-04 23:14:03 +02:00
2021-10-10 18:31:04 +02:00
const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite";
const isSqlite = type.includes("sqlite");
2021-10-04 23:14:03 +02:00
// @ts-ignore
2021-10-14 00:32:15 +02:00
const newDB = await createConnection({
2021-10-10 18:31:04 +02:00
type,
url: isSqlite ? undefined : process.env.TO,
database: isSqlite ? process.env.TO : undefined,
2021-10-04 23:14:03 +02:00
entities,
2021-10-14 00:32:15 +02:00
name: "new",
synchronize: true,
2021-10-04 23:14:03 +02:00
});
2021-10-05 17:00:58 +02:00
let i = 0;
try {
2021-10-10 19:00:50 +02:00
for (const entity of entities) {
2021-10-05 17:00:58 +02:00
const entries = await oldDB.manager.find(entity);
2021-10-10 18:31:04 +02:00
// @ts-ignore
2021-10-10 18:28:50 +02:00
console.log("migrating " + entries.length + " " + entity.name + " ...");
2021-10-05 17:00:58 +02:00
for (const entry of entries) {
console.log(i++);
2021-10-04 23:14:03 +02:00
2021-10-10 19:00:50 +02:00
try {
await newDB.manager.insert(entity, entry);
} catch (error) {
try {
if (!entry.id) throw new Error("object doesn't have a unique id: " + entry);
await newDB.manager.update(entity, { id: entry.id }, entry);
} catch (error) {
console.error("couldn't migrate " + i + " " + entity.name, error);
2021-10-05 17:00:58 +02:00
}
}
}
2021-10-10 18:28:50 +02:00
2021-10-04 23:14:03 +02:00
// @ts-ignore
2021-10-10 18:31:04 +02:00
console.log("migrated " + entries.length + " " + entity.name);
2021-10-05 17:00:58 +02:00
}
} catch (error) {
2021-10-10 19:00:50 +02:00
console.error(error.message);
2021-10-05 17:00:58 +02:00
}
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();