Fix serverless save when name empty (#6720)
- fix serverless function error on save when name empty - remove useless unique constraint on serverless function names
This commit is contained in:
@ -42,7 +42,7 @@ describe('useServerlessFunctionUpdateFormState', () => {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
const [formValues] = result.current;
|
const { formValues } = result.current;
|
||||||
|
|
||||||
expect(formValues).toEqual({ name: '', description: '', code: '' });
|
expect(formValues).toEqual({ name: '', description: '', code: '' });
|
||||||
});
|
});
|
||||||
|
|||||||
@ -18,7 +18,11 @@ type SetServerlessFunctionFormValues = Dispatch<
|
|||||||
|
|
||||||
export const useServerlessFunctionUpdateFormState = (
|
export const useServerlessFunctionUpdateFormState = (
|
||||||
serverlessFunctionId: string,
|
serverlessFunctionId: string,
|
||||||
): [ServerlessFunctionFormValues, SetServerlessFunctionFormValues] => {
|
): {
|
||||||
|
formValues: ServerlessFunctionFormValues;
|
||||||
|
setFormValues: SetServerlessFunctionFormValues;
|
||||||
|
loading: boolean;
|
||||||
|
} => {
|
||||||
const [formValues, setFormValues] = useState<ServerlessFunctionFormValues>({
|
const [formValues, setFormValues] = useState<ServerlessFunctionFormValues>({
|
||||||
name: '',
|
name: '',
|
||||||
description: '',
|
description: '',
|
||||||
@ -28,7 +32,7 @@ export const useServerlessFunctionUpdateFormState = (
|
|||||||
const { serverlessFunction } =
|
const { serverlessFunction } =
|
||||||
useGetOneServerlessFunction(serverlessFunctionId);
|
useGetOneServerlessFunction(serverlessFunctionId);
|
||||||
|
|
||||||
useGetOneServerlessFunctionSourceCode({
|
const { loading } = useGetOneServerlessFunctionSourceCode({
|
||||||
id: serverlessFunctionId,
|
id: serverlessFunctionId,
|
||||||
version: 'draft',
|
version: 'draft',
|
||||||
onCompleted: (data: FindOneServerlessFunctionSourceCodeQuery) => {
|
onCompleted: (data: FindOneServerlessFunctionSourceCodeQuery) => {
|
||||||
@ -44,5 +48,5 @@ export const useServerlessFunctionUpdateFormState = (
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
return [formValues, setFormValues];
|
return { formValues, setFormValues, loading };
|
||||||
};
|
};
|
||||||
|
|||||||
@ -23,6 +23,7 @@ import { isDefined } from '~/utils/isDefined';
|
|||||||
import { useDebouncedCallback } from 'use-debounce';
|
import { useDebouncedCallback } from 'use-debounce';
|
||||||
import { useGetOneServerlessFunctionSourceCode } from '@/settings/serverless-functions/hooks/useGetOneServerlessFunctionSourceCode';
|
import { useGetOneServerlessFunctionSourceCode } from '@/settings/serverless-functions/hooks/useGetOneServerlessFunctionSourceCode';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
|
import isEmpty from 'lodash.isempty';
|
||||||
|
|
||||||
const TAB_LIST_COMPONENT_ID = 'serverless-function-detail';
|
const TAB_LIST_COMPONENT_ID = 'serverless-function-detail';
|
||||||
|
|
||||||
@ -37,7 +38,7 @@ export const SettingsServerlessFunctionDetail = () => {
|
|||||||
const { executeOneServerlessFunction } = useExecuteOneServerlessFunction();
|
const { executeOneServerlessFunction } = useExecuteOneServerlessFunction();
|
||||||
const { updateOneServerlessFunction } = useUpdateOneServerlessFunction();
|
const { updateOneServerlessFunction } = useUpdateOneServerlessFunction();
|
||||||
const { publishOneServerlessFunction } = usePublishOneServerlessFunction();
|
const { publishOneServerlessFunction } = usePublishOneServerlessFunction();
|
||||||
const [formValues, setFormValues] =
|
const { formValues, setFormValues, loading } =
|
||||||
useServerlessFunctionUpdateFormState(serverlessFunctionId);
|
useServerlessFunctionUpdateFormState(serverlessFunctionId);
|
||||||
const { code: latestVersionCode } = useGetOneServerlessFunctionSourceCode({
|
const { code: latestVersionCode } = useGetOneServerlessFunctionSourceCode({
|
||||||
id: serverlessFunctionId,
|
id: serverlessFunctionId,
|
||||||
@ -52,6 +53,9 @@ export const SettingsServerlessFunctionDetail = () => {
|
|||||||
|
|
||||||
const save = async () => {
|
const save = async () => {
|
||||||
try {
|
try {
|
||||||
|
if (isEmpty(formValues.name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
await updateOneServerlessFunction({
|
await updateOneServerlessFunction({
|
||||||
id: serverlessFunctionId,
|
id: serverlessFunctionId,
|
||||||
name: formValues.name,
|
name: formValues.name,
|
||||||
@ -201,7 +205,7 @@ export const SettingsServerlessFunctionDetail = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
formValues.name && (
|
!loading && (
|
||||||
<SubMenuTopBarContainer
|
<SubMenuTopBarContainer
|
||||||
Icon={IconFunction}
|
Icon={IconFunction}
|
||||||
title={
|
title={
|
||||||
|
|||||||
@ -0,0 +1,19 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
export class RemoveServerlessFunctionUniqueConstraint1724423248330
|
||||||
|
implements MigrationInterface
|
||||||
|
{
|
||||||
|
name = 'RemoveServerlessFunctionUniqueConstraint1724423248330';
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."serverlessFunction" DROP CONSTRAINT "IndexOnNameAndWorkspaceIdUnique"`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."serverlessFunction" ADD CONSTRAINT "IndexOnNameAndWorkspaceIdUnique" UNIQUE ("name", "workspaceId")`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable no-console */
|
||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import { tmpdir } from 'os';
|
import { tmpdir } from 'os';
|
||||||
import { promises as fs } from 'fs';
|
import { promises as fs } from 'fs';
|
||||||
@ -137,7 +138,7 @@ export class LocalDriver
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
child.kill();
|
child.kill();
|
||||||
fs.unlink(tmpFilePath);
|
fs.unlink(tmpFilePath).catch(console.error);
|
||||||
});
|
});
|
||||||
|
|
||||||
child.stderr?.on('data', (data) => {
|
child.stderr?.on('data', (data) => {
|
||||||
@ -169,19 +170,19 @@ export class LocalDriver
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
child.kill();
|
child.kill();
|
||||||
fs.unlink(tmpFilePath);
|
fs.unlink(tmpFilePath).catch(console.error);
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('error', (error) => {
|
child.on('error', (error) => {
|
||||||
reject(error);
|
reject(error);
|
||||||
child.kill();
|
child.kill();
|
||||||
fs.unlink(tmpFilePath);
|
fs.unlink(tmpFilePath).catch(console.error);
|
||||||
});
|
});
|
||||||
|
|
||||||
child.on('exit', (code) => {
|
child.on('exit', (code) => {
|
||||||
if (code && code !== 0) {
|
if (code && code !== 0) {
|
||||||
reject(new Error(`Child process exited with code ${code}`));
|
reject(new Error(`Child process exited with code ${code}`));
|
||||||
fs.unlink(tmpFilePath);
|
fs.unlink(tmpFilePath).catch(console.error);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,6 @@ export class UpdateServerlessFunctionInput {
|
|||||||
description?: string;
|
description?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
|
||||||
@Field()
|
@Field()
|
||||||
code: string;
|
code: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,6 @@ export enum ServerlessFunctionRuntime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Entity('serverlessFunction')
|
@Entity('serverlessFunction')
|
||||||
@Unique('IndexOnNameAndWorkspaceIdUnique', ['name', 'workspaceId'])
|
|
||||||
export class ServerlessFunctionEntity {
|
export class ServerlessFunctionEntity {
|
||||||
@PrimaryGeneratedColumn('uuid')
|
@PrimaryGeneratedColumn('uuid')
|
||||||
id: string;
|
id: string;
|
||||||
|
|||||||
@ -240,18 +240,6 @@ export class ServerlessFunctionService extends TypeOrmQueryService<ServerlessFun
|
|||||||
code: FileUpload | string,
|
code: FileUpload | string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
) {
|
) {
|
||||||
const existingServerlessFunction =
|
|
||||||
await this.serverlessFunctionRepository.findOne({
|
|
||||||
where: { name: serverlessFunctionInput.name, workspaceId },
|
|
||||||
});
|
|
||||||
|
|
||||||
if (existingServerlessFunction) {
|
|
||||||
throw new ServerlessFunctionException(
|
|
||||||
`Function already exists`,
|
|
||||||
ServerlessFunctionExceptionCode.SERVERLESS_FUNCTION_ALREADY_EXIST,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
let typescriptCode: string;
|
let typescriptCode: string;
|
||||||
|
|
||||||
if (typeof code === 'string') {
|
if (typeof code === 'string') {
|
||||||
|
|||||||
Reference in New Issue
Block a user