Repository: twentyhq/twenty
File: packages/twenty-server/src/engine/workspace-manager/workspace-manager.service.ts (TypeScript)

Code before:
      await this.featureFlagService.getWorkspaceFeatureFlagsMap(workspaceId);

    const twentyStandardApplication =
      await this.applicationService.createTwentyStandardApplication({
        workspaceId,
      });

    const isV2SyncEnabled =
      featureFlags[FeatureFlagKey.IS_WORKSPACE_CREATION_V2_ENABLED];

    if (isV2SyncEnabled) {
      await this.twentyStandardApplicationService.synchronizeTwentyStandardApplicationOrThrow(
        {
          workspaceId,
        },
      );

      const { flatObjectMetadataMaps, flatFieldMetadataMaps } =
        await this.flatEntityMapsCacheService.getOrRecomputeManyOrAllFlatEntityMaps(
          {
            workspaceId,
            flatMapsKeys: ['flatObjectMetadataMaps', 'flatFieldMetadataMaps'],
          },
        );

      const queryRunner = this.coreDataSource.createQueryRunner();

      await queryRunner.connect();
      try {
        await queryRunner.startTransaction();

        await prefillCompanies(queryRunner.manager, schemaName);

        await prefillPeople(queryRunner.manager, schemaName);

        await prefillWorkflows(
          queryRunner.manager,
          schemaName,
          flatObjectMetadataMaps,
          flatFieldMetadataMaps,
        );

        await queryRunner.commitTransaction();
      } catch (error) {
        if (queryRunner.isTransactionActive) {
          try {
            await queryRunner.rollbackTransaction();
          } catch (rollbackError) {
            this.logger.error(
              `Failed to rollback prefill transaction: ${rollbackError.message}`,
            );

Review comment (suggestion):
I'm assuming this was like this before but ideally we should use the workspace global datasource and inject the workspace context and not use the core datasource for record pre-filling @prastoin

Code after:
    const twentyStandardApplication =
      await this.applicationService.createTwentyStandardApplication({
        workspaceId,
      });

    const isV2SyncEnabled =
      featureFlags[FeatureFlagKey.IS_WORKSPACE_CREATION_V2_ENABLED];

    if (isV2SyncEnabled) {
      await this.twentyStandardApplicationService.synchronizeTwentyStandardApplicationOrThrow(
        {
          workspaceId,
        },
      );

      await this.prefillCreatedWorkspaceRecords({
        workspaceId,
        schemaName,
      });
    } else {
      await this.workspaceSyncMetadataService.synchronize({
        workspaceId,
        dataSourceId: dataSourceMetadata.id,
        featureFlags,
      });

      const prefillStandardObjectsStart = performance.now();

      await this.prefillWorkspaceWithStandardObjectsRecords({
        dataSourceMetadata,
        workspaceId,
        featureFlags,
        twentyStandardFlatApplication: fromApplicationEntityToFlatApplication(
          twentyStandardApplication,
        ),
      });

      const prefillStandardObjectsEnd = performance.now();

      this.logger.log(
        `Prefill standard objects took ${prefillStandardObjectsEnd - prefillStandardObjectsStart}ms`,
      );
    }

    const dataSourceMetadataCreationEnd = performance.now();

    this.logger.log(
      `Metadata creation took ${dataSourceMetadataCreationEnd - dataSourceMetadataCreationStart}ms`,
    );

    const { workspaceCustomFlatApplication } =