Sha256: 8d4cf250e46d39e36a292dfd8ae0cd0c98941841b2bb6d5f5d1bc944e01f65fc

Contents?: true

Size: 1.87 KB

Versions: 17

Compression:

Stored size: 1.87 KB

Contents

$LOAD_PATH.unshift File.dirname(__FILE__) + '/..'

require 'tsort'
require 'rubyrep'

module RR
  # This class sorts a given list of tables so that tables referencing other
  # tables via foreign keys are placed behind those referenced tables.
  #
  # Rationale:
  # If tables are sorted in that sequence, the risk of foreign key violations is
  # smaller.
  class TableSorter
    include TSort

    # The active +Session+
    attr_accessor :session

    # The list of table names to be ordered
    attr_accessor :tables

    # The table dependencies.
    # Format as described e. g. here: PostgreSQLExtender#referenced_tables
    def referenced_tables
      unless @referenced_tables
        @referenced_tables = session.left.referenced_tables(tables)

        # Strip away all unrelated tables
        @referenced_tables.each_pair do |table, references|
          references.delete_if do |reference|
            not tables.include? reference
          end
        end
      end
      @referenced_tables
    end

    # Yields each table.
    # For details see standard library: TSort#sort_each_node.
    def tsort_each_node
      referenced_tables.each_key do |table|
        yield table
      end
    end

    # Yields all tables that are references by +table+.
    def tsort_each_child(table)
      referenced_tables[table].each do |reference|
        yield reference
      end
    end

    def sort
      # Note:
      # We should not use TSort#tsort as this one throws an exception if
      # there are cyclic redundancies.
      # (Our goal is to just get the best ordering that is possible and then
      # take our chances.)
      strongly_connected_components.flatten
    end

    # Initializes the TableSorter
    # * session: The active +Session+ instance
    # * tables: an array of table names
    def initialize(session, tables)
      self.session = session
      self.tables = tables
    end
  end
end

Version data entries

17 entries across 17 versions & 2 rubygems

Version Path
rubyrep-2.0.1 lib/rubyrep/table_sorter.rb
rubyrep-2.0.0 lib/rubyrep/table_sorter.rb
andyjeffries-rubyrep-1.2.1 lib/rubyrep/table_sorter.rb
rubyrep-1.2.0 lib/rubyrep/table_sorter.rb
rubyrep-1.1.2 lib/rubyrep/table_sorter.rb
rubyrep-1.1.1 lib/rubyrep/table_sorter.rb
rubyrep-1.1.0 lib/rubyrep/table_sorter.rb
rubyrep-1.0.9 lib/rubyrep/table_sorter.rb
rubyrep-1.0.8 lib/rubyrep/table_sorter.rb
rubyrep-1.0.3 lib/rubyrep/table_sorter.rb
rubyrep-1.0.4 lib/rubyrep/table_sorter.rb
rubyrep-1.0.5 lib/rubyrep/table_sorter.rb
rubyrep-1.0.6 lib/rubyrep/table_sorter.rb
rubyrep-1.0.7 lib/rubyrep/table_sorter.rb
rubyrep-1.0.2 lib/rubyrep/table_sorter.rb
rubyrep-1.0.0 lib/rubyrep/table_sorter.rb
rubyrep-1.0.1 lib/rubyrep/table_sorter.rb