Sha256: 9332bb2259587f34fd2887311d6a89a2a58f3074ff23da2c035044b3621835b3

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module TableSaw
  module DependencyGraph
    class Build
      attr_reader :manifest, :records

      def initialize(manifest)
        @manifest = manifest
        @records = {}
      end

      def call
        manifest.tables.values.sort_by { |t| t.partial? ? 1 : 0 }.each do |table|
          add TableSaw::DependencyGraph::AddDirective.new(table.name, ids: select_ids(table), partial: table.partial?,
                                                                      has_many: table.has_many)
        end

        records
      end

      private

      def add(directive)
        return [] unless directive.queryable?

        directives(directive).select(&:queryable?).each(&method(:add))
      end

      def directives(dir)
        record = records[dir.table_name]

        if record
          dir.partial? ? record.fetch_associations(dir) : []
        else
          TableSaw::DependencyGraph::DumpTable.new(manifest:, name: dir.table_name, partial: dir.partial?)
            .tap { |table| records[dir.table_name] = table }.fetch_associations(dir)
        end
      end

      def select_ids(table)
        return [] unless table.partial?

        TableSaw.connection.exec_query(table.query).map { |row| row[TableSaw.schema_cache.primary_keys(table.name)] }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
table_saw-3.2.0 lib/table_saw/dependency_graph/build.rb