From 598903df2ed9bfa215dded99e78716f00af54399 Mon Sep 17 00:00:00 2001 From: Amadeo Pellicce Date: Fri, 18 Oct 2024 01:54:46 +0200 Subject: [PATCH] adding migrate tables option (#49) * adding migrate tables option * fixing pg defs --- connectors/connector-postgres/def.ts | 3 ++- connectors/connector-postgres/server.ts | 6 +++++- packages/engine-backend/services/AuthProvider.ts | 5 +++++ packages/engine-backend/services/sync-service.ts | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/connectors/connector-postgres/def.ts b/connectors/connector-postgres/def.ts index 79662b45f..92f7c93c4 100644 --- a/connectors/connector-postgres/def.ts +++ b/connectors/connector-postgres/def.ts @@ -11,6 +11,7 @@ export const zPgConfig = z.object({ migrationsPath: z.string().optional(), migrationTableName: z.string().optional(), transformFieldNames: z.boolean().optional(), + migrateTables: z.boolean().optional(), }) export const postgresSchemas = { @@ -19,7 +20,7 @@ export const postgresSchemas = { // if it's resourceSettings then it doesn't make as much sense to configure // in the list of integrations... // How do we create default resources for integrations that are basically single resource? - resourceSettings: zPgConfig.pick({databaseUrl: true}).extend({ + resourceSettings: zPgConfig.pick({databaseUrl: true, migrateTables: true}).extend({ // gotta make sourceQueries a Textarea sourceQueries: z diff --git a/connectors/connector-postgres/server.ts b/connectors/connector-postgres/server.ts index d1a073ed0..8a6f5271b 100644 --- a/connectors/connector-postgres/server.ts +++ b/connectors/connector-postgres/server.ts @@ -193,7 +193,7 @@ export const postgresServer = { ), ) }, - destinationSync: ({endUser, source, settings: {databaseUrl}}) => { + destinationSync: ({endUser, source, settings: {databaseUrl, migrateTables}}) => { console.log('[destinationSync] Will makePostgresClient', { // databaseUrl, // migrationsPath: __dirname + '/migrations', @@ -210,6 +210,10 @@ export const postgresServer = { const migrationRan: Record = {} async function runMigration(pool: DatabasePool, tableName: string) { + if (!migrateTables) { + console.log('[destinationSync] Will not run migration for', tableName, 'as migrateTables is false'); + return; + } console.log('will run migration for', tableName) if (migrationRan[tableName]) { return diff --git a/packages/engine-backend/services/AuthProvider.ts b/packages/engine-backend/services/AuthProvider.ts index b9b7f61ed..ed34ffd12 100644 --- a/packages/engine-backend/services/AuthProvider.ts +++ b/packages/engine-backend/services/AuthProvider.ts @@ -32,6 +32,11 @@ export const zOrganization = z.object({ description: 'Events like sync.completed and connection.created can be sent to url of your choosing', }), + migrate_tables: z.boolean().optional().openapi({ + title: 'Migrate Tables', + description: 'If enabled, table migrations will be run if needed when entities are persisted', + default: true, + }), }), privateMetadata: z .object({ diff --git a/packages/engine-backend/services/sync-service.ts b/packages/engine-backend/services/sync-service.ts index 4e973f3af..2d36bcc21 100644 --- a/packages/engine-backend/services/sync-service.ts +++ b/packages/engine-backend/services/sync-service.ts @@ -77,7 +77,7 @@ export function makeSyncService({ await metaLinks.patch('resource', defaultDestId, { connectorConfigId: dCcfgId, // Should always snake_case here. This is also not typesafe... - settings: {databaseUrl: org.publicMetadata.database_url}, + settings: {databaseUrl: org.publicMetadata.database_url, migrateTables: org.publicMetadata.migrate_tables}, }) console.log('Created default resource', defaultDestId) }