[feat] Add updateRemoteServer endpoint (#5148)
## Context #4765 Following investigations ([#5083](https://github.com/twentyhq/twenty/issues/5083)) we decided to restrict updates of server from which zero tables have been synchronized only ## How was it tested Locally with /metadata 1. Updating a database that already has synchronized tables <img width="1072" alt="Capture d’écran 2024-04-24 à 16 16 05" src="https://github.com/twentyhq/twenty/assets/51697796/f9a84c34-2dcd-4f3c-b0bc-b710abae5021"> 2. Updating a database that has no synchronized tables <img width="843" alt="Capture d’écran 2024-04-24 à 16 17 28" src="https://github.com/twentyhq/twenty/assets/51697796/f320fe03-a6bc-4724-bcd0-4e89d3ac31f5"> + tested that the connection works well
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { isDefined } from 'class-validator';
|
||||
|
||||
import {
|
||||
ForeignDataWrapperOptions,
|
||||
RemoteServerType,
|
||||
@ -21,6 +23,20 @@ export class ForeignDataWrapperQueryFactory {
|
||||
return `CREATE SERVER "${foreignDataWrapperId}" FOREIGN DATA WRAPPER ${name} OPTIONS (${options})`;
|
||||
}
|
||||
|
||||
updateForeignDataWrapper({
|
||||
foreignDataWrapperId,
|
||||
foreignDataWrapperOptions,
|
||||
}: {
|
||||
foreignDataWrapperId: string;
|
||||
foreignDataWrapperOptions: Partial<
|
||||
ForeignDataWrapperOptions<RemoteServerType>
|
||||
>;
|
||||
}) {
|
||||
const options = this.buildUpdateOptions(foreignDataWrapperOptions);
|
||||
|
||||
return `ALTER SERVER "${foreignDataWrapperId}" OPTIONS (${options})`;
|
||||
}
|
||||
|
||||
createUserMapping(
|
||||
foreignDataWrapperId: string,
|
||||
userMappingOptions: UserMappingOptions,
|
||||
@ -29,6 +45,16 @@ export class ForeignDataWrapperQueryFactory {
|
||||
return `CREATE USER MAPPING IF NOT EXISTS FOR CURRENT_USER SERVER "${foreignDataWrapperId}" OPTIONS (user '${userMappingOptions.username}', password '${userMappingOptions.password}')`;
|
||||
}
|
||||
|
||||
updateUserMapping(
|
||||
foreignDataWrapperId: string,
|
||||
userMappingOptions: Partial<UserMappingOptions>,
|
||||
) {
|
||||
const options = this.buildUpdateUserMappingOptions(userMappingOptions);
|
||||
|
||||
// CURRENT_USER works for now since we are using only one user. But if we switch to a user per workspace, we need to change this.
|
||||
return `ALTER USER MAPPING FOR CURRENT_USER SERVER "${foreignDataWrapperId}" OPTIONS (${options})`;
|
||||
}
|
||||
|
||||
private buildNameAndOptionsFromType(
|
||||
type: RemoteServerType,
|
||||
options: ForeignDataWrapperOptions<RemoteServerType>,
|
||||
@ -41,6 +67,36 @@ export class ForeignDataWrapperQueryFactory {
|
||||
}
|
||||
}
|
||||
|
||||
private buildUpdateOptions(
|
||||
options: Partial<ForeignDataWrapperOptions<RemoteServerType>>,
|
||||
) {
|
||||
const rawQuerySetStatements: string[] = [];
|
||||
|
||||
Object.entries(options).forEach(([key, value]) => {
|
||||
if (isDefined(value)) {
|
||||
rawQuerySetStatements.push(`SET ${key} '${value}'`);
|
||||
}
|
||||
});
|
||||
|
||||
return rawQuerySetStatements.join(', ');
|
||||
}
|
||||
|
||||
private buildUpdateUserMappingOptions(
|
||||
userMappingOptions?: Partial<UserMappingOptions>,
|
||||
) {
|
||||
const setStatements: string[] = [];
|
||||
|
||||
if (isDefined(userMappingOptions?.username)) {
|
||||
setStatements.push(`SET user '${userMappingOptions?.username}'`);
|
||||
}
|
||||
|
||||
if (isDefined(userMappingOptions?.password)) {
|
||||
setStatements.push(`SET password '${userMappingOptions?.password}'`);
|
||||
}
|
||||
|
||||
return setStatements.join(', ');
|
||||
}
|
||||
|
||||
private buildPostgresFDWQueryOptions(
|
||||
foreignDataWrapperOptions: ForeignDataWrapperOptions<RemoteServerType>,
|
||||
) {
|
||||
|
||||
Reference in New Issue
Block a user