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.
Initializes the TableSorter
session: The active Session instance
tables: an array of table names
# File lib/rubyrep/table_sorter.rb, line 65 def initialize(session, tables) self.session = session self.tables = tables end
The table dependencies. Format as described e. g. here: PostgreSQLExtender#referenced_tables
# File lib/rubyrep/table_sorter.rb, line 24 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
# File lib/rubyrep/table_sorter.rb, line 53 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
Generated with the Darkfish Rdoc Generator 2.