twentyhq / twenty

Building a modern alternative to Salesforce, powered by the community.
https://twenty.com
GNU Affero General Public License v3.0
15.69k stars 1.7k forks source link

Discrepancy in defaultValue uuid_generate_v4 naming in schema query #6249

Open charlesBochet opened 1 month ago

charlesBochet commented 1 month ago

Scope & Context

Recently (since we put in place pg-bouncer in production), we are getting false positive in the health check regarding uuid_generate_v4() <> public.uuid_generate_v4()

I have narrowed down the issue to the following root cause:

Query: select c.column_name, c.column_default from information_schema.columns AS c where c.table_schema = 'workspace_xxx' AND c.table_name = 'person' and c.column_name = 'id';

The health check is expecting public.uuid_generate_v4() (as it's what we need to write when setting the default value in postgres).

However, the behavior is not consistent; The pooler seems to be given the right answer but not the database but it was working before we used the pooler. Also I have noticed that the database was returning the right answer while connecting to it through dbeaver.

greptile-apps[bot] commented 1 month ago

To resolve the discrepancy in defaultValue naming for uuid_generate_v4, update the health check logic to accept both uuid_generate_v4() and public.uuid_generate_v4() as valid default values.

  1. Modify database-structure.service.ts:

Update the getPostgresDefault method to normalize the default value.

getPostgresDefault(field: FieldMetadataType): string {
  const defaultValue = field.defaultValue;
  if (defaultValue === 'uuid_generate_v4()' || defaultValue === 'public.uuid_generate_v4()') {
    return 'uuid_generate_v4()';
  }
  // existing logic
}

File: /packages/twenty-server/src/workspace/workspace-health/services/database-structure.service.ts

  1. Modify workspace-default-value.fixer.ts:

Update the computeFieldMetadataDefaultValueFromColumnDefault method to normalize the default value.

private computeFieldMetadataDefaultValueFromColumnDefault(columnDefault?: string): string {
  if (columnDefault === 'uuid_generate_v4()' || columnDefault === 'public.uuid_generate_v4()') {
    return 'uuid_generate_v4()';
  }
  // existing logic
}

File: /packages/twenty-server/src/workspace/workspace-health/fixer/workspace-default-value.fixer.ts

References

/packages/twenty-server/src/workspace/workspace-health/services/database-structure.service.ts /packages/twenty-server/src/workspace/workspace-health/fixer/workspace-default-value.fixer.ts /packages/twenty-server/src/engine/workspace-manager/workspace-health/services/field-metadata-health.service.ts

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/twentyhq/twenty/main) · [Edit Issue Bot Settings](https://app.greptile.com/apps/github)