mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 06:34:05 -08:00
fix: DB revert command shouldn't run full migrations before each revert (#5131)
This commit is contained in:
parent
62cce2e518
commit
a9fb393e1a
|
@ -44,68 +44,57 @@ export function linkRepository<Entity extends ObjectLiteral>(
|
||||||
return getRepository(entityClass, connection.name);
|
return getRepository(entityClass, connection.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getConnectionOptions(dbType: DatabaseType): Promise<ConnectionOptions> {
|
||||||
|
switch (dbType) {
|
||||||
|
case 'postgresdb':
|
||||||
|
const sslCa = (await GenericHelpers.getConfigValue('database.postgresdb.ssl.ca')) as string;
|
||||||
|
const sslCert = (await GenericHelpers.getConfigValue(
|
||||||
|
'database.postgresdb.ssl.cert',
|
||||||
|
)) as string;
|
||||||
|
const sslKey = (await GenericHelpers.getConfigValue('database.postgresdb.ssl.key')) as string;
|
||||||
|
const sslRejectUnauthorized = (await GenericHelpers.getConfigValue(
|
||||||
|
'database.postgresdb.ssl.rejectUnauthorized',
|
||||||
|
)) as boolean;
|
||||||
|
|
||||||
|
let ssl: TlsOptions | undefined;
|
||||||
|
if (sslCa !== '' || sslCert !== '' || sslKey !== '' || !sslRejectUnauthorized) {
|
||||||
|
ssl = {
|
||||||
|
ca: sslCa || undefined,
|
||||||
|
cert: sslCert || undefined,
|
||||||
|
key: sslKey || undefined,
|
||||||
|
rejectUnauthorized: sslRejectUnauthorized,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...getPostgresConnectionOptions(),
|
||||||
|
...(await getOptionOverrides('postgresdb')),
|
||||||
|
ssl,
|
||||||
|
};
|
||||||
|
|
||||||
|
case 'mariadb':
|
||||||
|
case 'mysqldb':
|
||||||
|
return {
|
||||||
|
...(dbType === 'mysqldb' ? getMysqlConnectionOptions() : getMariaDBConnectionOptions()),
|
||||||
|
...(await getOptionOverrides('mysqldb')),
|
||||||
|
timezone: 'Z', // set UTC as default
|
||||||
|
};
|
||||||
|
|
||||||
|
case 'sqlite':
|
||||||
|
return getSqliteConnectionOptions();
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Error(`The database "${dbType}" is currently not supported!`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function init(
|
export async function init(
|
||||||
testConnectionOptions?: ConnectionOptions,
|
testConnectionOptions?: ConnectionOptions,
|
||||||
): Promise<IDatabaseCollections> {
|
): Promise<IDatabaseCollections> {
|
||||||
if (isInitialized) return collections;
|
if (isInitialized) return collections;
|
||||||
|
|
||||||
const dbType = (await GenericHelpers.getConfigValue('database.type')) as DatabaseType;
|
const dbType = (await GenericHelpers.getConfigValue('database.type')) as DatabaseType;
|
||||||
|
const connectionOptions = testConnectionOptions ?? (await getConnectionOptions(dbType));
|
||||||
let connectionOptions: ConnectionOptions;
|
|
||||||
|
|
||||||
const entityPrefix = config.getEnv('database.tablePrefix');
|
|
||||||
|
|
||||||
if (testConnectionOptions) {
|
|
||||||
connectionOptions = testConnectionOptions;
|
|
||||||
} else {
|
|
||||||
switch (dbType) {
|
|
||||||
case 'postgresdb':
|
|
||||||
const sslCa = (await GenericHelpers.getConfigValue('database.postgresdb.ssl.ca')) as string;
|
|
||||||
const sslCert = (await GenericHelpers.getConfigValue(
|
|
||||||
'database.postgresdb.ssl.cert',
|
|
||||||
)) as string;
|
|
||||||
const sslKey = (await GenericHelpers.getConfigValue(
|
|
||||||
'database.postgresdb.ssl.key',
|
|
||||||
)) as string;
|
|
||||||
const sslRejectUnauthorized = (await GenericHelpers.getConfigValue(
|
|
||||||
'database.postgresdb.ssl.rejectUnauthorized',
|
|
||||||
)) as boolean;
|
|
||||||
|
|
||||||
let ssl: TlsOptions | undefined;
|
|
||||||
if (sslCa !== '' || sslCert !== '' || sslKey !== '' || !sslRejectUnauthorized) {
|
|
||||||
ssl = {
|
|
||||||
ca: sslCa || undefined,
|
|
||||||
cert: sslCert || undefined,
|
|
||||||
key: sslKey || undefined,
|
|
||||||
rejectUnauthorized: sslRejectUnauthorized,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
connectionOptions = {
|
|
||||||
...getPostgresConnectionOptions(),
|
|
||||||
...(await getOptionOverrides('postgresdb')),
|
|
||||||
ssl,
|
|
||||||
};
|
|
||||||
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'mariadb':
|
|
||||||
case 'mysqldb':
|
|
||||||
connectionOptions = {
|
|
||||||
...(dbType === 'mysqldb' ? getMysqlConnectionOptions() : getMariaDBConnectionOptions()),
|
|
||||||
...(await getOptionOverrides('mysqldb')),
|
|
||||||
timezone: 'Z', // set UTC as default
|
|
||||||
};
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'sqlite':
|
|
||||||
connectionOptions = getSqliteConnectionOptions();
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw new Error(`The database "${dbType}" is currently not supported!`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let loggingOption: LoggerOptions = (await GenericHelpers.getConfigValue(
|
let loggingOption: LoggerOptions = (await GenericHelpers.getConfigValue(
|
||||||
'database.logging.enabled',
|
'database.logging.enabled',
|
||||||
|
@ -143,6 +132,7 @@ export async function init(
|
||||||
// n8n knows it has changed. Happens only on sqlite.
|
// n8n knows it has changed. Happens only on sqlite.
|
||||||
let migrations = [];
|
let migrations = [];
|
||||||
try {
|
try {
|
||||||
|
const entityPrefix = config.getEnv('database.tablePrefix');
|
||||||
migrations = await connection.query(
|
migrations = await connection.query(
|
||||||
`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
|
`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
|
||||||
);
|
);
|
||||||
|
|
|
@ -5,8 +5,8 @@ import { Connection, ConnectionOptions, createConnection } from 'typeorm';
|
||||||
import { LoggerProxy } from 'n8n-workflow';
|
import { LoggerProxy } from 'n8n-workflow';
|
||||||
|
|
||||||
import { getLogger } from '@/Logger';
|
import { getLogger } from '@/Logger';
|
||||||
|
import { getConnectionOptions } from '@/Db';
|
||||||
import * as Db from '@/Db';
|
import config from '@/config';
|
||||||
|
|
||||||
export class DbRevertMigrationCommand extends Command {
|
export class DbRevertMigrationCommand extends Command {
|
||||||
static description = 'Revert last database migration';
|
static description = 'Revert last database migration';
|
||||||
|
@ -17,35 +17,24 @@ export class DbRevertMigrationCommand extends Command {
|
||||||
help: flags.help({ char: 'h' }),
|
help: flags.help({ char: 'h' }),
|
||||||
};
|
};
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
||||||
async run() {
|
async run() {
|
||||||
const logger = getLogger();
|
const logger = getLogger();
|
||||||
LoggerProxy.init(logger);
|
LoggerProxy.init(logger);
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-shadow, @typescript-eslint/no-unused-vars
|
this.parse(DbRevertMigrationCommand);
|
||||||
const { flags } = this.parse(DbRevertMigrationCommand);
|
|
||||||
|
|
||||||
let connection: Connection | undefined;
|
let connection: Connection | undefined;
|
||||||
try {
|
try {
|
||||||
await Db.init();
|
const dbType = config.getEnv('database.type');
|
||||||
connection = Db.collections.Credentials.manager.connection;
|
const connectionOptions: ConnectionOptions = {
|
||||||
|
...(await getConnectionOptions(dbType)),
|
||||||
if (!connection) {
|
|
||||||
throw new Error('No database connection available.');
|
|
||||||
}
|
|
||||||
|
|
||||||
const connectionOptions: ConnectionOptions = Object.assign(connection.options, {
|
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
synchronize: false,
|
synchronize: false,
|
||||||
migrationsRun: false,
|
migrationsRun: false,
|
||||||
dropSchema: false,
|
dropSchema: false,
|
||||||
logging: ['query', 'error', 'schema'],
|
logging: ['query', 'error', 'schema'],
|
||||||
});
|
};
|
||||||
|
|
||||||
// close connection in order to reconnect with updated options
|
|
||||||
await connection.close();
|
|
||||||
connection = await createConnection(connectionOptions);
|
connection = await createConnection(connectionOptions);
|
||||||
|
|
||||||
await connection.undoLastMigration();
|
await connection.undoLastMigration();
|
||||||
await connection.close();
|
await connection.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
Loading…
Reference in a new issue