refactor(core): Mark deprecations for v2 (#11817)

This commit is contained in:
Iván Ovejero 2024-11-22 13:49:00 +01:00 committed by GitHub
parent 5c80cb57cf
commit bce2366947
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 131 additions and 23 deletions

View file

@ -21,6 +21,7 @@ import { LICENSE_FEATURES, inDevelopment, inTest } from '@/constants';
import * as CrashJournal from '@/crash-journal';
import * as Db from '@/db';
import { getDataDeduplicationService } from '@/deduplication';
import { DeprecationService } from '@/deprecation/deprecation.service';
import { initErrorHandling } from '@/error-reporting';
import { MessageEventBus } from '@/eventbus/message-event-bus/message-event-bus';
import { TelemetryEventRelay } from '@/events/relays/telemetry.event-relay';
@ -88,33 +89,14 @@ export abstract class BaseCommand extends Command {
await this.exitWithCrash('There was an error running database migrations', error),
);
const { type: dbType } = this.globalConfig.database;
if (['mysqldb', 'mariadb'].includes(dbType)) {
this.logger.warn(
'Support for MySQL/MariaDB has been deprecated and will be removed with an upcoming version of n8n. Please migrate to PostgreSQL.',
);
}
if (process.env.N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN) {
this.logger.warn(
'The flag to skip webhook deregistration N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN has been removed. n8n no longer deregisters webhooks at startup and shutdown, in main and queue mode.',
);
}
if (config.getEnv('executions.mode') === 'queue' && dbType === 'sqlite') {
this.logger.warn(
'Queue mode is not officially supported with sqlite. Please switch to PostgreSQL.',
);
}
Container.get(DeprecationService).warn();
if (
process.env.N8N_BINARY_DATA_TTL ??
process.env.N8N_PERSISTED_BINARY_DATA_TTL ??
process.env.EXECUTIONS_DATA_PRUNE_TIMEOUT
config.getEnv('executions.mode') === 'queue' &&
this.globalConfig.database.type === 'sqlite'
) {
this.logger.warn(
'The env vars N8N_BINARY_DATA_TTL and N8N_PERSISTED_BINARY_DATA_TTL and EXECUTIONS_DATA_PRUNE_TIMEOUT no longer have any effect and can be safely removed. Instead of relying on a TTL system for binary data, n8n currently cleans up binary data together with executions during pruning.',
'Scaling mode is not officially supported with sqlite. Please use PostgreSQL instead.',
);
}

View file

@ -0,0 +1,41 @@
import { mockLogger } from '@test/mocking';
import { DeprecationService } from '../deprecation.service';
describe('DeprecationService', () => {
const toTest = (envVar: string, value: string, inUse: boolean) => {
process.env[envVar] = value;
const deprecationService = new DeprecationService(mockLogger());
deprecationService.warn();
expect(deprecationService.isInUse(envVar)).toBe(inUse);
};
test.each([
['N8N_BINARY_DATA_TTL', '1', true],
['N8N_PERSISTED_BINARY_DATA_TTL', '1', true],
['EXECUTIONS_DATA_PRUNE_TIMEOUT', '1', true],
['N8N_CONFIG_FILES', '1', true],
['N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN', '1', true],
])('should detect when %s is in use', (envVar, value, inUse) => {
toTest(envVar, value, inUse);
});
test.each([
['default', true],
['filesystem', false],
['s3', false],
])('should handle N8N_BINARY_DATA_MODE as %s', (mode, inUse) => {
toTest('N8N_BINARY_DATA_MODE', mode, inUse);
});
test.each([
['sqlite', false],
['postgresdb', false],
['mysqldb', true],
['mariadb', true],
])('should handle DB_TYPE as %s', (dbType, inUse) => {
toTest('DB_TYPE', dbType, inUse);
});
});

View file

@ -0,0 +1,85 @@
import { ApplicationError } from 'n8n-workflow';
import { Service } from 'typedi';
import { Logger } from '@/logging/logger.service';
type EnvVarName = string;
type Deprecation = {
/** Name of the deprecated env var. */
envVar: EnvVarName;
/** Message to display when the deprecated env var is currently in use. */
message: string;
/** Function to identify the specific value in the env var that is deprecated. */
checkValue?: (value: string) => boolean;
};
const SAFE_TO_REMOVE = 'Remove this environment variable; it is no longer needed.';
/** Responsible for warning about use of deprecated env vars. */
@Service()
export class DeprecationService {
private readonly deprecations: Deprecation[] = [
{ envVar: 'N8N_BINARY_DATA_TTL', message: SAFE_TO_REMOVE },
{ envVar: 'N8N_PERSISTED_BINARY_DATA_TTL', message: SAFE_TO_REMOVE },
{ envVar: 'EXECUTIONS_DATA_PRUNE_TIMEOUT', message: SAFE_TO_REMOVE },
{
envVar: 'N8N_BINARY_DATA_MODE',
message: '`default` is deprecated. Please switch to `filesystem` mode.',
checkValue: (value: string) => value === 'default',
},
{ envVar: 'N8N_CONFIG_FILES', message: 'Please use .env files or *_FILE env vars instead.' },
{
envVar: 'DB_TYPE',
message: 'MySQL and MariaDB are deprecated. Please migrate to PostgreSQL.',
checkValue: (value: string) => ['mysqldb', 'mariadb'].includes(value),
},
{
envVar: 'N8N_SKIP_WEBHOOK_DEREGISTRATION_SHUTDOWN',
message: `n8n no longer deregisters webhooks at startup and shutdown. ${SAFE_TO_REMOVE}`,
},
];
/** Runtime state of deprecated env vars. */
private readonly state: Record<EnvVarName, { inUse: boolean }> = {};
constructor(private readonly logger: Logger) {}
warn() {
this.deprecations.forEach((d) => {
const envValue = process.env[d.envVar];
this.state[d.envVar] = {
inUse: d.checkValue
? envValue !== undefined && d.checkValue(envValue)
: envValue !== undefined,
};
});
const inUse = Object.entries(this.state)
.filter(([, d]) => d.inUse)
.map(([envVar]) => {
const deprecation = this.deprecations.find((d) => d.envVar === envVar);
if (!deprecation) {
throw new ApplicationError(`Deprecation not found for env var: ${envVar}`);
}
return deprecation;
});
if (inUse.length === 0) return;
const header = `The following environment variable${
inUse.length === 1 ? ' is' : 's are'
} deprecated and will be removed in an upcoming version of n8n. Please take the recommended actions to update your configuration`;
const deprecations = inUse
.map(({ envVar, message }) => ` - ${envVar} -> ${message}\n`)
.join('');
this.logger.warn(`\n${header}:\n${deprecations}`);
}
isInUse(envVar: string) {
return this.state[envVar]?.inUse ?? false;
}
}