Class: StagingTable::Session
- Inherits:
-
Object
- Object
- StagingTable::Session
- Defined in:
- lib/staging_table/session.rb
Constant Summary collapse
- CALLBACK_OPTIONS =
Supported callback options:
- before_insert: ->(session) { ... } - after_insert: ->(session, records) { ... } - before_transfer: ->(session) { ... } - after_transfer: ->(session, result) { ... } %i[before_insert after_insert before_transfer after_transfer].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#source_model ⇒ Object
readonly
Returns the value of attribute source_model.
-
#staging_model ⇒ Object
readonly
Returns the value of attribute staging_model.
Instance Method Summary collapse
- #create_table ⇒ Object
- #drop_table ⇒ Object
-
#initialize(source_model, **options) ⇒ Session
constructor
A new instance of Session.
- #insert(records) ⇒ Object
- #insert_from_query(relation) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Delegate unknown methods to the staging model (e.g. for querying).
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
- #transfer ⇒ Object
Constructor Details
#initialize(source_model, **options) ⇒ Session
Returns a new instance of Session.
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/staging_table/session.rb', line 16 def initialize(source_model, **) @source_model = source_model config = StagingTable.configuration @callbacks = .slice(*CALLBACK_OPTIONS) @options = { batch_size: config.default_batch_size, transfer_strategy: config.default_transfer_strategy }.merge(.except(*CALLBACK_OPTIONS)) @table_created = false end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegate unknown methods to the staging model (e.g. for querying)
141 142 143 144 145 146 147 |
# File 'lib/staging_table/session.rb', line 141 def method_missing(method, *args, &block) if staging_model.respond_to?(method) staging_model.send(method, *args, &block) else super end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/staging_table/session.rb', line 7 def @options end |
#source_model ⇒ Object (readonly)
Returns the value of attribute source_model.
7 8 9 |
# File 'lib/staging_table/session.rb', line 7 def source_model @source_model end |
#staging_model ⇒ Object (readonly)
Returns the value of attribute staging_model.
7 8 9 |
# File 'lib/staging_table/session.rb', line 7 def staging_model @staging_model end |
Instance Method Details
#create_table ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/staging_table/session.rb', line 27 def create_table return if @table_created payload = { source_model: source_model, source_table: source_model.table_name, staging_table: staging_table_name } Instrumentation.instrument(:create_table, payload) do adapter.create_table(staging_table_name, source_model.table_name, ) @staging_model = ModelFactory.build(source_model, staging_table_name, excluded_columns: [:excluded_columns] || []) @table_created = true end end |
#drop_table ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/staging_table/session.rb', line 43 def drop_table return unless @table_created payload = { source_model: source_model, source_table: source_model.table_name, staging_table: staging_table_name } Instrumentation.instrument(:drop_table, payload) do adapter.drop_table(staging_table_name) @table_created = false @staging_model = nil end end |
#insert(records) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/staging_table/session.rb', line 59 def insert(records) ensure_table_created! run_callback(:before_insert, self) normalized_records = normalize_records(records) payload = { source_model: source_model, source_table: source_model.table_name, staging_table: staging_table_name, record_count: normalized_records.size, batch_size: [:batch_size] || 1000 } Instrumentation.instrument(:insert, payload) do BulkInserter.new(staging_model, batch_size: [:batch_size] || 1000).insert(normalized_records) end run_callback(:after_insert, self, normalized_records) end |
#insert_from_query(relation) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/staging_table/session.rb', line 81 def insert_from_query(relation) ensure_table_created! run_callback(:before_insert, self) # TODO: Implement direct INSERT INTO SELECT for query-based insertion # For now, we'll iterate, but this should be optimized all_records = [] payload = { source_model: source_model, source_table: source_model.table_name, staging_table: staging_table_name, batch_size: [:batch_size] || 1000 } Instrumentation.instrument(:insert, payload) do |instrumentation_payload| relation.find_in_batches(batch_size: [:batch_size] || 1000) do |batch| records = batch.map(&:attributes) all_records.concat(records) BulkInserter.new(staging_model, batch_size: [:batch_size] || 1000).insert(records) end instrumentation_payload[:record_count] = all_records.size end run_callback(:after_insert, self, all_records) end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
149 150 151 |
# File 'lib/staging_table/session.rb', line 149 def respond_to_missing?(method, include_private = false) staging_model.respond_to?(method, include_private) || super end |
#transfer ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/staging_table/session.rb', line 109 def transfer ensure_table_created! run_callback(:before_transfer, self) strategy_name = [:transfer_strategy].to_s.camelize begin strategy_class = TransferStrategies.const_get(strategy_name) rescue NameError raise ConfigurationError, "Invalid transfer strategy: #{[:transfer_strategy]}. Available strategies: insert, upsert." end payload = { source_model: source_model, source_table: source_model.table_name, staging_table: staging_table_name, strategy: [:transfer_strategy], staged_count: staging_model.count } result = Instrumentation.instrument(:transfer, payload) do |instrumentation_payload| transfer_result = strategy_class.new(source_model, staging_model, ).transfer instrumentation_payload[:result] = transfer_result transfer_result end run_callback(:after_transfer, self, result) result end |