Skip to content

Commit 325341d

Browse files
committed
fix: migrations with sql_require_primary_key=1
1 parent e11d2b4 commit 325341d

6 files changed

+47
-14
lines changed

backend/src/migrations/1544877081073-CreateSlowmodeTables.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
1010
name: "guild_id",
1111
type: "bigint",
1212
unsigned: true,
13+
isPrimary: true,
1314
},
1415
{
1516
name: "channel_id",
1617
type: "bigint",
1718
unsigned: true,
19+
isPrimary: true,
1820
},
1921
{
2022
name: "slowmode_seconds",
@@ -25,7 +27,6 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
2527
indices: [],
2628
}),
2729
);
28-
await queryRunner.createPrimaryKey("slowmode_channels", ["guild_id", "channel_id"]);
2930

3031
await queryRunner.createTable(
3132
new Table({
@@ -35,16 +36,19 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
3536
name: "guild_id",
3637
type: "bigint",
3738
unsigned: true,
39+
isPrimary: true,
3840
},
3941
{
4042
name: "channel_id",
4143
type: "bigint",
4244
unsigned: true,
45+
isPrimary: true,
4346
},
4447
{
4548
name: "user_id",
4649
type: "bigint",
4750
unsigned: true,
51+
isPrimary: true,
4852
},
4953
{
5054
name: "expires_at",
@@ -58,7 +62,6 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
5862
],
5963
}),
6064
);
61-
await queryRunner.createPrimaryKey("slowmode_users", ["guild_id", "channel_id", "user_id"]);
6265
}
6366

6467
public async down(queryRunner: QueryRunner): Promise<any> {

backend/src/migrations/1544887946307-CreateStarboardTable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ export class CreateStarboardTable1544887946307 implements MigrationInterface {
6161
name: "starboard_id",
6262
type: "int",
6363
unsigned: true,
64+
isPrimary: true,
6465
},
6566
{
6667
name: "message_id",
6768
type: "bigint",
6869
unsigned: true,
70+
isPrimary: true,
6971
},
7072
{
7173
name: "starboard_message_id",
@@ -75,7 +77,6 @@ export class CreateStarboardTable1544887946307 implements MigrationInterface {
7577
],
7678
}),
7779
);
78-
await queryRunner.createPrimaryKey("starboard_messages", ["starboard_id", "message_id"]);
7980
}
8081

8182
public async down(queryRunner: QueryRunner): Promise<any> {

backend/src/migrations/1547290549908-CreateAutoReactionsTable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ export class CreateAutoReactionsTable1547290549908 implements MigrationInterface
1010
name: "guild_id",
1111
type: "bigint",
1212
unsigned: true,
13+
isPrimary: true,
1314
},
1415
{
1516
name: "channel_id",
1617
type: "bigint",
1718
unsigned: true,
19+
isPrimary: true,
1820
},
1921
{
2022
name: "reactions",
@@ -23,7 +25,6 @@ export class CreateAutoReactionsTable1547290549908 implements MigrationInterface
2325
],
2426
}),
2527
);
26-
await queryRunner.createPrimaryKey("auto_reactions", ["guild_id", "channel_id"]);
2728
}
2829

2930
public async down(queryRunner: QueryRunner): Promise<any> {

backend/src/migrations/1558804449510-CreateDashboardUsersTable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ export class CreateDashboardUsersTable1558804449510 implements MigrationInterfac
99
{
1010
name: "guild_id",
1111
type: "bigint",
12+
isPrimary: true,
1213
},
1314
{
1415
name: "user_id",
1516
type: "bigint",
17+
isPrimary: true,
1618
},
1719
{
1820
name: "username",
@@ -28,7 +30,6 @@ export class CreateDashboardUsersTable1558804449510 implements MigrationInterfac
2830
}),
2931
);
3032

