5
5
"errors"
6
6
"fmt"
7
7
"log/slog"
8
+ "maps"
8
9
"os"
10
+ "slices"
9
11
"strconv"
10
12
"sync/atomic"
11
13
"time"
@@ -124,6 +126,12 @@ func (a *FlowableActivity) EnsurePullability(
124
126
}
125
127
defer connectors .CloseConnector (ctx , srcConn )
126
128
129
+ cfg , err := internal .FetchConfigFromDB (config .FlowJobName )
130
+ if err != nil {
131
+ return nil , a .Alerter .LogFlowError (ctx , config .FlowJobName , fmt .Errorf ("failed to fetch config from DB: %w" , err ))
132
+ }
133
+
134
+ config .SourceTableIdentifiers = slices .Sorted (maps .Keys (internal .TableNameMapping (cfg .TableMappings )))
127
135
output , err := srcConn .EnsurePullability (ctx , config )
128
136
if err != nil {
129
137
return nil , a .Alerter .LogFlowError (ctx , config .FlowJobName , fmt .Errorf ("failed to ensure pullability: %w" , err ))
@@ -165,6 +173,18 @@ func (a *FlowableActivity) SetupTableSchema(
165
173
})
166
174
defer shutdown ()
167
175
176
+ // have to fetch config from the DB
177
+ cfg , err := internal .FetchConfigFromDB (config .FlowName )
178
+ if err != nil {
179
+ return a .Alerter .LogFlowError (ctx , config .FlowName , fmt .Errorf ("failed to fetch config from DB: %w" , err ))
180
+ }
181
+ tableMappings := cfg .TableMappings
182
+ if len (config .FilteredTableMappings ) > 0 {
183
+ // we use the filtered table mappings if provided. they are provided from
184
+ // the sync flow which includes changes to the schema.
185
+ tableMappings = config .FilteredTableMappings
186
+ }
187
+
168
188
logger := internal .LoggerFromCtx (ctx )
169
189
ctx = context .WithValue (ctx , shared .FlowNameKey , config .FlowName )
170
190
srcConn , err := connectors .GetByNameAs [connectors.GetTableSchemaConnector ](ctx , config .Env , a .CatalogPool , config .PeerName )
@@ -173,11 +193,11 @@ func (a *FlowableActivity) SetupTableSchema(
173
193
}
174
194
defer connectors .CloseConnector (ctx , srcConn )
175
195
176
- tableNameSchemaMapping , err := srcConn .GetTableSchema (ctx , config .Env , config .Version , config .System , config . TableMappings )
196
+ tableNameSchemaMapping , err := srcConn .GetTableSchema (ctx , config .Env , config .Version , config .System , tableMappings )
177
197
if err != nil {
178
198
return a .Alerter .LogFlowError (ctx , config .FlowName , fmt .Errorf ("failed to get GetTableSchemaConnector: %w" , err ))
179
199
}
180
- processed := internal .BuildProcessedSchemaMapping (config . TableMappings , tableNameSchemaMapping , logger )
200
+ processed := internal .BuildProcessedSchemaMapping (tableMappings , tableNameSchemaMapping , logger )
181
201
182
202
tx , err := a .CatalogPool .BeginTx (ctx , pgx.TxOptions {})
183
203
if err != nil {
@@ -213,6 +233,13 @@ func (a *FlowableActivity) CreateNormalizedTable(
213
233
numTablesSetup := atomic.Uint32 {}
214
234
numTablesToSetup := atomic.Int32 {}
215
235
236
+ cfg , err := internal .FetchConfigFromDB (config .FlowName )
237
+ if err != nil {
238
+ return nil , a .Alerter .LogFlowError (ctx , config .FlowName , fmt .Errorf ("failed to fetch config from DB: %w" , err ))
239
+ }
240
+
241
+ tableMappings := cfg .TableMappings
242
+
216
243
shutdown := heartbeatRoutine (ctx , func () string {
217
244
return fmt .Sprintf ("setting up normalized tables - %d of %d done" , numTablesSetup .Load (), numTablesToSetup .Load ())
218
245
})
@@ -244,7 +271,7 @@ func (a *FlowableActivity) CreateNormalizedTable(
244
271
245
272
numTablesToSetup .Store (int32 (len (tableNameSchemaMapping )))
246
273
tableExistsMapping := make (map [string ]bool , len (tableNameSchemaMapping ))
247
- for _ , tableMapping := range config . TableMappings {
274
+ for _ , tableMapping := range tableMappings {
248
275
tableIdentifier := tableMapping .DestinationTableIdentifier
249
276
tableSchema := tableNameSchemaMapping [tableIdentifier ]
250
277
existing , err := conn .SetupNormalizedTable (
@@ -292,6 +319,12 @@ func (a *FlowableActivity) SyncFlow(
292
319
var syncingBatchID atomic.Int64
293
320
var syncState atomic.Pointer [string ]
294
321
syncState .Store (shared .Ptr ("setup" ))
322
+
323
+ config , err := internal .FetchConfigFromDB (config .FlowJobName )
324
+ if err != nil {
325
+ return fmt .Errorf ("unable to query flow config from catalog: %w" , err )
326
+ }
327
+
295
328
shutdown := heartbeatRoutine (ctx , func () string {
296
329
// Must load Waiting after BatchID to avoid race saying we're waiting on currently processing batch
297
330
sBatchID := syncingBatchID .Load ()
@@ -363,10 +396,10 @@ func (a *FlowableActivity) SyncFlow(
363
396
var syncResponse * model.SyncResponse
364
397
var syncErr error
365
398
if config .System == protos .TypeSystem_Q {
366
- syncResponse , syncErr = a .syncRecords (groupCtx , config , options , srcConn .(connectors.CDCPullConnector ),
399
+ syncResponse , syncErr = a .syncRecords (groupCtx , config , srcConn .(connectors.CDCPullConnector ),
367
400
normRequests , normResponses , normBufferSize , & syncingBatchID , & syncState )
368
401
} else {
369
- syncResponse , syncErr = a .syncPg (groupCtx , config , options , srcConn .(connectors.CDCPullPgConnector ),
402
+ syncResponse , syncErr = a .syncPg (groupCtx , config , srcConn .(connectors.CDCPullPgConnector ),
370
403
normRequests , normResponses , normBufferSize , & syncingBatchID , & syncState )
371
404
}
372
405
@@ -414,7 +447,6 @@ func (a *FlowableActivity) SyncFlow(
414
447
func (a * FlowableActivity ) syncRecords (
415
448
ctx context.Context ,
416
449
config * protos.FlowConnectionConfigs ,
417
- options * protos.SyncFlowOptions ,
418
450
srcConn connectors.CDCPullConnector ,
419
451
normRequests * concurrency.LastChan ,
420
452
normResponses * concurrency.LastChan ,
@@ -451,7 +483,7 @@ func (a *FlowableActivity) syncRecords(
451
483
return stream , nil
452
484
}
453
485
}
454
- return syncCore (ctx , a , config , options , srcConn ,
486
+ return syncCore (ctx , a , config , srcConn ,
455
487
normRequests , normResponses , normBufferSize ,
456
488
syncingBatchID , syncWaiting , adaptStream ,
457
489
connectors .CDCPullConnector .PullRecords ,
@@ -461,15 +493,14 @@ func (a *FlowableActivity) syncRecords(
461
493
func (a * FlowableActivity ) syncPg (
462
494
ctx context.Context ,
463
495
config * protos.FlowConnectionConfigs ,
464
- options * protos.SyncFlowOptions ,
465
496
srcConn connectors.CDCPullPgConnector ,
466
497
normRequests * concurrency.LastChan ,
467
498
normResponses * concurrency.LastChan ,
468
499
normBufferSize int64 ,
469
500
syncingBatchID * atomic.Int64 ,
470
501
syncWaiting * atomic.Pointer [string ],
471
502
) (* model.SyncResponse , error ) {
472
- return syncCore (ctx , a , config , options , srcConn ,
503
+ return syncCore (ctx , a , config , srcConn ,
473
504
normRequests , normResponses , normBufferSize ,
474
505
syncingBatchID , syncWaiting , nil ,
475
506
connectors .CDCPullPgConnector .PullPg ,
@@ -1214,9 +1245,13 @@ func (a *FlowableActivity) ReplicateXminPartition(ctx context.Context,
1214
1245
}
1215
1246
}
1216
1247
1217
- func (a * FlowableActivity ) AddTablesToPublication (ctx context.Context , cfg * protos. FlowConnectionConfigs ,
1248
+ func (a * FlowableActivity ) AddTablesToPublication (ctx context.Context , flowJobName string ,
1218
1249
additionalTableMappings []* protos.TableMapping ,
1219
1250
) error {
1251
+ cfg , err := internal .FetchConfigFromDB (flowJobName )
1252
+ if err != nil {
1253
+ return fmt .Errorf ("unable to query flow config from catalog: %w" , err )
1254
+ }
1220
1255
ctx = context .WithValue (ctx , shared .FlowNameKey , cfg .FlowJobName )
1221
1256
srcConn , err := connectors .GetByNameAs [* connpostgres.PostgresConnector ](ctx , cfg .Env , a .CatalogPool , cfg .SourceName )
1222
1257
if err != nil {
0 commit comments