From ecf5cbf6e01c6ec535b3f44f1ba7b079f4c68e7e Mon Sep 17 00:00:00 2001 From: sogehige Date: Sat, 9 May 2020 20:28:29 +0200 Subject: [PATCH] fix(quotes): set id to be integer (#3689) * fix(quotes): set id to be integer * fix migration * fix integer range * fix integer 2 --- src/bot/database/entity/quotes.ts | 4 +-- .../mysql/1588973421498-quotesIdToInt.ts | 18 ++++++++++++ .../postgres/1588971215782-quotesIdToInt.ts | 20 +++++++++++++ .../sqlite/1588962416420-quotesIdToInt.ts | 28 +++++++++++++++++++ src/bot/systems/quotes.ts | 6 ++-- .../views/managers/quotes/quotes-edit.vue | 4 +-- test/tests/quotes/remove.js | 2 +- test/tests/quotes/set.js | 4 +-- test/tests/quotes/show.js | 5 ++-- 9 files changed, 77 insertions(+), 14 deletions(-) create mode 100644 src/bot/database/migration/mysql/1588973421498-quotesIdToInt.ts create mode 100644 src/bot/database/migration/postgres/1588971215782-quotesIdToInt.ts create mode 100644 src/bot/database/migration/sqlite/1588962416420-quotesIdToInt.ts diff --git a/src/bot/database/entity/quotes.ts b/src/bot/database/entity/quotes.ts index eb239d11c1d..9dd22646a81 100644 --- a/src/bot/database/entity/quotes.ts +++ b/src/bot/database/entity/quotes.ts @@ -2,7 +2,7 @@ import { EntitySchema } from 'typeorm'; import { ColumnNumericTransformer } from './_transformer'; export interface QuotesInterface { - id?: string; + id?: number; tags: string[]; quote: string; quotedBy: number; @@ -12,7 +12,7 @@ export interface QuotesInterface { export const Quotes = new EntitySchema>>({ name: 'quotes', columns: { - id: { type: 'uuid', primary: true, generated: 'uuid' }, + id: { type: 'int', primary: true, generated: 'increment' }, tags: { type: 'simple-array' }, quote: { type: String }, quotedBy: { type: Number }, diff --git a/src/bot/database/migration/mysql/1588973421498-quotesIdToInt.ts b/src/bot/database/migration/mysql/1588973421498-quotesIdToInt.ts new file mode 100644 index 00000000000..18b37361056 --- /dev/null +++ b/src/bot/database/migration/mysql/1588973421498-quotesIdToInt.ts @@ -0,0 +1,18 @@ +import {MigrationInterface, QueryRunner} from 'typeorm'; + +export class quotesIdToInt1588973421498 implements MigrationInterface { + name = 'quotesIdToInt1588973421498'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE `quotes` DROP PRIMARY KEY', undefined); + await queryRunner.query('ALTER TABLE `quotes` DROP COLUMN `id`', undefined); + await queryRunner.query('ALTER TABLE `quotes` ADD `id` int NOT NULL PRIMARY KEY AUTO_INCREMENT', undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query('ALTER TABLE `quotes` DROP COLUMN `id`', undefined); + await queryRunner.query('ALTER TABLE `quotes` ADD `id` varchar(36) NOT NULL', undefined); + await queryRunner.query('ALTER TABLE `quotes` ADD PRIMARY KEY (`id`)', undefined); + } + +} diff --git a/src/bot/database/migration/postgres/1588971215782-quotesIdToInt.ts b/src/bot/database/migration/postgres/1588971215782-quotesIdToInt.ts new file mode 100644 index 00000000000..870c16f4689 --- /dev/null +++ b/src/bot/database/migration/postgres/1588971215782-quotesIdToInt.ts @@ -0,0 +1,20 @@ +import {MigrationInterface, QueryRunner} from 'typeorm'; + +export class quotesIdToInt1588971215782 implements MigrationInterface { + name = 'quotesIdToInt1588971215782'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "quotes" DROP CONSTRAINT "PK_99a0e8bcbcd8719d3a41f23c263"`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" DROP COLUMN "id"`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" ADD "id" SERIAL NOT NULL`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" ADD CONSTRAINT "PK_99a0e8bcbcd8719d3a41f23c263" PRIMARY KEY ("id")`, undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "quotes" DROP CONSTRAINT "PK_99a0e8bcbcd8719d3a41f23c263"`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" DROP COLUMN "id"`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" ADD "id" uuid NOT NULL DEFAULT uuid_generate_v4()`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" ADD CONSTRAINT "PK_99a0e8bcbcd8719d3a41f23c263" PRIMARY KEY ("id")`, undefined); + } + +} diff --git a/src/bot/database/migration/sqlite/1588962416420-quotesIdToInt.ts b/src/bot/database/migration/sqlite/1588962416420-quotesIdToInt.ts new file mode 100644 index 00000000000..c43b8dddef6 --- /dev/null +++ b/src/bot/database/migration/sqlite/1588962416420-quotesIdToInt.ts @@ -0,0 +1,28 @@ +import {MigrationInterface, QueryRunner} from 'typeorm'; + +export class quotesIdToInt1588962416420 implements MigrationInterface { + name = 'quotesIdToInt1588962416420'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "temporary_quotes" ("id" varchar PRIMARY KEY NOT NULL, "tags" text NOT NULL, "quote" varchar NOT NULL, "quotedBy" integer NOT NULL, "createdAt" bigint NOT NULL)`, undefined); + await queryRunner.query(`INSERT INTO "temporary_quotes"("id", "tags", "quote", "quotedBy", "createdAt") SELECT "id", "tags", "quote", "quotedBy", "createdAt" FROM "quotes"`, undefined); + await queryRunner.query(`DROP TABLE "quotes"`, undefined); + await queryRunner.query(`ALTER TABLE "temporary_quotes" RENAME TO "quotes"`, undefined); + await queryRunner.query(`CREATE TABLE "temporary_quotes" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "tags" text NOT NULL, "quote" varchar NOT NULL, "quotedBy" integer NOT NULL, "createdAt" bigint NOT NULL)`, undefined); + await queryRunner.query(`INSERT INTO "temporary_quotes"("tags", "quote", "quotedBy", "createdAt") SELECT "tags", "quote", "quotedBy", "createdAt" FROM "quotes"`, undefined); + await queryRunner.query(`DROP TABLE "quotes"`, undefined); + await queryRunner.query(`ALTER TABLE "temporary_quotes" RENAME TO "quotes"`, undefined); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "quotes" RENAME TO "temporary_quotes"`, undefined); + await queryRunner.query(`CREATE TABLE "quotes" ("id" varchar PRIMARY KEY NOT NULL, "tags" text NOT NULL, "quote" varchar NOT NULL, "quotedBy" integer NOT NULL, "createdAt" bigint NOT NULL)`, undefined); + await queryRunner.query(`INSERT INTO "quotes"("id", "tags", "quote", "quotedBy", "createdAt") SELECT "id", "tags", "quote", "quotedBy", "createdAt" FROM "temporary_quotes"`, undefined); + await queryRunner.query(`DROP TABLE "temporary_quotes"`, undefined); + await queryRunner.query(`ALTER TABLE "quotes" RENAME TO "temporary_quotes"`, undefined); + await queryRunner.query(`CREATE TABLE "quotes" ("id" varchar PRIMARY KEY NOT NULL, "tags" text NOT NULL, "quote" varchar NOT NULL, "quotedBy" integer NOT NULL, "createdAt" bigint NOT NULL)`, undefined); + await queryRunner.query(`INSERT INTO "quotes"("tags", "quote", "quotedBy", "createdAt") SELECT "tags", "quote", "quotedBy", "createdAt" FROM "temporary_quotes"`, undefined); + await queryRunner.query(`DROP TABLE "temporary_quotes"`, undefined); + } + +} diff --git a/src/bot/systems/quotes.ts b/src/bot/systems/quotes.ts index 3211ae213f0..4dc32231097 100644 --- a/src/bot/systems/quotes.ts +++ b/src/bot/systems/quotes.ts @@ -93,7 +93,7 @@ class Quotes extends System { if (opts.parameters.length === 0) { throw new Error(); } - const id = new Expects(opts.parameters).argument({ type: 'uuid', name: 'id' }).toArray()[0]; + const id = new Expects(opts.parameters).argument({ type: Number, name: 'id' }).toArray()[0]; const item = await getRepository(QuotesEntity).findOne({id}); if (!item) { @@ -117,7 +117,7 @@ class Quotes extends System { if (opts.parameters.length === 0) { throw new Error(); } - const [id, tag] = new Expects(opts.parameters).argument({ type: 'uuid', name: 'id' }).argument({ name: 'tag', multi: true, delimiter: '' }).toArray(); + const [id, tag] = new Expects(opts.parameters).argument({ type: Number, name: 'id' }).argument({ name: 'tag', multi: true, delimiter: '' }).toArray(); const quote = await getRepository(QuotesEntity).findOne({id}); if (quote) { @@ -151,7 +151,7 @@ class Quotes extends System { @command('!quote') async main (opts): Promise { - const [id, tag] = new Expects(opts.parameters).argument({ type: 'uuid', name: 'id', optional: true }).argument({ name: 'tag', optional: true, multi: true, delimiter: '' }).toArray(); + const [id, tag] = new Expects(opts.parameters).argument({ type: Number, name: 'id', optional: true }).argument({ name: 'tag', optional: true, multi: true, delimiter: '' }).toArray(); if (_.isNil(id) && _.isNil(tag) || id === '-tag') { const response = prepare('systems.quotes.show.error.no-parameters', { command: opts.command }); return [{ response, ...opts }]; diff --git a/src/panel/views/managers/quotes/quotes-edit.vue b/src/panel/views/managers/quotes/quotes-edit.vue index da91b6daeb4..8363e725518 100644 --- a/src/panel/views/managers/quotes/quotes-edit.vue +++ b/src/panel/views/managers/quotes/quotes-edit.vue @@ -79,8 +79,6 @@ import { getSocket } from 'src/panel/helpers/socket'; import { Validations } from 'vuelidate-property-decorators'; import { required } from 'vuelidate/lib/validators'; -import { v4 as uuid } from 'uuid'; - import { getUsernameById } from '../../../helpers/userById'; import { QuotesInterface } from 'src/bot/database/entity/quotes'; @@ -111,7 +109,7 @@ export default class QuotesEdit extends Vue { pending: boolean = false; item: QuotesInterface = { - id: uuid(), + id: undefined, createdAt: Date.now(), tags: [], quotedBy: 0, diff --git a/test/tests/quotes/remove.js b/test/tests/quotes/remove.js index 08dc48aa323..cb92ea721dc 100644 --- a/test/tests/quotes/remove.js +++ b/test/tests/quotes/remove.js @@ -21,7 +21,7 @@ const tests = [ { sender: owner, parameters: '', shouldFail: true }, { sender: owner, parameters: '-id', shouldFail: true }, { sender: owner, parameters: '-id a', id: 'a', shouldFail: true, exist: false }, - { sender: owner, parameters: '-id cb286f64-833d-497f-b5d9-a2dbd7645147', id: 'cb286f64-833d-497f-b5d9-a2dbd7645147', shouldFail: false, exist: false }, + { sender: owner, parameters: '-id 99999', id: 99999, shouldFail: false, exist: false }, { sender: owner, parameters: '-id $id', id: 1, shouldFail: false, exist: true }, ]; diff --git a/test/tests/quotes/set.js b/test/tests/quotes/set.js index 65abc4def0a..1d039bfc706 100644 --- a/test/tests/quotes/set.js +++ b/test/tests/quotes/set.js @@ -24,8 +24,8 @@ const tests = [ { sender: owner, parameters: '-id $id -tag ipsum, dolor', id: 1, tags: 'ipsum, dolor', shouldFail: false, exist: true }, { sender: owner, parameters: '-tag ipsum, dolor -id $id', id: 1, tags: 'ipsum, dolor', shouldFail: false, exist: true }, - { sender: owner, parameters: '-id ca0cfbe4-2cc2-449b-8b9d-67bb34b21701 -tag ipsum, dolor', id: 'ca0cfbe4-2cc2-449b-8b9d-67bb34b21701', tags: 'ipsum, dolor', shouldFail: false, exist: false }, - { sender: owner, parameters: '-tag ipsum, dolor -id ca0cfbe4-2cc2-449b-8b9d-67bb34b21701', id: 'ca0cfbe4-2cc2-449b-8b9d-67bb34b21701', tags: 'ipsum, dolor', shouldFail: false, exist: false }, + { sender: owner, parameters: '-id 99999 -tag ipsum, dolor', id: 99999, tags: 'ipsum, dolor', shouldFail: false, exist: false }, + { sender: owner, parameters: '-tag ipsum, dolor -id 99999', id: 99999, tags: 'ipsum, dolor', shouldFail: false, exist: false }, ]; describe('Quotes - set()', () => { diff --git a/test/tests/quotes/show.js b/test/tests/quotes/show.js index b2af150b2e7..c0b801bb0a2 100644 --- a/test/tests/quotes/show.js +++ b/test/tests/quotes/show.js @@ -24,11 +24,10 @@ const tests = [ { sender: owner, parameters: '-tag -id', shouldFail: true, error: 'systems.quotes.show.error.no-parameters' }, { sender: owner, parameters: '-id -tag', shouldFail: true, error: 'systems.quotes.show.error.no-parameters' }, - { sender: owner, parameters: '-id $id', id: 1, tag: 'general', shouldFail: false, exist: true }, { sender: owner, parameters: '-id $id -tag', id: 1, tag: 'general', shouldFail: false, exist: true }, - { sender: owner, parameters: '-id 732ff1bd-711f-457a-bef9-8a83eb8fc4b0', id: '732ff1bd-711f-457a-bef9-8a83eb8fc4b0', tag: 'general', shouldFail: false, exist: false }, - { sender: owner, parameters: '-id 732ff1bd-711f-457a-bef9-8a83eb8fc4b0 -tag', id: '732ff1bd-711f-457a-bef9-8a83eb8fc4b0', tag: 'general', shouldFail: false, exist: false }, + { sender: owner, parameters: '-id 99999', id: 99999, tag: 'general', shouldFail: false, exist: false }, + { sender: owner, parameters: '-id 99999 -tag', id: 99999, tag: 'general', shouldFail: false, exist: false }, { sender: owner, parameters: '-tag lorem ipsum', id: 1, tag: 'lorem ipsum', shouldFail: false, exist: true }, { sender: owner, parameters: '-tag general', id: 1, tag: 'general', shouldFail: false, exist: false },