refactor(core): Add addColumns and dropColumns to the migrations DSL (no-changelog) (#7073)
This commit is contained in:
committed by
GitHub
parent
51093f649d
commit
97f87ae0fc
@@ -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",
|
||||
|
||||
@@ -41,7 +41,7 @@ export class Column {
|
||||
return this;
|
||||
}
|
||||
|
||||
timestamp(msPrecision?: number) {
|
||||
timestamp(msPrecision = 3) {
|
||||
this.type = 'timestamp';
|
||||
this.length = msPrecision ?? 'auto';
|
||||
return this;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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[],
|
||||
|
||||
@@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user