1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 05:02:37 +01:00

🎨 update migration script

This commit is contained in:
Flam3rboy 2021-10-10 18:31:04 +02:00
parent 25aa1eeb37
commit 0cabe8ed4c

View File

@ -15,7 +15,6 @@ import {
Invite, Invite,
Member, Member,
Message, Message,
RateLimit,
ReadState, ReadState,
Recipient, Recipient,
Relationship, Relationship,
@ -30,7 +29,7 @@ import {
} from ".."; } from "..";
async function main() { async function main() {
if (!process.env.FROM) throw new Error("FROM database env connection string not set"); if (!process.env.TO) throw new Error("TO database env connection string not set");
// manually arrange them because of foreign key // manually arrange them because of foreign key
const entities = [ const entities = [
@ -59,10 +58,14 @@ async function main() {
const newDB = await initDatabase(); const newDB = await initDatabase();
const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite";
const isSqlite = type.includes("sqlite");
// @ts-ignore // @ts-ignore
const oldDB = await createConnection({ const oldDB = await createConnection({
type: process.env.FROM.split(":")[0]?.replace("+srv", ""), type,
url: process.env.FROM, url: isSqlite ? undefined : process.env.TO,
database: isSqlite ? process.env.TO : undefined,
entities, entities,
name: "old", name: "old",
}); });
@ -72,14 +75,13 @@ async function main() {
for (const e of entities) { for (const e of entities) {
const entity = e as EntityTarget<any>; const entity = e as EntityTarget<any>;
const entries = await oldDB.manager.find(entity); const entries = await oldDB.manager.find(entity);
//@ts-ignore // @ts-ignore
console.log("migrated " + entries.length + " " + entity.name); console.log("migrating " + entries.length + " " + entity.name + " ...");
for (const entry of entries) { for (const entry of entries) {
console.log(i++); console.log(i++);
if (entry instanceof User) { if (entry instanceof User) {
console.log("instance of User");
if (entry.bio == null) entry.bio = ""; if (entry.bio == null) entry.bio = "";
if (entry.rights == null) entry.rights = "0"; if (entry.rights == null) entry.rights = "0";
if (entry.disabled == null) entry.disabled = false; if (entry.disabled == null) entry.disabled = false;
@ -116,7 +118,7 @@ async function main() {
// } // }
} }
// @ts-ignore // @ts-ignore
console.log("migrated all " + entity.name); console.log("migrated " + entries.length + " " + entity.name);
} }
} catch (error) { } catch (error) {
console.error((error as any).message); console.error((error as any).message);