class Dbsketch::Comparison::TableComparator

Public Class Methods

new(column_comparator: nil, options: {}) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 38
def initialize column_comparator: nil, options: {}
        ### Preconditions
        raise ArgumentError, "column_comparator is not a Dbsketch::Comparison::ColumnComparator" unless nil == column_comparator or column_comparator.is_a? Dbsketch::Comparison::ColumnComparator
        ###
        @check_constraint_comparator = CheckConstraintComparator.new
        @column_comparator = (nil == column_comparator ? ColumnComparator.new(:options => options) : column_comparator)
        @computed_column_comparator = ComputedColumnComparator.new
        @foreign_key_comparator = ForeignKeyComparator.new
        @primary_key_comparator = PrimaryKeyComparator.new
        @unique_constraint_comparator = UniqueConstraintComparator.new
end

Public Instance Methods

are_equivalent?(old_table, new_table) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 50
def are_equivalent? old_table, new_table
        ### Preconditions
        raise ArgumentError, "old_table is not a Dbsketch::Model::Table" unless nil == old_table or old_table.is_a? Dbsketch::Model::Table
        raise ArgumentError, "new_table is not a Dbsketch::Model::Table" unless nil == new_table or new_table.is_a? Dbsketch::Model::Table
        ###
        (nil != old_table and nil != new_table) and
                columns(old_table, new_table).empty? and
                @primary_key_comparator.are_equivalent?(old_table.primary_key, new_table.primary_key) and
                check_constraints(old_table, new_table).empty? and
                foreign_keys(old_table, new_table).empty? and
                unique_constraints(old_table, new_table).empty?
end
compare(old_table, new_table) click to toggle source

Returns a TableDiff if tables are different, nil otherwise

# File lib/dbsketch/comparison/table_comparator.rb, line 64
def compare old_table, new_table
        ### Preconditions
        raise ArgumentError, "old_table is not a Dbsketch::Model::Table" unless nil == old_table or old_table.is_a? Dbsketch::Model::Table
        raise ArgumentError, "new_table is not a Dbsketch::Model::Table" unless nil == new_table or new_table.is_a? Dbsketch::Model::Table
        ###
        if not are_equivalent? old_table, new_table
                primary_key_diff = @primary_key_comparator.compare(old_table.primary_key, new_table.primary_key) if nil != old_table and nil != new_table
                TableDiff.new(
                        old_table, new_table,
                        columns(old_table, new_table),
                        primary_key_diff,
                        check_constraints(old_table, new_table),
                        foreign_keys(old_table, new_table),
                        unique_constraints(old_table, new_table)
                )
        end
end

Private Instance Methods

check_constraints(old_table, new_table) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 104
def check_constraints old_table, new_table
        diffs = []
        if nil != old_table and nil != new_table
                diffs = old_table.check_constraints.map do |old_c|
                        if new_table.has_check_constraint? old_c.name
                                @check_constraint_comparator.compare(old_c, new_table.check_constraint(old_c.name))
                        else
                                @check_constraint_comparator.compare(old_c, nil)
                        end
                end
                diffs << new_table.check_constraints.select { |new_c| not old_table.has_check_constraint? new_c.name }.map do |new_c|
                        @check_constraint_comparator.compare(nil, new_c)
                end
        end
        diffs.flatten.compact
end
columns(old_table, new_table) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 84
def columns old_table, new_table
        diffs = []
        if nil != old_table and nil != new_table
                diffs = old_table.columns.select { |old_c| not new_table.has_column? old_c.name }.map do |old_c|
                        (old_c.is_a? Dbsketch::Model::Column) ? @column_comparator.compare(old_c, nil) : @computed_column_comparator.compare(old_c, nil)
                end
                diffs << new_table.columns.select { |new_c| not old_table.has_column? new_c.name }.map do |new_c|
                        (new_c.is_a? Dbsketch::Model::Column) ? @column_comparator.compare(nil, new_c) : @computed_column_comparator.compare(nil, new_c)
                end
                diffs << old_table.columns.select { |old_c| new_table.has_column? old_c.name }.map do |old_c|
                        if old_c.class == new_table[old_c.name].class
                                (old_c.is_a? Dbsketch::Model::Column) ? @column_comparator.compare(old_c, new_table[old_c.name]) : @computed_column_comparator.compare(old_c, new_table[old_c.name])
                        else
                                Diff.new old_c, new_table[old_c.name]
                        end
                end
        end
        diffs.flatten.compact
end
foreign_keys(old_table, new_table) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 121
def foreign_keys old_table, new_table
        diffs = []
        if nil != old_table and nil != new_table
                diffs = old_table.foreign_keys.map do |old_k|
                        if new_table.has_foreign_key? old_k.name
                                @foreign_key_comparator.compare(old_k, new_table.foreign_key(old_k.name))
                        else
                                @foreign_key_comparator.compare(old_k, nil)
                        end
                end
                diffs << new_table.foreign_keys.select { |new_k| not old_table.has_foreign_key? new_k.name }.map do |new_k|
                        @foreign_key_comparator.compare(nil, new_k)
                end
        end
        diffs.flatten.compact
end
unique_constraints(old_table, new_table) click to toggle source
# File lib/dbsketch/comparison/table_comparator.rb, line 138
def unique_constraints old_table, new_table
        diffs = []
        if nil != old_table and nil != new_table
                diffs = old_table.unique_constraints.map do |old_c|
                        if new_table.has_unique_constraint? old_c.name
                                @unique_constraint_comparator.compare(old_c, new_table.unique_constraint(old_c.name))
                        else
                                @unique_constraint_comparator.compare(old_c, nil)
                        end
                end
                diffs << new_table.unique_constraints.select { |new_c| not old_table.has_unique_constraint? new_c.name }.map do |new_c|
                        @unique_constraint_comparator.compare(nil, new_c)
                end
        end
        diffs.flatten.compact
end