Sha256: ea45c784c56aa8f1599e6b8fb96d329e372fedc0d91397ec966bc1d805f455df

Contents?: true

Size: 1.3 KB

Versions: 14

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: 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(table.query).map { |row| row[TableSaw.schema_cache.primary_keys(table.name)] }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
table_saw-3.1.0 lib/table_saw/dependency_graph/build.rb
table_saw-3.0.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.10.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.9.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.8.1 lib/table_saw/dependency_graph/build.rb
table_saw-2.8.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.7.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.6.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.5.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.4.3 lib/table_saw/dependency_graph/build.rb
table_saw-2.4.2 lib/table_saw/dependency_graph/build.rb
table_saw-2.4.1 lib/table_saw/dependency_graph/build.rb
table_saw-2.4.0 lib/table_saw/dependency_graph/build.rb
table_saw-2.3.0 lib/table_saw/dependency_graph/build.rb