Skip to content

Commit

Permalink
fix(quotes): set id to be integer (#3689)
Browse files Browse the repository at this point in the history
* fix(quotes): set id to be integer

* fix migration

* fix integer range

* fix integer 2
  • Loading branch information
sogehige committed May 9, 2020
1 parent 01cbc3f commit ecf5cbf
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/bot/database/entity/quotes.ts
Expand Up @@ -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;
Expand All @@ -12,7 +12,7 @@ export interface QuotesInterface {
export const Quotes = new EntitySchema<Readonly<Required<QuotesInterface>>>({
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 },
Expand Down
18 changes: 18 additions & 0 deletions 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<void> {
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<void> {
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);
}

}
20 changes: 20 additions & 0 deletions 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<void> {
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<void> {
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);
}

}
28 changes: 28 additions & 0 deletions 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<void> {
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<void> {
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);
}

}
6 changes: 3 additions & 3 deletions src/bot/systems/quotes.ts
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -151,7 +151,7 @@ class Quotes extends System {

@command('!quote')
async main (opts): Promise<CommandResponse[]> {
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 }];
Expand Down
4 changes: 1 addition & 3 deletions src/panel/views/managers/quotes/quotes-edit.vue
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/tests/quotes/remove.js
Expand Up @@ -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 },
];

Expand Down
4 changes: 2 additions & 2 deletions test/tests/quotes/set.js
Expand Up @@ -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()', () => {
Expand Down
5 changes: 2 additions & 3 deletions test/tests/quotes/show.js
Expand Up @@ -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 },
Expand Down

0 comments on commit ecf5cbf

Please sign in to comment.