3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-14 21:31:50 +00:00

fix: migrations with sql_require_primary_key=1

This commit is contained in:
Dragory 2024-04-06 11:49:45 +00:00
parent e11d2b48e4
commit 325341dced
No known key found for this signature in database
6 changed files with 47 additions and 14 deletions

View file

@ -10,11 +10,13 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
name: "guild_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "channel_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "slowmode_seconds",
@ -25,7 +27,6 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
indices: [],
}),
);
await queryRunner.createPrimaryKey("slowmode_channels", ["guild_id", "channel_id"]);
await queryRunner.createTable(
new Table({
@ -35,16 +36,19 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
name: "guild_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "channel_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "user_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "expires_at",
@ -58,7 +62,6 @@ export class CreateSlowmodeTables1544877081073 implements MigrationInterface {
],
}),
);
await queryRunner.createPrimaryKey("slowmode_users", ["guild_id", "channel_id", "user_id"]);
}
public async down(queryRunner: QueryRunner): Promise<any> {

View file

@ -61,11 +61,13 @@ export class CreateStarboardTable1544887946307 implements MigrationInterface {
name: "starboard_id",
type: "int",
unsigned: true,
isPrimary: true,
},
{
name: "message_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "starboard_message_id",
@ -75,7 +77,6 @@ export class CreateStarboardTable1544887946307 implements MigrationInterface {
],
}),
);
await queryRunner.createPrimaryKey("starboard_messages", ["starboard_id", "message_id"]);
}
public async down(queryRunner: QueryRunner): Promise<any> {

View file

@ -10,11 +10,13 @@ export class CreateAutoReactionsTable1547290549908 implements MigrationInterface
name: "guild_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "channel_id",
type: "bigint",
unsigned: true,
isPrimary: true,
},
{
name: "reactions",
@ -23,7 +25,6 @@ export class CreateAutoReactionsTable1547290549908 implements MigrationInterface
],
}),
);
await queryRunner.createPrimaryKey("auto_reactions", ["guild_id", "channel_id"]);
}
public async down(queryRunner: QueryRunner): Promise<any> {

View file

@ -9,10 +9,12 @@ export class CreateDashboardUsersTable1558804449510 implements MigrationInterfac
{
name: "guild_id",
type: "bigint",
isPrimary: true,
},
{
name: "user_id",
type: "bigint",
isPrimary: true,
},
{
name: "username",
@ -28,7 +30,6 @@ export class CreateDashboardUsersTable1558804449510 implements MigrationInterfac
}),
);
await queryRunner.createPrimaryKey("dashboard_users", ["guild_id", "user_id"]);
await queryRunner.createIndex(
"dashboard_users",
new TableIndex({

View file

@ -2,14 +2,17 @@ import { MigrationInterface, QueryRunner, TableColumn, TableIndex } from "typeor
export class AddTypeAndPermissionsToApiPermissions1573158035867 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
try {
await queryRunner.dropPrimaryKey("api_permissions");
} catch {} // eslint-disable-line no-empty
// Edge case: Since we're dropping the primary key temporarily, we need to disable the sql_require_primary_key setting if it's enabled
// This is restored at the end of the migration
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
await queryRunner.query("SET SESSION sql_require_primary_key=0");
const table = (await queryRunner.getTable("api_permissions"))!;
if (table.indices.length) {
await queryRunner.dropIndex("api_permissions", table.indices[0]);
}
await queryRunner.dropPrimaryKey("api_permissions");
// We can't use a TableIndex object in dropIndex directly as the table name is included in the generated index name
// and the table name has changed since the original index was created
const originalIndexName = queryRunner.connection.namingStrategy.indexName("dashboard_users", ["user_id"]);
await queryRunner.dropIndex("api_permissions", originalIndexName);
await queryRunner.addColumn(
"api_permissions",
@ -46,10 +49,20 @@ export class AddTypeAndPermissionsToApiPermissions1573158035867 implements Migra
columnNames: ["type", "target_id"],
}),
);
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
}
public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.dropIndex("api_permissions", "IDX_e06d750f13e6a4b4d3d6b847a9");
const sqlRequirePrimaryKey = (await queryRunner.query("SELECT @@sql_require_primary_key AS value"))[0].value;
await queryRunner.query("SET SESSION sql_require_primary_key=0");
await queryRunner.dropIndex(
"api_permissions",
new TableIndex({
columnNames: ["type", "target_id"],
}),
);
await queryRunner.dropColumn("api_permissions", "permissions");
@ -76,5 +89,7 @@ export class AddTypeAndPermissionsToApiPermissions1573158035867 implements Migra
);
await queryRunner.createPrimaryKey("api_permissions", ["guild_id", "user_id"]);
await queryRunner.query(`SET SESSION sql_require_primary_key=${sqlRequirePrimaryKey}`);
}
}

View file

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