summary refs log tree commit diff
path: root/src/util/migration/mariadb/1696420827239-guildChannelOrdering.ts
blob: 960feb9b59fd7168300547b3cf13b7af77c60324 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 channel_ordering 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 channel_ordering = ? 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.
	}
}