mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
refactor(core): Add addColumns
and dropColumns
to the migrations DSL (no-changelog) (#7073)
This commit is contained in:
parent
51093f649d
commit
97f87ae0fc
|
@ -187,7 +187,7 @@
|
||||||
"swagger-ui-express": "^4.3.0",
|
"swagger-ui-express": "^4.3.0",
|
||||||
"syslog-client": "^1.1.1",
|
"syslog-client": "^1.1.1",
|
||||||
"typedi": "^0.10.0",
|
"typedi": "^0.10.0",
|
||||||
"typeorm": "^0.3.12",
|
"typeorm": "^0.3.17",
|
||||||
"uuid": "^8.3.2",
|
"uuid": "^8.3.2",
|
||||||
"validator": "13.7.0",
|
"validator": "13.7.0",
|
||||||
"winston": "^3.3.3",
|
"winston": "^3.3.3",
|
||||||
|
|
|
@ -41,7 +41,7 @@ export class Column {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
timestamp(msPrecision?: number) {
|
timestamp(msPrecision = 3) {
|
||||||
this.type = 'timestamp';
|
this.type = 'timestamp';
|
||||||
this.length = msPrecision ?? 'auto';
|
this.length = msPrecision ?? 'auto';
|
||||||
return this;
|
return this;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import type { TableForeignKeyOptions, TableIndexOptions } from 'typeorm';
|
import type { TableForeignKeyOptions, TableIndexOptions } from 'typeorm';
|
||||||
import { Table, QueryRunner } from 'typeorm';
|
import { Table, QueryRunner, TableColumn } from 'typeorm';
|
||||||
import LazyPromise from 'p-lazy';
|
import LazyPromise from 'p-lazy';
|
||||||
import { Column } from './Column';
|
import { Column } from './Column';
|
||||||
|
|
||||||
|
@ -31,8 +31,8 @@ export class CreateTable extends TableOperation {
|
||||||
|
|
||||||
get withTimestamps() {
|
get withTimestamps() {
|
||||||
this.columns.push(
|
this.columns.push(
|
||||||
new Column('createdAt').timestamp(3).notNull.default('NOW()'),
|
new Column('createdAt').timestamp().notNull.default('NOW()'),
|
||||||
new Column('updatedAt').timestamp(3).notNull.default('NOW()'),
|
new Column('updatedAt').timestamp().notNull.default('NOW()'),
|
||||||
);
|
);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
@ -80,3 +80,39 @@ export class DropTable extends TableOperation {
|
||||||
return queryRunner.dropTable(`${prefix}${name}`, true);
|
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 type { QueryRunner } from 'typeorm';
|
||||||
import { Column } from './Column';
|
import { Column } from './Column';
|
||||||
import { CreateTable, DropTable } from './Table';
|
import { AddColumns, CreateTable, DropColumns, DropTable } from './Table';
|
||||||
import { CreateIndex, DropIndex } from './Indices';
|
import { CreateIndex, DropIndex } from './Indices';
|
||||||
|
|
||||||
export const createSchemaBuilder = (tablePrefix: string, queryRunner: QueryRunner) => ({
|
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),
|
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: (
|
createIndex: (
|
||||||
tableName: string,
|
tableName: string,
|
||||||
columnNames: string[],
|
columnNames: string[],
|
||||||
|
|
|
@ -1,26 +1,14 @@
|
||||||
import type { MigrationContext, ReversibleMigration } from '@db/types';
|
import type { MigrationContext, ReversibleMigration } from '@db/types';
|
||||||
import { TableColumn } from 'typeorm';
|
|
||||||
|
|
||||||
export class RemoveResetPasswordColumns1690000000030 implements ReversibleMigration {
|
export class RemoveResetPasswordColumns1690000000030 implements ReversibleMigration {
|
||||||
async up({ queryRunner, tablePrefix }: MigrationContext) {
|
async up({ schemaBuilder: { dropColumns } }: MigrationContext) {
|
||||||
await queryRunner.dropColumns(`${tablePrefix}user`, [
|
await dropColumns('user', ['resetPasswordToken', 'resetPasswordTokenExpiration']);
|
||||||
'resetPasswordToken',
|
|
||||||
'resetPasswordTokenExpiration',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async down({ queryRunner, tablePrefix }: MigrationContext) {
|
async down({ schemaBuilder: { addColumns, column } }: MigrationContext) {
|
||||||
await queryRunner.addColumns(`${tablePrefix}user`, [
|
await addColumns('user', [
|
||||||
new TableColumn({
|
column('resetPasswordToken').varchar(),
|
||||||
name: 'resetPasswordToken',
|
column('resetPasswordTokenExpiration').int,
|
||||||
type: 'varchar',
|
|
||||||
isNullable: true,
|
|
||||||
}),
|
|
||||||
new TableColumn({
|
|
||||||
name: 'resetPasswordTokenExpiration',
|
|
||||||
type: 'int',
|
|
||||||
isNullable: true,
|
|
||||||
}),
|
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,35 +1,15 @@
|
||||||
import type { MigrationContext, ReversibleMigration } from '@/databases/types';
|
import type { MigrationContext, ReversibleMigration } from '@/databases/types';
|
||||||
import { TableColumn } from 'typeorm';
|
|
||||||
|
|
||||||
export class AddMfaColumns1690000000030 implements ReversibleMigration {
|
export class AddMfaColumns1690000000030 implements ReversibleMigration {
|
||||||
async up({ queryRunner, tablePrefix }: MigrationContext) {
|
async up({ schemaBuilder: { addColumns, column } }: MigrationContext) {
|
||||||
await queryRunner.addColumns(`${tablePrefix}user`, [
|
await addColumns('user', [
|
||||||
new TableColumn({
|
column('mfaEnabled').bool.notNull.default(false),
|
||||||
name: 'mfaEnabled',
|
column('mfaSecret').text,
|
||||||
type: 'boolean',
|
column('mfaRecoveryCodes').text,
|
||||||
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 down({ queryRunner, tablePrefix }: MigrationContext) {
|
async down({ schemaBuilder: { dropColumns } }: MigrationContext) {
|
||||||
await queryRunner.dropColumns(`${tablePrefix}user`, [
|
await dropColumns('user', ['mfaEnabled', 'mfaSecret', 'mfaRecoveryCodes']);
|
||||||
'mfaEnabled',
|
|
||||||
'mfaSecret',
|
|
||||||
'mfaRecoveryCodes',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,8 +453,8 @@ importers:
|
||||||
specifier: ^0.10.0
|
specifier: ^0.10.0
|
||||||
version: 0.10.0(patch_hash=62r6bc2crgimafeyruodhqlgo4)
|
version: 0.10.0(patch_hash=62r6bc2crgimafeyruodhqlgo4)
|
||||||
typeorm:
|
typeorm:
|
||||||
specifier: ^0.3.12
|
specifier: ^0.3.17
|
||||||
version: 0.3.12(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6)
|
version: 0.3.17(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6)
|
||||||
uuid:
|
uuid:
|
||||||
specifier: ^8.3.2
|
specifier: ^8.3.2
|
||||||
version: 8.3.2
|
version: 8.3.2
|
||||||
|
@ -10697,17 +10697,11 @@ packages:
|
||||||
whatwg-url: 11.0.0
|
whatwg-url: 11.0.0
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/date-fns@2.29.3:
|
|
||||||
resolution: {integrity: sha512-dDCnyH2WnnKusqvZZ6+jA1O51Ibt8ZMRNkDZdyAyK4YfbDwa/cEmuztzG5pk6hqlp9aSBPYcjOlktquahGwGeA==}
|
|
||||||
engines: {node: '>=0.11'}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/date-fns@2.30.0:
|
/date-fns@2.30.0:
|
||||||
resolution: {integrity: sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==}
|
resolution: {integrity: sha512-fnULvOpxnC5/Vg3NCiWelDsLiUc9bRwAPs/+LfTLNvetFCtCTN+yQz15C/fs4AwX1R9K5GLtLfn8QW+dWisaAw==}
|
||||||
engines: {node: '>=0.11'}
|
engines: {node: '>=0.11'}
|
||||||
dependencies:
|
dependencies:
|
||||||
'@babel/runtime': 7.22.6
|
'@babel/runtime': 7.22.6
|
||||||
dev: true
|
|
||||||
|
|
||||||
/dateformat@3.0.3:
|
/dateformat@3.0.3:
|
||||||
resolution: {integrity: sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==}
|
resolution: {integrity: sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==}
|
||||||
|
@ -11132,7 +11126,6 @@ packages:
|
||||||
/dotenv@16.3.1:
|
/dotenv@16.3.1:
|
||||||
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
|
resolution: {integrity: sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==}
|
||||||
engines: {node: '>=12'}
|
engines: {node: '>=12'}
|
||||||
dev: true
|
|
||||||
|
|
||||||
/dotenv@8.6.0:
|
/dotenv@8.6.0:
|
||||||
resolution: {integrity: sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==}
|
resolution: {integrity: sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==}
|
||||||
|
@ -21011,8 +21004,8 @@ packages:
|
||||||
dev: false
|
dev: false
|
||||||
patched: true
|
patched: true
|
||||||
|
|
||||||
/typeorm@0.3.12(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6):
|
/typeorm@0.3.17(ioredis@5.2.4)(mysql2@2.3.3)(pg@8.8.0)(sqlite3@5.1.6):
|
||||||
resolution: {integrity: sha512-sYSxBmCf1nJLLTcYtwqZ+lQIRtLPyUoO93rHTOKk9vJCyT4UfRtU7oRsJvfvKP3nnZTD1hzz2SEy2zwPEN6OyA==}
|
resolution: {integrity: sha512-UDjUEwIQalO9tWw9O2A4GU+sT3oyoUXheHJy4ft+RFdnRdQctdQ34L9SqE2p7LdwzafHx1maxT+bqXON+Qnmig==}
|
||||||
engines: {node: '>= 12.9.0'}
|
engines: {node: '>= 12.9.0'}
|
||||||
hasBin: true
|
hasBin: true
|
||||||
peerDependencies:
|
peerDependencies:
|
||||||
|
@ -21021,8 +21014,8 @@ packages:
|
||||||
better-sqlite3: ^7.1.2 || ^8.0.0
|
better-sqlite3: ^7.1.2 || ^8.0.0
|
||||||
hdb-pool: ^0.1.6
|
hdb-pool: ^0.1.6
|
||||||
ioredis: ^5.0.4
|
ioredis: ^5.0.4
|
||||||
mongodb: ^3.6.0
|
mongodb: ^5.2.0
|
||||||
mssql: ^7.3.0
|
mssql: ^9.1.1
|
||||||
mysql2: ^2.2.5 || ^3.0.1
|
mysql2: ^2.2.5 || ^3.0.1
|
||||||
oracledb: ^5.1.0
|
oracledb: ^5.1.0
|
||||||
pg: ^8.5.1
|
pg: ^8.5.1
|
||||||
|
@ -21074,12 +21067,11 @@ packages:
|
||||||
buffer: 6.0.3
|
buffer: 6.0.3
|
||||||
chalk: 4.1.2
|
chalk: 4.1.2
|
||||||
cli-highlight: 2.1.11
|
cli-highlight: 2.1.11
|
||||||
date-fns: 2.29.3
|
date-fns: 2.30.0
|
||||||
debug: 4.3.4(supports-color@8.1.1)
|
debug: 4.3.4(supports-color@8.1.1)
|
||||||
dotenv: 16.0.3
|
dotenv: 16.3.1
|
||||||
glob: 8.1.0
|
glob: 8.1.0
|
||||||
ioredis: 5.2.4
|
ioredis: 5.2.4
|
||||||
js-yaml: 4.1.0
|
|
||||||
mkdirp: 2.1.3
|
mkdirp: 2.1.3
|
||||||
mysql2: 2.3.3
|
mysql2: 2.3.3
|
||||||
pg: 8.8.0
|
pg: 8.8.0
|
||||||
|
@ -21088,8 +21080,7 @@ packages:
|
||||||
sqlite3: 5.1.6
|
sqlite3: 5.1.6
|
||||||
tslib: 2.6.1
|
tslib: 2.6.1
|
||||||
uuid: 9.0.0
|
uuid: 9.0.0
|
||||||
xml2js: 0.5.0
|
yargs: 17.7.2
|
||||||
yargs: 17.6.2
|
|
||||||
transitivePeerDependencies:
|
transitivePeerDependencies:
|
||||||
- supports-color
|
- supports-color
|
||||||
dev: false
|
dev: false
|
||||||
|
@ -22425,19 +22416,6 @@ packages:
|
||||||
yargs-parser: 21.1.1
|
yargs-parser: 21.1.1
|
||||||
dev: false
|
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:
|
/yargs@17.7.2:
|
||||||
resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==}
|
resolution: {integrity: sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==}
|
||||||
engines: {node: '>=12'}
|
engines: {node: '>=12'}
|
||||||
|
@ -22449,7 +22427,6 @@ packages:
|
||||||
string-width: 4.2.3
|
string-width: 4.2.3
|
||||||
y18n: 5.0.8
|
y18n: 5.0.8
|
||||||
yargs-parser: 21.1.1
|
yargs-parser: 21.1.1
|
||||||
dev: true
|
|
||||||
|
|
||||||
/yargs@7.1.2:
|
/yargs@7.1.2:
|
||||||
resolution: {integrity: sha512-ZEjj/dQYQy0Zx0lgLMLR8QuaqTihnxirir7EwUHp1Axq4e3+k8jXU5K0VLbNvedv1f4EWtBonDIZm0NUr+jCcA==}
|
resolution: {integrity: sha512-ZEjj/dQYQy0Zx0lgLMLR8QuaqTihnxirir7EwUHp1Axq4e3+k8jXU5K0VLbNvedv1f4EWtBonDIZm0NUr+jCcA==}
|
||||||
|
|
Loading…
Reference in a new issue