@@ -12,67 +12,51 @@ class ConnectionUpdate
1212
1313 class Error < StandardError ; end
1414
15- def self . call ( access_token :, allowed_list :, discovered_schema : )
16- new ( access_token , allowed_list , discovered_schema ) . call
15+ def self . call ( access_token :, allowed_list :)
16+ new ( access_token , allowed_list ) . call
1717 end
1818
19- def initialize ( access_token , allowed_list , discovered_schema )
19+ def initialize ( access_token , allowed_list )
20+ raise Error , 'allowed_list must be a Hash of table_name => fields' unless allowed_list . is_a? ( Hash )
21+
2022 @access_token = access_token
2123 @allowed_list = allowed_list
22- @discovered_streams = discovered_schema &. dig ( 'catalog' , 'streams' )
24+ @connection_id = DfE :: Analytics . config . airbyte_configuration [ :connection_id ]
2325 end
2426
2527 def call
26- Services ::Airbyte ::ApiServer . post (
27- path : ' /api/v1/connections/update' ,
28+ Services ::Airbyte ::ApiServer . patch (
29+ path : " /api/public/ v1/connections/#{ @connection_id } " ,
2830 access_token : @access_token ,
29- payload : connection_update_payload
31+ payload : connection_patch_payload
3032 )
3133 end
3234
3335 private
3436
35- def discovered_stream_for ( stream_name )
36- discovered_stream = @discovered_streams . find { |s | s . dig ( 'stream' , 'name' ) == stream_name . to_s } if @discovered_streams . present?
37-
38- return discovered_stream if discovered_stream . present?
39-
40- error_message = "Stream definition not found in discovered_schema for: #{ stream_name } "
41- Rails . logger . error ( error_message )
42- raise Error , error_message
43- end
44-
45- def connection_update_payload
37+ def connection_patch_payload
4638 {
47- connectionId : DfE ::Analytics . config . airbyte_configuration [ :connection_id ] ,
48- syncCatalog : {
39+ configurations : {
4940 streams : @allowed_list . map do |stream_name , fields |
50- discovered_stream = discovered_stream_for ( stream_name )
5141 {
52- stream : {
53- name : stream_name . to_s ,
54- namespace : discovered_stream . dig ( 'stream' , 'namespace' ) ,
55- jsonSchema : discovered_stream . dig ( 'stream' , 'jsonSchema' ) ,
56- supportedSyncModes : discovered_stream . dig ( 'stream' , 'supportedSyncModes' ) ,
57- defaultCursorField : discovered_stream . dig ( 'stream' , 'defaultCursorField' ) ,
58- sourceDefinedCursor : discovered_stream . dig ( 'stream' , 'sourceDefinedCursor' ) ,
59- sourceDefinedPrimaryKey : discovered_stream . dig ( 'stream' , 'sourceDefinedPrimaryKey' )
60- } ,
61- config : {
62- syncMode : SYNC_MODE ,
63- destinationSyncMode : DESTINATION_SYNC_MODE ,
64- cursorField : discovered_stream . dig ( 'config' , 'cursorField' ) || CURSOR_FIELD ,
65- primaryKey : discovered_stream . dig ( 'config' , 'primaryKey' ) || [ [ DEFAULT_PRIMARY_KEY ] ] ,
66- aliasName : stream_name . to_s ,
67- selected : true ,
68- fieldSelectionEnabled : true ,
69- selectedFields : ( CURSOR_FIELD + AIRBYTE_FIELDS + fields ) . uniq . map { |f | { fieldPath : [ f ] } }
70- }
42+ name : stream_name . to_s ,
43+ selected : true ,
44+ syncMode : SYNC_MODE ,
45+ destinationSyncMode : DESTINATION_SYNC_MODE ,
46+ cursorField : CURSOR_FIELD ,
47+ primaryKey : [ [ DEFAULT_PRIMARY_KEY ] ] ,
48+ selectedFields : selected_fields ( fields )
7149 }
7250 end
7351 }
7452 }
7553 end
54+
55+ def selected_fields ( fields )
56+ ( CURSOR_FIELD + AIRBYTE_FIELDS + fields ) . uniq . map do |field |
57+ { fieldPath : [ field ] }
58+ end
59+ end
7660 end
7761 end
7862end
0 commit comments