Skip to content
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

Fix update remote field metadata #5638

Merged
merged 7 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
@@ -1,4 +1,4 @@
import { useQuery } from '@apollo/client';
import { useQuery, WatchQueryFetchPolicy } from '@apollo/client';

import { GET_ONE_DATABASE_CONNECTION } from '@/databases/graphql/queries/findOneDatabaseConnection';
import { getForeignDataWrapperType } from '@/databases/utils/getForeignDataWrapperType';
Expand All @@ -12,16 +12,20 @@ type UseGetDatabaseConnectionParams = {
databaseKey: string;
connectionId: string;
skip?: boolean;
fetchPolicy?: WatchQueryFetchPolicy;
};

export const useGetDatabaseConnection = ({
databaseKey,
connectionId,
skip,
fetchPolicy,
}: UseGetDatabaseConnectionParams) => {
const apolloMetadataClient = useApolloMetadataClient();
const foreignDataWrapperType = getForeignDataWrapperType(databaseKey);

const fetchPolicyOption = fetchPolicy ? { fetchPolicy: fetchPolicy } : {};

const { data, loading } = useQuery<
GetOneDatabaseConnectionQuery,
GetOneDatabaseConnectionQueryVariables
Expand All @@ -33,6 +37,7 @@ export const useGetDatabaseConnection = ({
id: connectionId,
},
},
...fetchPolicyOption,
});

const connection = data?.findOneRemoteServerById ?? null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@apollo/client';
import { useQuery, WatchQueryFetchPolicy } from '@apollo/client';

import { GET_MANY_REMOTE_TABLES } from '@/databases/graphql/queries/findManyRemoteTables';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
Expand All @@ -11,15 +11,19 @@ type UseGetDatabaseConnectionTablesParams = {
connectionId: string;
skip?: boolean;
shouldFetchPendingSchemaUpdates?: boolean;
fetchPolicy?: WatchQueryFetchPolicy | undefined;
};
ijreilly marked this conversation as resolved.
Show resolved Hide resolved

export const useGetDatabaseConnectionTables = ({
connectionId,
skip,
shouldFetchPendingSchemaUpdates,
fetchPolicy,
}: UseGetDatabaseConnectionTablesParams) => {
const apolloMetadataClient = useApolloMetadataClient();

const fetchPolicyOption = fetchPolicy ? { fetchPolicy: fetchPolicy } : {};

const { data, error } = useQuery<
GetManyRemoteTablesQuery,
GetManyRemoteTablesQueryVariables
Expand All @@ -32,6 +36,7 @@ export const useGetDatabaseConnectionTables = ({
shouldFetchPendingSchemaUpdates,
},
},
...fetchPolicyOption,
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { useCallback } from 'react';
import { ApolloClient, useMutation } from '@apollo/client';
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';

import { SYNC_REMOTE_TABLE_SCHEMA_CHANGES } from '@/databases/graphql/mutations/syncRemoteTableSchemaChanges';
import { modifyRemoteTableFromCache } from '@/databases/utils/modifyRemoteTableFromCache';
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { useFindManyObjectMetadataItems } from '@/object-metadata/hooks/useFindManyObjectMetadataItems';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';
import {
RemoteTableInput,
SyncRemoteTableSchemaChangesMutation,
Expand All @@ -13,6 +16,14 @@ import { isDefined } from '~/utils/isDefined';

export const useSyncRemoteTableSchemaChanges = () => {
const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();

const { refetch: refetchObjectMetadataItems } =
useFindManyObjectMetadataItems();

const { findManyRecordsQuery: findManyViewsQuery } = useFindManyRecordsQuery({
objectNameSingular: CoreObjectNameSingular.View,
});

const [mutate, mutationInformation] = useMutation<
SyncRemoteTableSchemaChangesMutation,
Expand Down Expand Up @@ -42,9 +53,16 @@ export const useSyncRemoteTableSchemaChanges = () => {
},
});

await refetchObjectMetadataItems();

await apolloClient.query({
query: findManyViewsQuery,
fetchPolicy: 'network-only',
});

return remoteTable;
},
[mutate],
[mutate, refetchObjectMetadataItems, findManyViewsQuery, apolloClient],
);

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Breadcrumb } from '@/ui/navigation/bread-crumb/components/Breadcrumb';
export const SettingsIntegrationDatabaseConnectionShowContainer = () => {
const navigate = useNavigate();
const { connection, integration, databaseKey, tables } =
useDatabaseConnection();
useDatabaseConnection({ fetchPolicy: 'network-only' });

const { deleteOneDatabaseConnection } = useDeleteOneDatabaseConnection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDatabaseConnection } from '@/settings/integrations/database-connecti

