fix: DB revert command shouldn't run full migrations before each revert (#5131)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2023-01-11 18:29:31 +01:00 committed by GitHub
parent 62cce2e518
commit a9fb393e1a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 74 deletions

View file

@ -44,68 +44,57 @@ export function linkRepository<Entity extends ObjectLiteral>(
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(
testConnectionOptions?: ConnectionOptions,
): Promise<IDatabaseCollections> {
if (isInitialized) return collections;
const dbType = (await GenericHelpers.getConfigValue('database.type')) as DatabaseType;
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!`);
}
}
const connectionOptions = testConnectionOptions ?? (await getConnectionOptions(dbType));
let loggingOption: LoggerOptions = (await GenericHelpers.getConfigValue(
'database.logging.enabled',
@ -143,6 +132,7 @@ export async function init(
// n8n knows it has changed. Happens only on sqlite.
let migrations = [];
try {
const entityPrefix = config.getEnv('database.tablePrefix');
migrations = await connection.query(
`SELECT id FROM ${entityPrefix}migrations where name = "MakeStoppedAtNullable1607431743769"`,
);

View file

@ -5,8 +5,8 @@ import { Connection, ConnectionOptions, createConnection } from 'typeorm';
import { LoggerProxy } from 'n8n-workflow';
import { getLogger } from '@/Logger';
import * as Db from '@/Db';
import { getConnectionOptions } from '@/Db';
import config from '@/config';
export class DbRevertMigrationCommand extends Command {
static description = 'Revert last database migration';
@ -17,35 +17,24 @@ export class DbRevertMigrationCommand extends Command {
help: flags.help({ char: 'h' }),
};
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async run() {
const logger = getLogger();
LoggerProxy.init(logger);
// eslint-disable-next-line @typescript-eslint/no-shadow, @typescript-eslint/no-unused-vars
const { flags } = this.parse(DbRevertMigrationCommand);
this.parse(DbRevertMigrationCommand);
let connection: Connection | undefined;
try {
await Db.init();
connection = Db.collections.Credentials.manager.connection;
if (!connection) {
throw new Error('No database connection available.');
}
const connectionOptions: ConnectionOptions = Object.assign(connection.options, {
const dbType = config.getEnv('database.type');
const connectionOptions: ConnectionOptions = {
...(await getConnectionOptions(dbType)),
subscribers: [],
synchronize: false,
migrationsRun: false,
dropSchema: false,
logging: ['query', 'error', 'schema'],
});
// close connection in order to reconnect with updated options
await connection.close();
};
connection = await createConnection(connectionOptions);
await connection.undoLastMigration();
await connection.close();
} catch (error) {