3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 20:35:02 +00:00

Switch from Knex to TypeORM. Update Knub.

This commit is contained in:
Dragory 2018-10-26 06:41:20 +03:00
parent e3ff4cef45
commit f9c16263ae
49 changed files with 1192 additions and 1395 deletions

View file

@ -0,0 +1,112 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class CreatePreTypeORMTables1540519249973 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`archives\` (
\`id\` VARCHAR(36) NOT NULL,
\`guild_id\` VARCHAR(20) NOT NULL,
\`body\` MEDIUMTEXT NOT NULL,
\`created_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
\`expires_at\` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (\`id\`)
)
COLLATE='utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`cases\` (
\`id\` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
\`guild_id\` BIGINT(20) UNSIGNED NOT NULL,
\`case_number\` INT(10) UNSIGNED NOT NULL,
\`user_id\` BIGINT(20) UNSIGNED NOT NULL,
\`user_name\` VARCHAR(128) NOT NULL,
\`mod_id\` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
\`mod_name\` VARCHAR(128) NULL DEFAULT NULL,
\`type\` INT(10) UNSIGNED NOT NULL,
\`audit_log_id\` BIGINT(20) NULL DEFAULT NULL,
\`created_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (\`id\`),
UNIQUE INDEX \`mod_actions_guild_id_case_number_unique\` (\`guild_id\`, \`case_number\`),
UNIQUE INDEX \`mod_actions_audit_log_id_unique\` (\`audit_log_id\`),
INDEX \`mod_actions_user_id_index\` (\`user_id\`),
INDEX \`mod_actions_mod_id_index\` (\`mod_id\`),
INDEX \`mod_actions_created_at_index\` (\`created_at\`)
)
COLLATE = 'utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`case_notes\` (
\`id\` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
\`case_id\` INT(10) UNSIGNED NOT NULL,
\`mod_id\` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
\`mod_name\` VARCHAR(128) NULL DEFAULT NULL,
\`body\` TEXT NOT NULL,
\`created_at\` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (\`id\`),
INDEX \`mod_action_notes_mod_action_id_index\` (\`case_id\`),
INDEX \`mod_action_notes_mod_id_index\` (\`mod_id\`),
INDEX \`mod_action_notes_created_at_index\` (\`created_at\`)
)
COLLATE = 'utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`mutes\` (
\`guild_id\` BIGINT(20) UNSIGNED NOT NULL,
\`user_id\` BIGINT(20) UNSIGNED NOT NULL,
\`created_at\` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
\`expires_at\` DATETIME NULL DEFAULT NULL,
\`case_id\` INT(10) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (\`guild_id\`, \`user_id\`),
INDEX \`mutes_expires_at_index\` (\`expires_at\`),
INDEX \`mutes_case_id_foreign\` (\`case_id\`),
CONSTRAINT \`mutes_case_id_foreign\` FOREIGN KEY (\`case_id\`) REFERENCES \`cases\` (\`id\`)
ON DELETE SET NULL
)
COLLATE = 'utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`persisted_data\` (
\`guild_id\` VARCHAR(20) NOT NULL,
\`user_id\` VARCHAR(20) NOT NULL,
\`roles\` VARCHAR(1024) NULL DEFAULT NULL,
\`nickname\` VARCHAR(255) NULL DEFAULT NULL,
\`is_voice_muted\` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (\`guild_id\`, \`user_id\`)
)
COLLATE = 'utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`reaction_roles\` (
\`guild_id\` VARCHAR(20) NOT NULL,
\`channel_id\` VARCHAR(20) NOT NULL,
\`message_id\` VARCHAR(20) NOT NULL,
\`emoji\` VARCHAR(20) NOT NULL,
\`role_id\` VARCHAR(20) NOT NULL,
PRIMARY KEY (\`guild_id\`, \`channel_id\`, \`message_id\`, \`emoji\`),
INDEX \`reaction_roles_message_id_emoji_index\` (\`message_id\`, \`emoji\`)
)
COLLATE = 'utf8mb4_general_ci'
`);
await queryRunner.query(`
CREATE TABLE IF NOT EXISTS \`tags\` (
\`guild_id\` BIGINT(20) UNSIGNED NOT NULL,
\`tag\` VARCHAR(64) NOT NULL,
\`user_id\` BIGINT(20) UNSIGNED NOT NULL,
\`body\` TEXT NOT NULL,
\`created_at\` DATETIME NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (\`guild_id\`, \`tag\`)
)
COLLATE = 'utf8mb4_general_ci'
`);
}
public async down(queryRunner: QueryRunner): Promise<any> {
// No down function since we're migrating (hehe) from another migration system (knex)
}
}

View file