export const SettingsIntegrationEditDatabaseConnectionContainer = () => {
const { connection, integration, databaseKey, tables } =
useDatabaseConnection();
useDatabaseConnection({});

if (!connection || !integration) return null;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { useEffect } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { WatchQueryFetchPolicy } from '@apollo/client';

import { useGetDatabaseConnection } from '@/databases/hooks/useGetDatabaseConnection';
import { useGetDatabaseConnectionTables } from '@/databases/hooks/useGetDatabaseConnectionTables';
import { useIsSettingsIntegrationEnabled } from '@/settings/integrations/hooks/useIsSettingsIntegrationEnabled';
import { useSettingsIntegrationCategories } from '@/settings/integrations/hooks/useSettingsIntegrationCategories';
import { AppPath } from '@/types/AppPath';

export const useDatabaseConnection = () => {
export const useDatabaseConnection = ({
fetchPolicy,
}: {
fetchPolicy?: WatchQueryFetchPolicy | undefined;
ijreilly marked this conversation as resolved.
Show resolved Hide resolved
}) => {
const { databaseKey = '', connectionId = '' } = useParams();
const navigate = useNavigate();

Expand All @@ -24,6 +29,7 @@ export const useDatabaseConnection = () => {
databaseKey,
connectionId,
skip: !isIntegrationAvailable,
fetchPolicy,
});

useEffect(() => {
Expand All @@ -43,6 +49,7 @@ export const useDatabaseConnection = () => {
connectionId,
skip: !connection,
shouldFetchPendingSchemaUpdates: true,
fetchPolicy,
});

return { connection, integration, databaseKey, tables };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Module } from '@nestjs/common';

import { FieldMetadataModule } from 'src/engine/metadata-modules/field-metadata/field-metadata.module';
import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module';
import { ForeignTableService } from 'src/engine/metadata-modules/remote-server/remote-table/foreign-table/foreign-table.service';
import { WorkspaceCacheVersionModule } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.module';
import { WorkspaceMigrationModule } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.module';
Expand All @@ -12,6 +14,8 @@ import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/wor
WorkspaceMigrationRunnerModule,
WorkspaceDataSourceModule,
WorkspaceCacheVersionModule,
ObjectMetadataModule,
FieldMetadataModule,
],
providers: [ForeignTableService],
exports: [ForeignTableService],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { BadRequestException, Injectable } from '@nestjs/common';

import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
import {
RemoteServerEntity,
RemoteServerType,
} from 'src/engine/metadata-modules/remote-server/remote-server.entity';
import { RemoteTableStatus } from 'src/engine/metadata-modules/remote-server/remote-table/dtos/remote-table.dto';
import { getForeignTableColumnName } from 'src/engine/metadata-modules/remote-server/remote-table/foreign-table/utils/get-foreign-table-column-name.util';
import {
mapUdtNameToFieldType,
mapUdtNameToFieldSettings,
} from 'src/engine/metadata-modules/remote-server/remote-table/utils/udt-name-mapper.util';
import { PostgresTableSchemaColumn } from 'src/engine/metadata-modules/remote-server/types/postgres-table-schema-column';
import { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util';
Expand All @@ -15,10 +22,12 @@ import {
WorkspaceMigrationForeignColumnDefinition,
WorkspaceMigrationForeignTable,
WorkspaceMigrationColumnAction,
WorkspaceMigrationColumnActionType,
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
import { WorkspaceMigrationService } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.service';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service';
import { camelToTitleCase } from 'src/utils/camel-to-title-case';

@Injectable()
export class ForeignTableService {
Expand All @@ -27,6 +36,8 @@ export class ForeignTableService {
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly workspaceCacheVersionService: WorkspaceCacheVersionService,
private readonly objectMetadataService: ObjectMetadataService,
private readonly fieldMetadataService: FieldMetadataService,
ijreilly marked this conversation as resolved.
Show resolved Hide resolved
) {}

public async fetchForeignTableNamesWithinWorkspace(
Expand Down Expand Up @@ -96,10 +107,72 @@ export class ForeignTableService {
}
}

public async updateForeignTable(
public async updateForeignTableAndFieldsMetadata(
ijreilly marked this conversation as resolved.
Show resolved Hide resolved
foreignTableName: string,
workspaceId: string,
columnsUpdates: WorkspaceMigrationColumnAction[],
) {
const updatedForeignTable = await this.updateForeignTable(
foreignTableName,
workspaceId,
columnsUpdates,
);

const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
where: { nameSingular: foreignTableName },
});

if (!objectMetadata) {
throw new Error(
ijreilly marked this conversation as resolved.
Show resolved Hide resolved
`Cannot find associated object for table ${foreignTableName}`,
);
}
for (const columnUpdate of columnsUpdates) {
ijreilly marked this conversation as resolved.
Show resolved Hide resolved
if (columnUpdate.action === WorkspaceMigrationColumnActionType.CREATE) {
const columnName = columnUpdate.columnName;

await this.fieldMetadataService.createOne({
name: columnName,
label: camelToTitleCase(columnName),
description: 'Field of remote',
type: mapUdtNameToFieldType(columnUpdate.columnType),
workspaceId: workspaceId,
objectMetadataId: objectMetadata.id,
isRemoteCreation: true,
isNullable: true,
icon: 'IconPlug',
settings: mapUdtNameToFieldSettings(columnUpdate.columnType),
} satisfies CreateFieldInput);
}
if (columnUpdate.action === WorkspaceMigrationColumnActionType.DROP) {
const columnName = columnUpdate.columnName;

const fieldMetadataToDelete =
await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
where: {
objectMetadataId: objectMetadata.id,
name: columnName,
},
});

if (!fieldMetadataToDelete) {
throw new Error(
`Cannot find associated field metadata for column ${columnName}`,
);
}

await this.fieldMetadataService.deleteOne(fieldMetadataToDelete.id);
}
}

return updatedForeignTable;
}

private async updateForeignTable(
foreignTableName: string,
workspaceId: string,
columnsUpdates?: WorkspaceMigrationColumnAction[],
columnsUpdates: WorkspaceMigrationColumnAction[],
) {
const workspaceMigration =
await this.workspaceMigrationService.createCustomMigration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,12 @@ export class RemoteTableService {
};
}

const updatedTable = await this.foreignTableService.updateForeignTable(
remoteTable.localTableName,
workspaceId,
columnsUpdates,
);
const updatedTable =
await this.foreignTableService.updateForeignTableAndFieldsMetadata(
remoteTable.localTableName,
workspaceId,
columnsUpdates,
);

return updatedTable;
}
Expand Down
Loading