From 0cabe8ed4c4cf8ce5ff1bca5bac1454fecfde77e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 18:31:04 +0200 Subject: :art: update migration script --- util/src/migrations/migrate_db_engine.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'util/src/migrations/migrate_db_engine.ts') diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts index 33024a8d..527e7b53 100644 --- a/util/src/migrations/migrate_db_engine.ts +++ b/util/src/migrations/migrate_db_engine.ts @@ -15,7 +15,6 @@ import { Invite, Member, Message, - RateLimit, ReadState, Recipient, Relationship, @@ -30,7 +29,7 @@ import { } from ".."; 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 const entities = [ @@ -59,10 +58,14 @@ async function main() { 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 const oldDB = await createConnection({ - type: process.env.FROM.split(":")[0]?.replace("+srv", ""), - url: process.env.FROM, + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, entities, name: "old", }); @@ -72,14 +75,13 @@ async function main() { for (const e of entities) { const entity = e as EntityTarget; const entries = await oldDB.manager.find(entity); - //@ts-ignore - console.log("migrated " + entries.length + " " + entity.name); + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); 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; @@ -116,7 +118,7 @@ async function main() { // } } // @ts-ignore - console.log("migrated all " + entity.name); + console.log("migrated " + entries.length + " " + entity.name); } } catch (error) { console.error((error as any).message); -- cgit 1.5.1 From aa25dac99f9ab2c595d9deeb4067f753796438a1 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 10 Oct 2021 19:00:50 +0200 Subject: :bug: fix modify role.permissions --- api/assets/schemas.json | 5 +- api/src/routes/guilds/#guild_id/roles.ts | 6 +- util/src/migrations/migrate_db_engine.js | 104 ++++++++++++++++++++++++ util/src/migrations/migrate_db_engine.ts | 131 ------------------------------- 4 files changed, 111 insertions(+), 135 deletions(-) create mode 100644 util/src/migrations/migrate_db_engine.js delete mode 100644 util/src/migrations/migrate_db_engine.ts (limited to 'util/src/migrations/migrate_db_engine.ts') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 4f1ab9a8..2ceaa923 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -2909,6 +2909,9 @@ } } }, + "required": [ + "image" + ], "definitions": { "ChannelPermissionOverwriteType": { "enum": [ @@ -4744,7 +4747,7 @@ "type": "string" }, "permissions": { - "type": "bigint" + "type": "string" }, "color": { "type": "integer" diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index 0a57c6a2..b1875598 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -17,7 +17,7 @@ const router: Router = Router(); export interface RoleModifySchema { name?: string; - permissions?: bigint; + permissions?: string; color?: number; hoist?: boolean; // whether the role should be displayed separately in the sidebar mentionable?: boolean; // whether the role should be mentionable @@ -57,7 +57,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" }) ...body, guild_id: guild_id, managed: false, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))), + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")), tags: undefined }); @@ -109,7 +109,7 @@ router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_ ...body, id: role_id, guild_id, - permissions: String(req.permission!.bitfield & (body.permissions || BigInt("0"))) + permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")) }); await Promise.all([ diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js new file mode 100644 index 00000000..7b8b5784 --- /dev/null +++ b/util/src/migrations/migrate_db_engine.js @@ -0,0 +1,104 @@ +const { config } = require("dotenv"); +config(); +const { createConnection } = require("typeorm"); +const { initDatabase } = require("../../dist/util/Database"); +require("missing-native-js-functions"); +const { + Application, + Attachment, + Ban, + Channel, + ConnectedAccount, + Emoji, + Guild, + Invite, + Member, + Message, + ReadState, + Recipient, + Relationship, + Role, + Sticker, + Team, + TeamMember, + Template, + User, + VoiceState, + Webhook, +} = require("../../dist/entities/index"); + +async function main() { + if (!process.env.TO) throw new Error("TO database env connection string not set"); + + // 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(); + + const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; + const isSqlite = type.includes("sqlite"); + + // @ts-ignore + const oldDB = await createConnection({ + type, + url: isSqlite ? undefined : process.env.TO, + database: isSqlite ? process.env.TO : undefined, + entities, + name: "old", + }); + let i = 0; + + try { + for (const entity of entities) { + const entries = await oldDB.manager.find(entity); + // @ts-ignore + console.log("migrating " + entries.length + " " + entity.name + " ..."); + + for (const entry of entries) { + console.log(i++); + + 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); + } + } + } + // @ts-ignore + console.log("migrated " + entries.length + " " + entity.name); + } + } catch (error) { + console.error(error.message); + } + + console.log("SUCCESS migrated all data"); + await newDB.close(); +} + +main().caught(); diff --git a/util/src/migrations/migrate_db_engine.ts b/util/src/migrations/migrate_db_engine.ts deleted file mode 100644 index 527e7b53..00000000 --- a/util/src/migrations/migrate_db_engine.ts +++ /dev/null @@ -1,131 +0,0 @@ -import { config } from "dotenv"; -config(); -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, - 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 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(); - - const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite"; - const isSqlite = type.includes("sqlite"); - - // @ts-ignore - const oldDB = await createConnection({ - type, - url: isSqlite ? undefined : process.env.TO, - database: isSqlite ? process.env.TO : undefined, - entities, - name: "old", - }); - let i = 0; - - try { - for (const e of entities) { - const entity = e as EntityTarget; - 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("migrated " + entries.length + " " + entity.name); - } - } catch (error) { - console.error((error as any).message); - } - - console.log("SUCCESS migrated all data"); - await newDB.close(); -} - -main().caught(); -- cgit 1.5.1