|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Services |
| 4 | + module Airbyte |
| 5 | + # Fetches the the current schema for given sourec - Also reloads cache |
| 6 | + class ConnectionUpdate |
| 7 | + CURSOR_FIELD = '_ab_cdc_lsn' |
| 8 | + DEFAULT_PRIMARY_KEY = 'id' |
| 9 | + SYNC_MODE = 'incremental' |
| 10 | + DESTINATION_SYNC_MODE = 'append_dedup' |
| 11 | + |
| 12 | + class Error < StandardError; end |
| 13 | + |
| 14 | + def self.call(access_token:, connection_id:, allowed_list:, discovered_schema:) |
| 15 | + new(access_token, connection_id, allowed_list, discovered_schema).call |
| 16 | + end |
| 17 | + |
| 18 | + def initialize(access_token, connection_id, allowed_list, discovered_schema) |
| 19 | + @access_token = access_token |
| 20 | + @connection_id = connection_id |
| 21 | + @allowed_list = allowed_list |
| 22 | + @discovered_streams = discovered_schema&.dig('catalog', 'streams') |
| 23 | + end |
| 24 | + |
| 25 | + def call |
| 26 | + response = HTTParty.post( |
| 27 | + "#{config.airbyte_server_url}/api/v1/connections/update", |
| 28 | + headers: { |
| 29 | + 'Authorization' => "Bearer #{@access_token}", |
| 30 | + 'Content-Type' => 'application/json' |
| 31 | + }, |
| 32 | + body: connection_update_payload.to_json |
| 33 | + ) |
| 34 | + |
| 35 | + unless response.success? |
| 36 | + error_message = "Error calling Airbyte discover_schema API: status: #{response.code} body: #{response.body}" |
| 37 | + Rails.logger.error(error_message) |
| 38 | + raise Error, error_message |
| 39 | + end |
| 40 | + |
| 41 | + response.parsed_response |
| 42 | + end |
| 43 | + |
| 44 | + private |
| 45 | + |
| 46 | + def config |
| 47 | + DfE::Analytics.config |
| 48 | + end |
| 49 | + |
| 50 | + def discovered_stream_for(stream_name) |
| 51 | + discovered_stream = @discovered_streams.find { |s| s.dig('stream', 'name') == stream_name.to_s } if @discovered_streams.present? |
| 52 | + |
| 53 | + return discovered_stream if discovered_stream.present? |
| 54 | + |
| 55 | + error_message = "Stream definition not found in discovered_schema for: #{stream_name}" |
| 56 | + Rails.logger.error(error_message) |
| 57 | + raise Error, error_message |
| 58 | + end |
| 59 | + |
| 60 | + def connection_update_payload |
| 61 | + { |
| 62 | + connectionId: @connection_id, |
| 63 | + syncCatalog: { |
| 64 | + streams: @allowed_list.map do |stream_name, fields| |
| 65 | + discovered_stream = discovered_stream_for(stream_name) |
| 66 | + { |
| 67 | + stream: { |
| 68 | + name: stream_name.to_s, |
| 69 | + namespace: discovered_stream.dig('stream', 'namespace'), |
| 70 | + jsonSchema: discovered_stream.dig('stream', 'jsonSchema'), |
| 71 | + supportedSyncModes: discovered_stream.dig('stream', 'supportedSyncModes'), |
| 72 | + defaultCursorField: discovered_stream.dig('stream', 'defaultCursorField'), |
| 73 | + sourceDefinedCursor: discovered_stream.dig('stream', 'sourceDefinedCursor'), |
| 74 | + sourceDefinedPrimaryKey: discovered_stream.dig('stream', 'sourceDefinedPrimaryKey'), |
| 75 | + }, |
| 76 | + config: { |
| 77 | + syncMode: discovered_stream.dig('config', 'syncMode') || SYNC_MODE, |
| 78 | + destinationSyncMode: discovered_stream.dig('config', 'destinationSyncMode') || DESTINATION_SYNC_MODE, |
| 79 | + cursorField: discovered_stream.dig('config', 'cursorField') || [CURSOR_FIELD], |
| 80 | + primaryKey: discovered_stream.dig('config', 'primaryKey') || [[DEFAULT_PRIMARY_KEY]], |
| 81 | + aliasName: stream_name.to_s, |
| 82 | + selected: true, |
| 83 | + fieldSelectionEnabled: true, |
| 84 | + selectedFields: ([CURSOR_FIELD] + fields).uniq.map { |f| { fieldPath: [f] } } |
| 85 | + } |
| 86 | + } |
| 87 | + end |
| 88 | + } |
| 89 | + } |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments