summary refs log tree commit diff
path: root/src/util/migration/mariadb
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-10-04 23:21:50 +1100
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2023-10-04 23:40:05 +1100
commit7795f1b36ba03877ec222531326664661a93628a (patch)
treec5f062a76971711cbfb1abf80015cd1aaa68f3eb /src/util/migration/mariadb
parentMerge pull request #1099 from CyberL1/master (diff)
downloadserver-7795f1b36ba03877ec222531326664661a93628a.tar.xz
refactor channel positions in guild
Diffstat (limited to 'src/util/migration/mariadb')
-rw-r--r--src/util/migration/mariadb/1696420827239-guildChannelOrdering.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/util/migration/mariadb/1696420827239-guildChannelOrdering.ts b/src/util/migration/mariadb/1696420827239-guildChannelOrdering.ts
new file mode 100644
index 00000000..bbab72e7
--- /dev/null
+++ b/src/util/migration/mariadb/1696420827239-guildChannelOrdering.ts
@@ -0,0 +1,40 @@
+import { MigrationInterface, QueryRunner } from "typeorm";
+
+export class guildChannelOrdering1696420827239 implements MigrationInterface {
+	name = "guildChannelOrdering1696420827239";
+
+	public async up(queryRunner: QueryRunner): Promise<void> {
+		const guilds = await queryRunner.query(
+			`SELECT id FROM guilds`,
+			undefined,
+			true,
+		);
+
+		await queryRunner.query(
+			`ALTER TABLE guilds ADD channelOrdering text NOT NULL DEFAULT '[]'`,
+		);
+
+		for (const guild_id of guilds.records.map((x) => x.id)) {
+			const channels: Array<{ position: number; id: string }> = (
+				await queryRunner.query(
+					`SELECT id, position FROM channels WHERE guild_id = ?`,
+					[guild_id],
+					true,
+				)
+			).records;
+
+			channels.sort((a, b) => a.position - b.position);
+
+			await queryRunner.query(
+				`UPDATE guilds SET channelOrdering = ? WHERE id = ?`,
+				[JSON.stringify(channels.map((x) => x.id)), guild_id],
+			);
+		}
+
+		await queryRunner.query(`ALTER TABLE channels DROP COLUMN position`);
+	}
+
+	public async down(): Promise<void> {
+		// don't care actually, sorry.
+	}
+}