summary refs log tree commit diff
path: root/util/src/migrations
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 18:28:50 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 18:28:50 +0200
commitfd152e8b6df18c9d98d02fed9e446de9690650cd (patch)
tree72c6b4bad9373fc02c3cb0ec08bd464200b5daf6 /util/src/migrations
parent:bug: fix vanity url (diff)
downloadserver-fd152e8b6df18c9d98d02fed9e446de9690650cd.tar.xz
added vanity db migration
Diffstat (limited to 'util/src/migrations')
-rw-r--r--util/src/migrations/1633881705509-VanityInvite.ts17
-rw-r--r--util/src/migrations/migrate_db_engine.ts21
2 files changed, 27 insertions, 11 deletions
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.ts b/util/src/migrations/migrate_db_engine.ts
index 33024a8d..cd578fb2 100644
--- a/util/src/migrations/migrate_db_engine.ts
+++ b/util/src/migrations/migrate_db_engine.ts
@@ -1,6 +1,6 @@
 import { config } from "dotenv";
 config();
-import { BaseEntity, createConnection, EntityTarget } from "typeorm";
+import { createConnection, EntityTarget } from "typeorm";
 import { initDatabase } from "../util/Database";
 import "missing-native-js-functions";
 import {
@@ -15,7 +15,6 @@ import {
 	Invite,
 	Member,
 	Message,
-	RateLimit,
 	ReadState,
 	Recipient,
 	Relationship,
@@ -30,9 +29,9 @@ 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
+	// manually arrange them because of foreign keys
 	const entities = [
 		User,
 		Guild,
@@ -57,12 +56,12 @@ async function main() {
 		Attachment,
 	];
 
-	const newDB = await initDatabase();
+	const oldDB = await initDatabase();
 
 	// @ts-ignore
-	const oldDB = await createConnection({
-		type: process.env.FROM.split(":")[0]?.replace("+srv", ""),
-		url: process.env.FROM,
+	const newDB = await createConnection({
+		type: process.env.TO.split(":")[0]?.replace("+srv", ""),
+		url: process.env.TO,
 		entities,
 		name: "old",
 	});
@@ -73,13 +72,12 @@ async function main() {
 			const entity = e as EntityTarget<any>;
 			const entries = await oldDB.manager.find(entity);
 			//@ts-ignore
-			console.log("migrated " + entries.length + " " + entity.name);
+			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;
@@ -115,8 +113,9 @@ async function main() {
 				// 	await newDB.manager.update(entity, { id: entry.id }, entry);
 				// }
 			}
+
 			// @ts-ignore
-			console.log("migrated all " + entity.name);
+			console.log("migrating " + entries.length + " " + entity.name + " ...");
 		}
 	} catch (error) {
 		console.error((error as any).message);