Skip to content

Backfill workspace version column #10637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { InjectRepository } from '@nestjs/typeorm';

import chalk from 'chalk';
import { Repository } from 'typeorm';

import { BatchActiveWorkspacesMigrationCommandRunner } from 'src/database/commands/migration-command/batch-active-workspaces-migration-command.runner';
import { MigrationCommand } from 'src/database/commands/migration-command/decorators/migration-command.decorator';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';

@MigrationCommand({
name: 'backfill-workspace-version',
description: 'Backfill workspace version field to "0.43"',
version: '0.43',
})
export class BackfillWorkspaceVersionCommand extends BatchActiveWorkspacesMigrationCommandRunner {
constructor(
@InjectRepository(Workspace, 'core')
protected readonly workspaceRepository: Repository<Workspace>,
protected readonly twentyORMGlobalManager: TwentyORMGlobalManager,
) {
super(workspaceRepository, twentyORMGlobalManager);
}

async runMigrationCommandOnWorkspace(
workspaceId: string,
index: number,
total: number,
): Promise<void> {
try {
this.logger.log(
`Running version backfill for workspace ${workspaceId} ${index + 1}/${total}`,
);

await this.backfillWorkspaceVersion(workspaceId);

this.logger.log(
chalk.green(`Command completed for workspace ${workspaceId}.`),
);
} catch (error) {
this.logger.log(
chalk.red(`Error in workspace ${workspaceId} - ${error.message}`),
);
Comment on lines +41 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Error is only logged but not propagated. Could lead to silent failures in the migration process.

Suggested change
this.logger.log(
chalk.red(`Error in workspace ${workspaceId} - ${error.message}`),
);
this.logger.log(
chalk.red(`Error in workspace ${workspaceId} - ${error.message}`),
);
throw error;

}
}

private async backfillWorkspaceVersion(workspaceId: string): Promise<void> {
await this.workspaceRepository.update(
{ id: workspaceId },
{ version: '0.43' },
);

this.logger.log(
chalk.green(
`Successfully backfilled version to "0.43" for workspace ${workspaceId}`,
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { MigrationCommandModule } from 'src/database/commands/migration-command/miration-command.module';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Typo in import path: 'miration-command.module' should be 'migration-command.module'

Suggested change
import { MigrationCommandModule } from 'src/database/commands/migration-command/miration-command.module';
import { MigrationCommandModule } from 'src/database/commands/migration-command/migration-command.module';

import { StandardizationOfActorCompositeContextTypeCommand } from 'src/database/commands/upgrade-version/0-42/0-42-standardization-of-actor-composite-context-type';
import { AddTasksAssignedToMeViewCommand } from 'src/database/commands/upgrade-version/0-43/0-43-add-tasks-assigned-to-me-view.command';
import { BackfillWorkspaceVersionCommand } from 'src/database/commands/upgrade-version/0-43/0-43-backfill-workspace-version.command';
import { MigrateRelationsToFieldMetadataCommand } from 'src/database/commands/upgrade-version/0-43/0-43-migrate-relations-to-field-metadata.command';
import { MigrateSearchVectorOnNoteAndTaskEntitiesCommand } from 'src/database/commands/upgrade-version/0-43/0-43-migrate-search-vector-on-note-and-task-entities.command';
import { UpdateDefaultViewRecordOpeningOnWorkflowObjectsCommand } from 'src/database/commands/upgrade-version/0-43/0-43-update-default-view-record-opening-on-workflow-objects.command';
Expand Down Expand Up @@ -32,6 +33,7 @@ import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/wor
],
providers: [
AddTasksAssignedToMeViewCommand,
BackfillWorkspaceVersionCommand,
MigrateSearchVectorOnNoteAndTaskEntitiesCommand,
UpdateDefaultViewRecordOpeningOnWorkflowObjectsCommand,
StandardizationOfActorCompositeContextTypeCommand,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { WorkspaceActivationStatus } from 'twenty-shared';
import { DataSource } from 'typeorm';

import { getAppVersion } from 'src/engine/core-modules/utils/version.util';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';

const tableName = 'workspace';
Expand All @@ -22,6 +23,7 @@ export const seedWorkspaces = async (
| 'logo'
| 'subdomain'
| 'activationStatus'
| 'version'
>;
} = {
[SEED_APPLE_WORKSPACE_ID]: {
Expand All @@ -31,6 +33,7 @@ export const seedWorkspaces = async (
inviteHash: 'apple.dev-invite-hash',
logo: 'https://twentyhq.github.io/placeholder-images/workspaces/apple-logo.png',
activationStatus: WorkspaceActivationStatus.ACTIVE,
version: getAppVersion(),
},
[SEED_ACME_WORKSPACE_ID]: {
id: workspaceId,
Expand All @@ -39,6 +42,7 @@ export const seedWorkspaces = async (
inviteHash: 'acme.dev-invite-hash',
logo: 'https://logos-world.net/wp-content/uploads/2022/05/Acme-Logo-700x394.png',
activationStatus: WorkspaceActivationStatus.ACTIVE,
version: getAppVersion(),
},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { OnboardingService } from 'src/engine/core-modules/onboarding/onboarding
import { UserWorkspaceService } from 'src/engine/core-modules/user-workspace/user-workspace.service';
import { UserService } from 'src/engine/core-modules/user/services/user.service';
import { User } from 'src/engine/core-modules/user/user.entity';
import { getAppVersion } from 'src/engine/core-modules/utils/version.util';
import { WorkspaceInvitationService } from 'src/engine/core-modules/workspace-invitation/services/workspace-invitation.service';
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { UserRoleService } from 'src/engine/metadata-modules/user-role/user-role.service';
Expand Down Expand Up @@ -356,6 +357,7 @@ export class SignInUpService {
inviteHash: v4(),
activationStatus: WorkspaceActivationStatus.PENDING_CREATION,
logo,
version: getAppVersion(),
});

const workspace = await this.workspaceRepository.save(workspaceToCreate);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { readFileSync } from 'fs';
import { join } from 'path';

export const getAppVersion = (): string | null => {
try {
const packageJsonPath = join(__dirname, '../../../../package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));

return packageJson.version;
Comment on lines +7 to +9
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: No validation that packageJson.version exists or is in valid semver format

Suggested change
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
return packageJson.version;
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
return typeof packageJson.version === 'string' ? packageJson.version : null;

} catch (error) {
return null;
}
};