module InstDataShipper module DataSources # This module contains the logic for processing local AR tables module LocalTables include Base public delayed def import_local_table(model, schema_name: nil) model = model.safe_constantize if model.is_a?(String) table_def = lookup_table_schema!(schema_name, { model: model }) inner_block = ->(file) { query = model.all query = _resolve_model_query(query, table_def[:query]) if table_is_incremental?(table_def) query = _resolve_model_query( query, table_def.dig(:incremental, :scope), string: ->(query, column) { query.where("#{column} > ?", incremental_since) }, default: "updated_at", ) end query.find_each do |m| file << table_def[:columns].map do |c| instance_exec(m, &c[:block]) end end } upload_data(table_def, &inner_block) end private def _resolve_model_query(relation, query, string: nil, default: nil) return relation if query == false query = default if query.nil? return relation if query.nil? if query.is_a?(Symbol) relation.send(query) elsif query.is_a?(Proc) instance_exec(relation, &query) elsif query.is_a?(String) && string.present? instance_exec(relation, query, &string) else raise "Invalid query: #{query.inspect}" end end end end end