@@ -34,7 +34,7 @@ def self.mark_synced(record, airtable_id)
3434 )
3535 end
3636
37- def self . sync! ( classname , limit : nil , sync_all : false )
37+ def self . sync! ( classname , limit : nil , sync_all : false , no_upload : false )
3838 batch = false
3939 klass = resolve_class ( classname )
4040 validate_sync_methods! ( klass )
@@ -50,14 +50,16 @@ def self.sync!(classname, limit: nil, sync_all: false)
5050 airtable_ids = [ ]
5151
5252 if batch
53- batch_sync! ( table_id , records , klass . airtable_sync_sync_id , field_mappings )
53+ batch_sync! ( table_id , records , klass . airtable_sync_sync_id , field_mappings , no_upload : )
5454 else
5555 records . each do |record |
5656 old_airtable_id = find_by ( record_identifier : build_identifier ( record ) ) &.airtable_id
5757 airtable_ids << individual_sync! ( table_id , record , field_mappings , old_airtable_id )
5858 end
5959 end
6060
61+ return records if no_upload
62+
6163 sync_data = records . map do |record |
6264 data = {
6365 record_identifier : build_identifier ( record ) ,
@@ -76,7 +78,7 @@ def self.sync!(classname, limit: nil, sync_all: false)
7678 records
7779 end
7880
79- def self . batch_sync! ( table_id , records , sync_id , mappings )
81+ def self . batch_sync! ( table_id , records , sync_id , mappings , no_upload : false )
8082 csv_string = CSV . generate do |csv |
8183 csv << mappings . keys
8284
@@ -86,6 +88,14 @@ def self.batch_sync!(table_id, records, sync_id, mappings)
8688 end
8789 end
8890
91+ if no_upload
92+ filename = "airtable_sync_#{ table_id } _#{ Time . current . strftime ( '%Y%m%d_%H%M%S' ) } .csv"
93+ filepath = Rails . root . join ( "tmp" , filename )
94+ File . write ( filepath , csv_string )
95+ Rails . logger . info ( "Airtable batch sync saved locally: #{ filepath } " )
96+ return filepath
97+ end
98+
8999 response = Faraday . post ( "https://api.airtable.com/v0/#{ ENV [ 'AIRTABLE_BASE_ID' ] } /#{ table_id } /sync/#{ sync_id } " ) do |req |
90100 req . headers = {
91101 "Authorization" => "Bearer #{ ENV [ 'AIRTABLE_PAT' ] } " ,
0 commit comments