From 97f87ae0fc43eba740d0f66eb533cfe31dfa12af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Fri, 1 Sep 2023 15:19:49 +0200 Subject: [PATCH] refactor(core): Add `addColumns` and `dropColumns` to the migrations DSL (no-changelog) (#7073) --- packages/cli/package.json | 2 +- packages/cli/src/databases/dsl/Column.ts | 2 +- packages/cli/src/databases/dsl/Table.ts | 42 +++++++++++++++++-- packages/cli/src/databases/dsl/index.ts | 7 +++- ...690000000030-RemoveResetPasswordColumns.ts | 24 +++-------- .../common/1690000000040-AddMfaColumns.ts | 34 ++++----------- pnpm-lock.yaml | 41 ++++-------------- 7 files changed, 69 insertions(+), 83 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 2abed7f6ab..d3ba1303f7 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -187,7 +187,7 @@ "swagger-ui-express": "^4.3.0", "syslog-client": "^1.1.1", "typedi": "^0.10.0", - "typeorm": "^0.3.12", + "typeorm": "^0.3.17", "uuid": "^8.3.2", "validator": "13.7.0", "winston": "^3.3.3", diff --git a/packages/cli/src/databases/dsl/Column.ts b/packages/cli/src/databases/dsl/Column.ts index 54442c69d8..48d2191478 100644 --- a/packages/cli/src/databases/dsl/Column.ts +++ b/packages/cli/src/databases/dsl/Column.ts @@ -41,7 +41,7 @@ export class Column { return this; } - timestamp(msPrecision?: number) { + timestamp(msPrecision = 3) { this.type = 'timestamp'; this.length = msPrecision ?? 'auto'; return this; diff --git a/packages/cli/src/databases/dsl/Table.ts b/packages/cli/src/databases/dsl/Table.ts index fb5cef6cdc..b2735cadc9 100644 --- a/packages/cli/src/databases/dsl/Table.ts +++ b/packages/cli/src/databases/dsl/Table.ts @@ -1,5 +1,5 @@ import type { TableForeignKeyOptions, TableIndexOptions } from 'typeorm'; -import { Table, QueryRunner } from 'typeorm'; +import { Table, QueryRunner, TableColumn } from 'typeorm'; import LazyPromise from 'p-lazy'; import { Column } from './Column'; @@ -31,8 +31,8 @@ export class CreateTable extends TableOperation { get withTimestamps() { this.columns.push( - new Column('createdAt').timestamp(3).notNull.default('NOW()'), - new Column('updatedAt').timestamp(3).notNull.default('NOW()'), + new Column('createdAt').timestamp().notNull.default('NOW()'), + new Column('updatedAt').timestamp().notNull.default('NOW()'), ); return this; } @@ -80,3 +80,39 @@ export class DropTable extends TableOperation { return queryRunner.dropTable(`${prefix}${name}`, true); } } + +export class AddColumns extends TableOperation { + constructor( + tableName: string, + protected columns: Column[], + prefix: string, + queryRunner: QueryRunner, + ) { + super(tableName, prefix, queryRunner); + } + + async execute(queryRunner: QueryRunner) { + const { driver } = queryRunner.connection; + const { tableName, prefix, columns } = this; + return queryRunner.addColumns( + `${prefix}${tableName}`, + columns.map((c) => new TableColumn(c.toOptions(driver))), + ); + } +} + +export class DropColumns extends TableOperation { + constructor( + tableName: string, + protected columnNames: string[], + prefix: string, + queryRunner: QueryRunner, + ) { + super(tableName, prefix, queryRunner); + } + + async execute(queryRunner: QueryRunner) { + const { tableName, prefix, columnNames } = this; + return queryRunner.dropColumns(`${prefix}${tableName}`, columnNames); + } +} diff --git a/packages/cli/src/databases/dsl/index.ts b/packages/cli/src/databases/dsl/index.ts index 445a893a38..bd5a06a591 100644 --- a/packages/cli/src/databases/dsl/index.ts +++ b/packages/cli/src/databases/dsl/index.ts @@ -1,6 +1,6 @@ import type { QueryRunner } from 'typeorm'; import { Column } from './Column'; -import { CreateTable, DropTable } from './Table'; +import { AddColumns, CreateTable, DropColumns, DropTable } from './Table'; import { CreateIndex, DropIndex } from './Indices'; export const createSchemaBuilder = (tablePrefix: string, queryRunner: QueryRunner) => ({ @@ -11,6 +11,11 @@ export const createSchemaBuilder = (tablePrefix: string, queryRunner: QueryRunne dropTable: (tableName: string) => new DropTable(tableName, tablePrefix, queryRunner), + addColumns: (tableName: string, columns: Column[]) => + new AddColumns(tableName, columns, tablePrefix, queryRunner), + dropColumns: (tableName: string, columnNames: string[]) => + new DropColumns(tableName, columnNames, tablePrefix, queryRunner), + createIndex: ( tableName: string, columnNames: string[], diff --git a/packages/cli/src/databases/migrations/common/1690000000030-RemoveResetPasswordColumns.ts b/packages/cli/src/databases/migrations/common/1690000000030-RemoveResetPasswordColumns.ts index 92cc2e6529..76a38898d8 100644 --- a/packages/cli/src/databases/migrations/common/1690000000030-RemoveResetPasswordColumns.ts +++ b/packages/cli/src/databases/migrations/common/1690000000030-RemoveResetPasswordColumns.ts @@ -1,26 +1,14 @@ import type { MigrationContext, ReversibleMigration } from '@db/types'; -import { TableColumn } from 'typeorm'; export class RemoveResetPasswordColumns1690000000030 implements ReversibleMigration { - async up({ queryRunner, tablePrefix }: MigrationContext) { - await queryRunner.dropColumns(`${tablePrefix}user`, [ - 'resetPasswordToken', - 'resetPasswordTokenExpiration', - ]); + async up({ schemaBuilder: { dropColumns } }: MigrationContext) { + await dropColumns('user', ['resetPasswordToken', 'resetPasswordTokenExpiration']); } - async down({ queryRunner, tablePrefix }: MigrationContext) { - await queryRunner.addColumns(`${tablePrefix}user`, [ - new TableColumn({ - name: 'resetPasswordToken', - type: 'varchar', - isNullable: true, - }), - new TableColumn({ - name: 'resetPasswordTokenExpiration', - type: 'int', - isNullable: true, - }), + async down({ schemaBuilder: { addColumns, column } }: MigrationContext) { + await addColumns('user', [ + column('resetPasswordToken').varchar(), + column('resetPasswordTokenExpiration').int, ]); } } diff --git a/packages/cli/src/databases/migrations/common/1690000000040-AddMfaColumns.ts b/packages/cli/src/databases/migrations/common/1690000000040-AddMfaColumns.ts index 2c044f6dce..12f1000ffe 100644 --- a/packages/cli/src/databases/migrations/common/1690000000040-AddMfaColumns.ts +++ b/packages/cli/src/databases/migrations/common/1690000000040-AddMfaColumns.ts @@ -1,35 +1,15 @@ import type { MigrationContext, ReversibleMigration } from '@/databases/types'; -import { TableColumn } from 'typeorm'; export class AddMfaColumns1690000000030 implements ReversibleMigration { - async up({ queryRunner, tablePrefix }: MigrationContext) { - await queryRunner.addColumns(`${tablePrefix}user`, [ - new TableColumn({ - name: 'mfaEnabled', - type: 'boolean', - isNullable: false, - default: false, - }), - new TableColumn({ - name: 'mfaSecret', - type: 'text', - isNullable: true, - default: null, - }), - new TableColumn({ - name: 'mfaRecoveryCodes', - type: 'text', - isNullable: true, - default: null, - }), + async up({ schemaBuilder: { addColumns, column } }: MigrationContext) { + await addColumns('user', [ + column('mfaEnabled').bool.notNull.default(false), + column('mfaSecret').text, + column('mfaRecoveryCodes').text, ]); } - async down({ queryRunner, tablePrefix }: MigrationContext) { - await queryRunner.dropColumns(`${tablePrefix}user`, [ - 'mfaEnabled', - 'mfaSecret', - 'mfaRecoveryCodes', - ]); + async down({ schemaBuilder: { dropColumns } }: MigrationContext) { + await dropColumns('user', ['mfaEnabled', 'mfaSecret', 'mfaRecoveryCodes']); } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d9d6a9e029..99fa1c4f20 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -453,8 +453,8 @@ importers: specifier: ^0.10.0 version: 0.10.0(patch_hash=62r6bc2crgimafeyruodhqlgo4) typeorm: - specifier: ^0.3.12 - version: 0.3.12(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6) + specifier: ^0.3.17 + version: 0.3.17(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6) uuid: specifier: ^8.3.2 version: 8.3.2 @@ -10697,17 +10697,11 @@ packages: whatwg-url: 11.0.0 dev: true - /date-fns@2.29.3: - resolution: {integrity: sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==} - engines: {node: '>=0.11'} - dev: false - /date-fns@2.30.0: resolution: {integrity: sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==} engines: {node: '>=0.11'} dependencies: '@babel/runtime': 7.22.6 - dev: true /dateformat@3.0.3: resolution: {integrity: sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==} @@ -11132,7 +11126,6 @@ packages: /dotenv@16.3.1: resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==} engines: {node: '>=12'} - dev: true /dotenv@8.6.0: resolution: {integrity: sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==} @@ -21011,8 +21004,8 @@ packages: dev: false patched: true - /typeorm@0.3.12(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6): - resolution: {integrity: sha512-sYSxBmCf1nJLLTcYtwqZ+lQIRtLPyUoO93rHTOKk9vJCyT4UfRtU7oRsJvfvKP3nnZTD1hzz2SEy2zwPEN6OyA==} + /typeorm@0.3.17(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6): + resolution: {integrity: sha512-UDjUEwIQalO9tWw9O2A4GU+sT3oyoUXheHJy4ft+RFdnRdQctdQ34L9SqE2p7LdwzafHx1maxT+bqXON+Qnmig==} engines: {node: '>= 12.9.0'} hasBin: true peerDependencies: @@ -21021,8 +21014,8 @@ packages: better-sqlite3: ^7.1.2 || ^8.0.0 hdb-pool: ^0.1.6 ioredis: ^5.0.4 - mongodb: ^3.6.0 - mssql: ^7.3.0 + mongodb: ^5.2.0 + mssql: ^9.1.1 mysql2: ^2.2.5 || ^3.0.1 oracledb: ^5.1.0 pg: ^8.5.1 @@ -21074,12 +21067,11 @@ packages: buffer: 6.0.3 chalk: 4.1.2 cli-highlight: 2.1.11 - date-fns: 2.29.3 + date-fns: 2.30.0 debug: 4.3.4(supports-color@8.1.1) - dotenv: 16.0.3 + dotenv: 16.3.1 glob: 8.1.0 ioredis: 5.2.4 - js-yaml: 4.1.0 mkdirp: 2.1.3 mysql2: 2.3.3 pg: 8.8.0 @@ -21088,8 +21080,7 @@ packages: sqlite3: 5.1.6 tslib: 2.6.1 uuid: 9.0.0 - xml2js: 0.5.0 - yargs: 17.6.2 + yargs: 17.7.2 transitivePeerDependencies: - supports-color dev: false @@ -22425,19 +22416,6 @@ packages: yargs-parser: 21.1.1 dev: false - /yargs@17.6.2: - resolution: {integrity: sha512-1/9UrdHjDZc0eOU0HxOHoS78C69UD3JRMvzlJ7S79S2nTaWRA/whGCTV8o9e/N/1Va9YIV7Q4sOxD8VV4pCWOw==} - engines: {node: '>=12'} - dependencies: - cliui: 8.0.1 - escalade: 3.1.1 - get-caller-file: 2.0.5 - require-directory: 2.1.1 - string-width: 4.2.3 - y18n: 5.0.8 - yargs-parser: 21.1.1 - dev: false - /yargs@17.7.2: resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==} engines: {node: '>=12'} @@ -22449,7 +22427,6 @@ packages: string-width: 4.2.3 y18n: 5.0.8 yargs-parser: 21.1.1 - dev: true /yargs@7.1.2: resolution: {integrity: sha512-ZEjj/dQYQy0Zx0lgLMLR8QuaqTihnxirir7EwUHp1Axq4e3+k8jXU5K0VLbNvedv1f4EWtBonDIZm0NUr+jCcA==}