@@ -474,20 +474,13 @@ def update_or_create_udt_accounts!(local_block)
474474 end
475475
476476 if new_udt_accounts_attributes . present?
477- udt_attrs = new_udt_accounts_attributes . map! do |attr |
478- attr . merge! ( created_at : Time . current ,
479- updated_at : Time . current )
480- end
481- udt_attrs . each_slice ( 500 ) do |batch |
482- UdtAccount . insert_all! ( batch )
477+ new_udt_accounts_attributes . each_slice ( 500 ) do |batch |
478+ UdtAccount . insert_all! ( batch , record_timestamps : true )
483479 end
484480 end
485481 if udt_accounts_attributes . present?
486- udt_accounts_attrs = udt_accounts_attributes . map! do |attr |
487- attr . merge! ( updated_at : Time . current )
488- end
489- udt_accounts_attrs . each_slice ( 500 ) do |batch |
490- UdtAccount . upsert_all ( batch )
482+ udt_accounts_attributes . each_slice ( 500 ) do |batch |
483+ UdtAccount . upsert_all ( batch , record_timestamps : true )
491484 end
492485 end
493486 end
@@ -786,10 +779,7 @@ def update_ckb_txs_rel_and_fee(
786779 end
787780 end
788781 if full_tx_address_ids . present?
789- full_tx_address_ids . each_slice ( 500 ) do |batch |
790- AccountBook . upsert_all batch ,
791- unique_by : %i[ address_id ckb_transaction_id ]
792- end
782+ AccountBook . import full_tx_address_ids , validate : false , batch_size : 500
793783 end
794784 if full_tx_udt_ids . present?
795785 full_tx_udt_ids . each_slice ( 500 ) do |batch |
@@ -839,8 +829,8 @@ def build_cells_and_locks!(
839829 CellOutput . pending . where ( "tx_hash IN (#{ binary_hashes } )" ) . update_all ( status : :live )
840830 id_hashes = [ ]
841831 cell_outputs_attributes . each_slice ( 500 ) do |batch |
842- id_hashes . concat CellOutput . upsert_all ( batch , unique_by : %i[ tx_hash cell_index status ] ,
843- returning : %i[ id data_hash ] )
832+ result = CellOutput . import ( batch , validate : false , returning : %i[ id data_hash ] ) . results
833+ id_hashes . concat result . map { | item | { "id" => item [ 0 ] , "data_hash" => item [ 1 ] } }
844834 end
845835 cell_data_attrs = [ ]
846836
@@ -855,9 +845,7 @@ def build_cells_and_locks!(
855845 end
856846
857847 if cell_data_attrs . present?
858- cell_data_attrs . each_slice ( 500 ) do |batch |
859- CellDatum . upsert_all ( batch , unique_by : [ :cell_output_id ] )
860- end
848+ CellDatum . import cell_data_attrs , validate : false , batch_size : 500
861849 end
862850 end
863851
@@ -885,28 +873,51 @@ def build_cells_and_locks!(
885873 end
886874 end
887875 if cell_deps_attrs . present?
888- cell_deps_attrs . each_slice ( 500 ) do |batch |
889- CellDependency . upsert_all ( batch ,
890- unique_by : %i[ ckb_transaction_id contract_cell_id dep_type ] )
891- end
876+ CellDependency . import cell_deps_attrs , validate : false , batch_size : 500
892877 end
893878
894879 prev_outputs = nil
895880 build_cell_inputs ( inputs , ckb_txs , local_block . id , cell_inputs_attributes , prev_cell_outputs_attributes ,
896881 input_capacities , tags , udt_address_ids , contained_udt_ids , contained_addr_ids ,
897882 prev_outputs , addrs_changes , token_transfer_ckb_tx_ids )
898883
899- cell_inputs_attributes . each_slice ( 500 ) do |batch |
900- CellInput . upsert_all ( batch ,
901- unique_by : %i[ ckb_transaction_id index ] )
902- end
884+ CellInput . import cell_inputs_attributes , validate : false , batch_size : 500
903885 if prev_cell_outputs_attributes . present?
904- cell_ouput_ids = prev_cell_outputs_attributes . pluck ( :id )
905- CellOutput . live . where ( id : cell_ouput_ids ) . update_all ( status : :dead )
906886 prev_cell_outputs_attributes . each_slice ( 500 ) do |batch |
907- CellOutput . upsert_all ( batch ,
908- unique_by : %i[ tx_hash cell_index status ] ,
909- record_timestamps : true )
887+ updates = batch . map do |attr |
888+ [ attr [ :id ] , attr . slice ( :status , :consumed_by_id , :consumed_block_timestamp ) ]
889+ end . to_h
890+
891+ case_clauses = { status : [ ] , consumed_by_id : [ ] , consumed_block_timestamp : [ ] }
892+ ids = [ ]
893+
894+ updates . each do |id , attrs |
895+ ids << id
896+ attrs . each do |column , value |
897+ case_clauses [ column ] << "WHEN #{ id } THEN '#{ ActiveRecord ::Base . connection . quote ( value ) } '"
898+ end
899+ end
900+
901+ set_clauses = case_clauses . map do |column , clauses |
902+ if clauses . any?
903+ " #{ column } = CASE id\n #{ clauses . join ( "\n " ) } \n ELSE #{ column } \n END"
904+ else
905+ nil
906+ end
907+ end . compact . join ( ",\n " )
908+
909+ id_list = ids . join ( ', ' )
910+
911+ sql = <<-SQL
912+ UPDATE cell_outputs
913+ SET
914+ #{ set_clauses }
915+ WHERE id IN (#{ id_list } )
916+ SQL
917+
918+ # puts sql
919+
920+ ActiveRecord ::Base . connection . execute ( sql )
910921 end
911922 end
912923
@@ -1295,7 +1306,7 @@ def cell_input_attributes(input, ckb_transaction_id, local_block_id,
12951306 cell_type : previous_output . cell_type ,
12961307 tx_hash : input . previous_output . tx_hash ,
12971308 cell_index : input . previous_output . index ,
1298- status : "dead" ,
1309+ status : 1 ,
12991310 consumed_by_id : ckb_transaction_id ,
13001311 consumed_block_timestamp : @local_block . timestamp ,
13011312 } ,
@@ -1337,8 +1348,8 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
13371348 CkbTransaction . where ( tx_status : :pending ) . where ( "tx_hash IN (#{ binary_hashes } )" ) . update_all tx_status : "committed" if pending_txs . size > 0
13381349 txs = [ ]
13391350 ckb_transactions_attributes . each_slice ( 500 ) do |batch |
1340- txs . concat CkbTransaction . upsert_all ( batch , unique_by : %i[ tx_status tx_hash ] ,
1341- returning : %w( id tx_hash tx_index block_timestamp block_number created_at ) )
1351+ results = CkbTransaction . import ( batch , validate : false , returning : %w( id tx_hash tx_index block_timestamp block_number created_at ) ) . results
1352+ txs . concat results . map { | item | { "id" => item [ 0 ] , "tx_hash" => item [ 1 ] , "tx_index" => item [ 2 ] , "block_timestamp" => item [ 3 ] , "block_number" => item [ 4 ] , "created_at" => item [ 5 ] } }
13421353 end
13431354
13441355 if pending_txs . any?
@@ -1379,10 +1390,7 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
13791390 end
13801391 end
13811392 if header_deps_attrs . present?
1382- header_deps_attrs . each_slice ( 500 ) do |batch |
1383- HeaderDependency . upsert_all ( batch ,
1384- unique_by : %i[ ckb_transaction_id index ] )
1385- end
1393+ HeaderDependency . import header_deps_attrs , validate : false , batch_size : 500
13861394 end
13871395
13881396 # process witnesses
@@ -1403,11 +1411,7 @@ def build_ckb_transactions!(node_block, local_block, inputs, outputs, outputs_da
14031411 end
14041412
14051413 if witnesses_attrs . present?
1406- witnesses_attrs . each_slice ( 500 ) do |batch |
1407- Witness . upsert_all ( batch ,
1408- unique_by : %i[ ckb_transaction_id
1409- index ] )
1410- end
1414+ Witness . import witnesses_attrs , validate : false , batch_size : 500
14111415 end
14121416
14131417 txs
0 commit comments