summary refs log tree commit diff
path: root/util/src/migrations
diff options
context:
space:
mode:
Diffstat (limited to 'util/src/migrations')
-rw-r--r--util/src/migrations/1633864260873-EmojiRoles.ts13
-rw-r--r--util/src/migrations/1633864669243-EmojiUser.ts23
-rw-r--r--util/src/migrations/1633881705509-VanityInvite.ts17
-rw-r--r--util/src/migrations/migrate_db_engine.js109
-rw-r--r--util/src/migrations/migrate_db_engine.ts129
5 files changed, 162 insertions, 129 deletions
diff --git a/util/src/migrations/1633864260873-EmojiRoles.ts b/util/src/migrations/1633864260873-EmojiRoles.ts
new file mode 100644
index 00000000..f0d709f2
--- /dev/null
+++ b/util/src/migrations/1633864260873-EmojiRoles.ts
@@ -0,0 +1,13 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class EmojiRoles1633864260873 implements MigrationInterface {
+	name = "EmojiRoles1633864260873";
+
+	public async up(queryRunner: QueryRunner): Promise<void> {
+		await queryRunner.query(`ALTER TABLE "emojis" ADD "roles" text NOT NULL DEFAULT ''`);
+	}
+
+	public async down(queryRunner: QueryRunner): Promise<void> {
+		await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN column_name "roles"`);
+	}
+}
diff --git a/util/src/migrations/1633864669243-EmojiUser.ts b/util/src/migrations/1633864669243-EmojiUser.ts
new file mode 100644
index 00000000..982405d7
--- /dev/null
+++ b/util/src/migrations/1633864669243-EmojiUser.ts
@@ -0,0 +1,23 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class EmojiUser1633864669243 implements MigrationInterface {
+	name = "EmojiUser1633864669243";
+
+	public async up(queryRunner: QueryRunner): Promise<void> {
+		await queryRunner.query(`ALTER TABLE "emojis" ADD "user_id" varchar`);
+		try {
+			await queryRunner.query(
+				`ALTER TABLE "emojis" ADD CONSTRAINT FK_fa7ddd5f9a214e28ce596548421 FOREIGN KEY (user_id) REFERENCES users(id)`
+			);
+		} catch (error) {
+			console.error(
+				"sqlite doesn't support altering foreign keys: https://stackoverflow.com/questions/1884818/how-do-i-add-a-foreign-key-to-an-existing-sqlite-table"
+			);
+		}
+	}
+
+	public async down(queryRunner: QueryRunner): Promise<void> {
+		await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN column_name "user_id"`);
+		await queryRunner.query(`ALTER TABLE "emojis" DROP CONSTRAINT FK_fa7ddd5f9a214e28ce596548421`);
+	}
+}
diff --git a/util/src/migrations/1633881705509-VanityInvite.ts b/util/src/migrations/1633881705509-VanityInvite.ts
new file mode 100644
index 00000000..af9b98ae
--- /dev/null
+++ b/util/src/migrations/1633881705509-VanityInvite.ts
@@ -0,0 +1,17 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class VanityInvite1633881705509 implements MigrationInterface {
+	public async up(queryRunner: QueryRunner): Promise<void> {
+		try {
+			await queryRunner.query(`ALTER TABLE "emojis" DROP COLUMN vanity_url_code`);
+			await queryRunner.query(`ALTER TABLE "emojis" DROP CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad`);
+		} catch (error) {}
+	}
+
+	public async down(queryRunner: QueryRunner): Promise<void> {
+		await queryRunner.query(`ALTER TABLE "emojis" ADD vanity_url_code varchar`);
+		await queryRunner.query(
+			`ALTER TABLE "emojis" ADD CONSTRAINT FK_c2c1809d79eb120ea0cb8d342ad FOREIGN KEY ("vanity_url_code") REFERENCES "invites"("code") ON DELETE NO ACTION ON UPDATE NO ACTION`
+		);
+	}
+}
diff --git a/util/src/migrations/migrate_db_engine.js b/util/src/migrations/migrate_db_engine.js
new file mode 100644
index 00000000..79e9d86f
--- /dev/null
+++ b/util/src/migrations/migrate_db_engine.js
@@ -0,0 +1,109 @@
+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,
+	ConfigEntity,
+	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 keys
+	const entities = [
+		ConfigEntity,
+		User,
+		Guild,
+		Channel,
+		Invite,
+		Role,
+		Ban,
+		Application,
+		Emoji,
+		ConnectedAccount,
+		Member,
+		ReadState,
+		Recipient,
+		Relationship,
+		Sticker,
+		Team,
+		TeamMember,
+		Template,
+		VoiceState,
+		Webhook,
+		Message,
+		Attachment,
+	];
+
+	const oldDB = await initDatabase();
+
+	const type = process.env.TO.includes("://") ? process.env.TO.split(":")[0]?.replace("+srv", "") : "sqlite";
+	const isSqlite = type.includes("sqlite");
+
+	// @ts-ignore
+	const newDB = await createConnection({
+		type,
+		url: isSqlite ? undefined : process.env.TO,
+		database: isSqlite ? process.env.TO : undefined,
+		entities,
+		name: "new",
+		synchronize: true,
+	});
+	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 33024a8d..00000000
--- a/util/src/migrations/migrate_db_engine.ts
+++ /dev/null
@@ -1,129 +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,
-	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");
-
-	// 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();
-
-	// @ts-ignore
-	const oldDB = await createConnection({
-		type: process.env.FROM.split(":")[0]?.replace("+srv", ""),
-		url: process.env.FROM,
-		entities,
-		name: "old",
-	});
-	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++);
-
-				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 " + entity.name);
-		}
-	} catch (error) {
-		console.error((error as any).message);
-	}
-
-	console.log("SUCCESS migrated all data");
-	await newDB.close();
-}
-
-main().caught();