Guard yarn database:migrate:prod (#19008)

## Motivations
A lot of self hosters hands up using the `yarn database:migrated:prod`
either manually or through AI assisted debug while they try to upgrade
an instance while their workspace is still blocked in a previous one
Leading to their whole database permanent corruption

## What happened
Replaced the direct call the the typeorm cli to a command calling it
programmatically, adding a layer of security in case a workspace seems
to be blocked in a previous version than the one just before the one
being installed ( e.g 1.0 when you try to upgrade from 1.1 to 1.2 )

For our cloud we still need a way to bypass this security explaining the
-f flag

## Remark
Centralized this logic and refactored creating new services
`WorkspaceVersionService` and `CoreEngineVersionService` that will
become useful for the upcoming upgrade refactor

Related to https://github.com/twentyhq/twenty-infra/pull/529
This commit is contained in:
Paul Rastoin
2026-03-27 15:39:18 +01:00
committed by GitHub
parent c96c034908
commit 281bb6d783
25 changed files with 401 additions and 216 deletions
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { CoreEngineVersionService } from 'src/engine/core-engine-version/services/core-engine-version.service';
@Module({
providers: [CoreEngineVersionService],
exports: [CoreEngineVersionService],
})
export class CoreEngineVersionModule {}
@@ -0,0 +1,47 @@
import { Injectable } from '@nestjs/common';
import { SemVer } from 'semver';
import { isDefined } from 'twenty-shared/utils';
import { UPGRADE_COMMAND_SUPPORTED_VERSIONS } from 'src/engine/constants/upgrade-command-supported-versions.constant';
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { getPreviousVersion } from 'src/utils/version/get-previous-version';
@Injectable()
export class CoreEngineVersionService {
constructor(private readonly twentyConfigService: TwentyConfigService) {}
getCurrentVersion(): SemVer {
const appVersion = this.twentyConfigService.get('APP_VERSION');
if (!isDefined(appVersion)) {
throw new Error(
'APP_VERSION is not defined, please double check your env variables',
);
}
try {
return new SemVer(appVersion);
} catch {
throw new Error(`APP_VERSION is not a valid semver: "${appVersion}"`);
}
}
getPreviousVersion(): SemVer {
const currentAppVersion = this.getCurrentVersion();
const currentVersionMajorMinor = `${currentAppVersion.major}.${currentAppVersion.minor}.0`;
const previousVersion = getPreviousVersion({
currentVersion: currentVersionMajorMinor,
versions: [...UPGRADE_COMMAND_SUPPORTED_VERSIONS],
});
if (!isDefined(previousVersion)) {
throw new Error(
`No previous version found for version ${currentAppVersion}. Available versions: ${UPGRADE_COMMAND_SUPPORTED_VERSIONS.join(', ')}`,
);
}
return previousVersion;
}
}