31-
await queryRunner.createPrimaryKey("dashboard_users", ["guild_id", "user_id"]);
3233
await queryRunner.createIndex(
3334
"dashboard_users",
3435
new TableIndex({

backend/src/migrations/1573158035867-AddTypeAndPermissionsToApiPermissions.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { MigrationInterface, QueryRunner, TableColumn, TableIndex } from "typeor
22

33
export class AddTypeAndPermissionsToApiPermissions1573158035867 implements MigrationInterface {
44
public async up(queryRunner: QueryRunner): Promise<any> {
5-
try {
6-
await queryRunner.dropPrimaryKey("api_permissions");
7-
} catch {} // eslint-disable-line no-empty
5+
// Edge case: Since we're dropping the primary key temporarily, we need to disable the sql_require_primary_key setting if it's enabled
6+
// This is restored at the end of the migration
7+
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
8+
await queryRunner.query("SET SESSION sql_require_primary_key=0");
89

9-
const table = (await queryRunner.getTable("api_permissions"))!;
10-
if (table.indices.length) {
11-
await queryRunner.dropIndex("api_permissions", table.indices[0]);
12-
}
10+
await queryRunner.dropPrimaryKey("api_permissions");
11+
12+
// We can't use a TableIndex object in dropIndex directly as the table name is included in the generated index name
13+
// and the table name has changed since the original index was created
14+
const originalIndexName = queryRunner.connection.namingStrategy.indexName("dashboard_users", ["user_id"]);
15+
await queryRunner.dropIndex("api_permissions", originalIndexName);
1316

1417
await queryRunner.addColumn(
1518
"api_permissions",
@@ -46,10 +49,20 @@ export class AddTypeAndPermissionsToApiPermissions1573158035867 implements Migra
4649
columnNames: ["type", "target_id"],
4750
}),
4851
);
52+
53+
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
4954
}
5055

5156
public async down(queryRunner: QueryRunner): Promise<any> {
52-
await queryRunner.dropIndex("api_permissions", "IDX_e06d750f13e6a4b4d3d6b847a9");
57+
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
58+
await queryRunner.query("SET SESSION sql_require_primary_key=0");
59+
60+
await queryRunner.dropIndex(
61+
"api_permissions",
62+
new TableIndex({
63+
columnNames: ["type", "target_id"],
64+
}),
65+
);
5366

5467
await queryRunner.dropColumn("api_permissions", "permissions");
5568

@@ -76,5 +89,7 @@ export class AddTypeAndPermissionsToApiPermissions1573158035867 implements Migra
7689
);
7790

7891
await queryRunner.createPrimaryKey("api_permissions", ["guild_id", "user_id"]);
92+
93+
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
7994
}
8095
}

backend/src/migrations/1573248462469-MoveStarboardsToConfig.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { MigrationInterface, QueryRunner, Table, TableColumn } from "typeorm";
22

33
export class MoveStarboardsToConfig1573248462469 implements MigrationInterface {
44
public async up(queryRunner: QueryRunner): Promise<any> {
5-
// Create the new column for the channels id
5+
// Edge case: Since we're dropping the primary key temporarily, we need to disable the sql_require_primary_key setting if it's enabled
6+
// This is restored at the end of the migration
7+
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
8+
await queryRunner.query("SET SESSION sql_require_primary_key=0");
9+
10+
// Create a new column for the channel's id
611
const chanid_column = new TableColumn({
712
name: "starboard_channel_id",
813
type: "bigint",
@@ -33,9 +38,14 @@ export class MoveStarboardsToConfig1573248462469 implements MigrationInterface {
3338
await queryRunner.createPrimaryKey("starboard_messages", ["starboard_message_id"]);
3439
// Finally, drop the starboards channel as it is now obsolete
3540
await queryRunner.dropTable("starboards", true);
41+
42+
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
3643
}
3744

3845
public async down(queryRunner: QueryRunner): Promise<any> {
46+
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
47+
await queryRunner.query("SET SESSION sql_require_primary_key=0");
48+
3949
await queryRunner.dropColumn("starboard_messages", "starboard_channel_id");
4050
await queryRunner.dropColumn("starboard_messages", "guild_id");
4151

@@ -99,5 +109,7 @@ export class MoveStarboardsToConfig1573248462469 implements MigrationInterface {
99109
],
100110
}),
101111
);
112+
113+
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
102114
}
103115
}

0 commit comments

Comments
 (0)