1- import type { ConnectionOptions } from 'node:tls'
21import * as schema from '@sim/db'
32import { workflow , workflowBlocks , workflowEdges , workflowSubflows } from '@sim/db'
43import { and , eq , or , sql } from 'drizzle-orm'
@@ -11,34 +10,6 @@ import { loadWorkflowFromNormalizedTables } from '@/lib/workflows/db-helpers'
1110const logger = createLogger ( 'SocketDatabase' )
1211
1312const connectionString = env . DATABASE_URL
14-
15- const getSSLConfig = ( ) => {
16- const sslMode = env . DATABASE_SSL
17-
18- if ( ! sslMode ) return undefined
19- if ( sslMode === 'disable' ) return false
20- if ( sslMode === 'prefer' ) return 'prefer'
21-
22- const sslConfig : ConnectionOptions = { }
23-
24- if ( sslMode === 'require' ) {
25- sslConfig . rejectUnauthorized = false
26- } else if ( sslMode === 'verify-ca' || sslMode === 'verify-full' ) {
27- sslConfig . rejectUnauthorized = true
28- if ( env . DATABASE_SSL_CA ) {
29- try {
30- const ca = Buffer . from ( env . DATABASE_SSL_CA , 'base64' ) . toString ( 'utf-8' )
31- sslConfig . ca = ca
32- } catch ( error ) {
33- logger . error ( 'Failed to parse DATABASE_SSL_CA:' , error )
34- }
35- }
36- }
37-
38- return sslConfig
39- }
40-
41- const sslConfig = getSSLConfig ( )
4213const socketDb = drizzle (
4314 postgres ( connectionString , {
4415 prepare : false ,
@@ -47,7 +18,6 @@ const socketDb = drizzle(
4718 max : 25 ,
4819 onnotice : ( ) => { } ,
4920 debug : false ,
50- ...( sslConfig !== undefined && { ssl : sslConfig } ) ,
5121 } ) ,
5222 { schema }
5323)
@@ -162,6 +132,7 @@ export async function getWorkflowState(workflowId: string) {
162132 const finalState = {
163133 // Default values for expected properties
164134 deploymentStatuses : { } ,
135+ hasActiveWebhook : false ,
165136 // Data from normalized tables
166137 blocks : normalizedData . blocks ,
167138 edges : normalizedData . edges ,
@@ -195,7 +166,23 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
195166 try {
196167 const { operation : op , target, payload, timestamp, userId } = operation
197168
169+ // Log high-frequency operations for monitoring
170+ if ( op === 'update-position' && Math . random ( ) < 0.01 ) {
171+ // Log 1% of position updates
172+ logger . debug ( 'Socket DB operation sample:' , {
173+ operation : op ,
174+ target,
175+ workflowId : `${ workflowId . substring ( 0 , 8 ) } ...` ,
176+ } )
177+ }
178+
198179 await db . transaction ( async ( tx ) => {
180+ // Update the workflow's last modified timestamp first
181+ await tx
182+ . update ( workflow )
183+ . set ( { updatedAt : new Date ( timestamp ) } )
184+ . where ( eq ( workflow . id , workflowId ) )
185+
199186 // Handle different operation types within the transaction
200187 switch ( target ) {
201188 case 'block' :
@@ -213,13 +200,6 @@ export async function persistWorkflowOperation(workflowId: string, operation: an
213200 default :
214201 throw new Error ( `Unknown operation target: ${ target } ` )
215202 }
216-
217- if ( op !== 'update-position' ) {
218- await tx
219- . update ( workflow )
220- . set ( { updatedAt : new Date ( timestamp ) } )
221- . where ( eq ( workflow . id , workflowId ) )
222- }
223203 } )
224204
225205 // Log slow operations for monitoring
0 commit comments