@ -1,34 +0,0 @@
exports.up = async function(knex) {
if (! await knex.schema.hasTable('mod_actions')) {
await knex.schema.createTable('mod_actions', table => {
table.increments('id');
table.bigInteger('guild_id').unsigned().notNullable();
table.integer('case_number').unsigned().notNullable();
table.bigInteger('user_id').index().unsigned().notNullable();
table.string('user_name', 128).notNullable();
table.bigInteger('mod_id').index().unsigned().nullable().defaultTo(null);
table.string('mod_name', 128).nullable().defaultTo(null);
table.integer('action_type').unsigned().notNullable();
table.bigInteger('audit_log_id').unique().nullable().defaultTo(null);
table.dateTime('created_at').index().defaultTo(knex.raw('NOW()')).notNullable();
table.unique(['guild_id', 'case_number']);
});
}
if (! await knex.schema.hasTable('mod_action_notes')) {
await knex.schema.createTable('mod_action_notes', table => {
table.increments('id');
table.integer('mod_action_id').unsigned().notNullable().index().references('id').inTable('mod_actions').onDelete('CASCADE');
table.bigInteger('mod_id').index().unsigned().nullable().defaultTo(null);
table.string('mod_name', 128).nullable().defaultTo(null);
table.text('body').notNullable();
table.dateTime('created_at').index().defaultTo(knex.raw('NOW()')).notNullable();
});
}
};
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('mod_action_notes');
await knex.schema.dropTableIfExists('mod_actions');
};

View file

@ -1,17 +0,0 @@
exports.up = async function(knex) {
if (! await knex.schema.hasTable('mutes')) {
await knex.schema.createTable('mutes', table => {
table.bigInteger('guild_id').unsigned().notNullable();
table.bigInteger('user_id').unsigned().notNullable();
table.dateTime('created_at').defaultTo(knex.raw('NOW()'));
table.dateTime('expires_at').nullable().defaultTo(null);
table.primary(['guild_id', 'user_id']);
table.index(['expires_at']);
});
}
};
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('mutes');
};

View file

@ -1,21 +0,0 @@
exports.up = async function(knex) {
await knex.schema.renameTable('mod_actions', 'cases');
await knex.schema.renameTable('mod_action_notes', 'case_notes');
await knex.schema.table('cases', table => {
table.renameColumn('action_type', 'type');
});
await knex.schema.table('case_notes', table => {
table.renameColumn('mod_action_id', 'case_id');
});
};
exports.down = async function(knex) {
await knex.schema.table('cases', table => {
table.renameColumn('type', 'action_type');
});
await knex.schema.table('case_notes', table => {
table.renameColumn('case_id', 'mod_action_id');
});
await knex.schema.renameTable('cases', 'mod_actions');
await knex.schema.renameTable('case_notes', 'mod_action_notes');
};

View file

@ -1,18 +0,0 @@
exports.up = async function(knex, Promise) {
if (! await knex.schema.hasTable('reaction_roles')) {
await knex.schema.createTable('reaction_roles', table => {
table.string('guild_id', 20).notNullable();
table.string('channel_id', 20).notNullable();
table.string('message_id', 20).notNullable();
table.string('emoji', 20).notNullable();
table.string('role_id', 20).notNullable();
table.primary(['guild_id', 'channel_id', 'message_id', 'emoji']);
table.index(['message_id', 'emoji']);
});
}
};
exports.down = async function(knex, Promise) {
await knex.schema.dropTableIfExists('reaction_roles');
};

View file

@ -1,17 +0,0 @@
exports.up = async function(knex, Promise) {
if (! await knex.schema.hasTable('persisted_data')) {
await knex.schema.createTable('persisted_data', table => {
table.string('guild_id', 20).notNullable();
table.string('user_id', 20).notNullable();
table.string('roles', 1024).nullable().defaultTo(null);
table.string('nickname', 255).nullable().defaultTo(null);
table.integer('is_voice_muted').notNullable().defaultTo(0);
table.primary(['guild_id', 'user_id']);
});
}
};
exports.down = async function(knex, Promise) {
await knex.schema.dropTableIfExists('persisted_data');
};

View file

@ -1,15 +0,0 @@
exports.up = async function(knex, Promise) {
if (! await knex.schema.hasTable('spam_logs')) {
await knex.schema.createTable('spam_logs', table => {
table.string('id', 36).notNullable().primary();
table.string('guild_id', 20).notNullable();
table.text('body', 'mediumtext').notNullable();
table.dateTime('created_at').defaultTo(knex.raw('NOW()')).notNullable();
table.dateTime('expires_at').nullable();
});
}
};
exports.down = async function(knex, Promise) {
await knex.schema.dropTableIfExists('spam_logs');
};

View file

@ -1,12 +0,0 @@
exports.up = async function(knex, Promise) {
await knex.schema.table('mutes', table => {
table.integer('case_id').unsigned().nullable().defaultTo(null).after('user_id').references('id').inTable('cases').onDelete('SET NULL');
});
};
exports.down = async function(knex, Promise) {
await knex.schema.table('mutes', table => {
table.dropForeign('case_id');
table.dropColumn('case_id');
});
};

View file

@ -1,17 +0,0 @@
exports.up = async function(knex) {
if (! await knex.schema.hasTable('tags')) {
await knex.schema.createTable('tags', table => {
table.bigInteger('guild_id').unsigned().notNullable();
table.string('tag', 64).notNullable();
table.bigInteger('user_id').unsigned().notNullable();
table.text('body').notNullable();
table.dateTime('created_at').defaultTo(knex.raw('NOW()'));
table.primary(['guild_id', 'tag']);
});
}
};
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('tags');
};

View file

@ -1,7 +0,0 @@
exports.up = async function(knex) {
await knex.schema.renameTable('spam_logs', 'archives');
};
exports.down = async function(knex) {
await knex.schema.renameTable('archives', 'spam_logs');
};