Sha256: df806980f5e79eea74e6e2c15d8554b25aec2cd51953a44b0589b2bfd68cfcaa

Contents?: true

Size: 1.55 KB

Versions: 6

Compression:

Stored size: 1.55 KB

Contents

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
inst_data_shipper-0.2.6 lib/inst_data_shipper/data_sources/local_tables.rb
inst_data_shipper-0.2.5 lib/inst_data_shipper/data_sources/local_tables.rb
inst_data_shipper-0.2.4 lib/inst_data_shipper/data_sources/local_tables.rb
inst_data_shipper-0.2.3 lib/inst_data_shipper/data_sources/local_tables.rb
inst_data_shipper-0.2.2 lib/inst_data_shipper/data_sources/local_tables.rb
inst_data_shipper-0.2.1 lib/inst_data_shipper/data_sources/local_tables.rb