mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-10 12:42:44 +01:00
🎨 improve migration script
This commit is contained in:
parent
9d2f0ddf5c
commit
3e01ff75ed
@ -1,16 +1,61 @@
|
||||
import { config } from "dotenv";
|
||||
config();
|
||||
import * as Models from "../entities";
|
||||
import { User } from "../entities/User";
|
||||
import { createConnection, Connection } from "typeorm";
|
||||
import { BaseEntity, 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,
|
||||
RateLimit,
|
||||
ReadState,
|
||||
Recipient,
|
||||
Relationship,
|
||||
Role,
|
||||
Sticker,
|
||||
Team,
|
||||
TeamMember,
|
||||
Template,
|
||||
User,
|
||||
VoiceState,
|
||||
Webhook,
|
||||
} from "..";
|
||||
|
||||
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);
|
||||
// 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,
|
||||
];
|
||||
|
||||
const newDB = await initDatabase();
|
||||
|
||||
@ -21,27 +66,64 @@ async function main() {
|
||||
entities,
|
||||
name: "old",
|
||||
});
|
||||
let i = 0;
|
||||
|
||||
await Promise.all(
|
||||
entities.map(async (x) => {
|
||||
const data = await oldDB.manager.find(User);
|
||||
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);
|
||||
|
||||
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);
|
||||
for (const entry of entries) {
|
||||
console.log(i++);
|
||||
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
// @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("migrated all " + x.name);
|
||||
})
|
||||
);
|
||||
console.log("migrated all " + entity.name);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error((error as any).message);
|
||||
}
|
||||
|
||||
console.log("SUCCESS migrated all data");
|
||||
await newDB.close();
|
||||
}
|
||||
|
||||
main().caught();
|
||||
|
Loading…
Reference in New Issue
Block a user