twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
Other
20.16k stars 2.24k forks source link

Migrate fields metadata when remote table schema is updated #5610

Closed ijreilly closed 5 months ago

greptile-apps[bot] commented 5 months ago

Disclaimer: This comment was automatically generated with AI. It can give some useful hints as to where to look for, but sometimes it can also give very inaccurate answers so takes this with a pinch of salt :).

To migrate fields metadata when the remote table schema is updated, follow these steps:

  1. Update Migration Script: Create a new migration script to handle the schema update.
import { MigrationInterface, QueryRunner } from 'typeorm';

export class UpdateRemoteTableSchemaXXXXXX implements MigrationInterface {
  name = 'UpdateRemoteTableSchemaXXXXXX';

  public async up(queryRunner: QueryRunner): Promise<void> {
    // Add your schema update queries here
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Add your schema rollback queries here
  }
}
  1. Modify Field Metadata Service: Update the FieldMetadataService to handle the schema changes.

File: /packages/twenty-server/src/engine-metadata/field-metadata/field-metadata.service.ts

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { FieldMetadataEntity } from './field-metadata.entity';

@Injectable()
export class FieldMetadataService {
  constructor(
    @InjectRepository(FieldMetadataEntity)
    private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
  ) {}

  // Add method to update field metadata based on remote table schema changes
  async updateFieldMetadataForRemoteTable(schemaChanges: any): Promise<void> {
    // Implement logic to update field metadata
  }
}
  1. Handle Schema Update in Migration: Call the updateFieldMetadataForRemoteTable method in the migration script.

File: /packages/twenty-server/src/database/typeorm/metadata/migrations/XXXXXX-updateRemoteTableSchema.ts

import { MigrationInterface, QueryRunner } from 'typeorm';
import { FieldMetadataService } from '../../engine-metadata/field-metadata/field-metadata.service';

export class UpdateRemoteTableSchemaXXXXXX implements MigrationInterface {
  name = 'UpdateRemoteTableSchemaXXXXXX';

  public async up(queryRunner: QueryRunner): Promise<void> {
    const fieldMetadataService = new FieldMetadataService(queryRunner.manager.getRepository(FieldMetadataEntity));
    await fieldMetadataService.updateFieldMetadataForRemoteTable(/* schema changes */);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // Add your schema rollback queries here
  }
}

Ensure to replace XXXXXX with the appropriate timestamp or identifier for the migration.

References

/packages/twenty-server/src/database/typeorm/metadata/migrations/1713793656356-addSettingsColumnToFieldMetadata.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1714382420165-addSchemaAndAvailableTablesToServer.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1711466822763-addIsRemoteField.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1700663879152-addEnumOptions.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1711619086385-useTimestampWithTZ.ts /packages/twenty-server/src/engine/metadata-modules/object-metadata/object-metadata.service.ts /packages/twenty-server/src/engine-metadata/field-metadata/field-metadata.service.ts /packages/twenty-server/src/engine/workspace-manager/workspace-migration-builder/factories/workspace-migration-relation.factory.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1711459912762-dropTargetColumnMap.ts /packages/twenty-server/src/database/typeorm/metadata/migrations/1713270565699-createRemoteTable.ts

Ask Greptile