class Dbsketch::Comparison::DatabaseComparator

Public Class Methods

new(options: {}) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 32
def initialize options: {}
        @index_comparator = IndexComparator.new
        @function_comparator = FunctionComparator.new
        @procedure_comparator = ProcedureComparator.new
        @table_comparator = TableComparator.new :options => options
        @trigger_comparator = TriggerComparator.new
        @view_comparator = ViewComparator.new
end

Public Instance Methods

are_equivalent?(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 41
def are_equivalent? old_database, new_database
        ### Preconditions
        raise ArgumentError, "old_database is not a Dbsketch::Model::Database" unless old_database.is_a? Dbsketch::Model::Database
        raise ArgumentError, "new_database is not a Dbsketch::Model::Database" unless new_database.is_a? Dbsketch::Model::Database
        ###
        indexes(old_database, new_database).empty? and operations(old_database, new_database).empty? and tables(old_database, new_database).empty? and triggers(old_database, new_database).empty? and views(old_database, new_database).empty?
end
compare(old_database, new_database) click to toggle source

Returns a DatabaseDiff if tables are different, nil otherwise

# File lib/dbsketch/comparison/database_comparator.rb, line 50
def compare old_database, new_database
        ### Preconditions
        raise ArgumentError, "old_database is not a Dbsketch::Model::Database" unless old_database.is_a? Dbsketch::Model::Database
        raise ArgumentError, "new_database is not a Dbsketch::Model::Database" unless new_database.is_a? Dbsketch::Model::Database
        ###
        DatabaseDiff.new(
                old_database, new_database, indexes(old_database, new_database), operations(old_database, new_database), tables(old_database, new_database), triggers(old_database, new_database), views(old_database, new_database)
        ) if not are_equivalent? old_database, new_database
end

Private Instance Methods

find_diffs(old_database, new_database, comparator, collection) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 62
def find_diffs old_database, new_database, comparator, collection
        ### Preconditions
        raise ArgumentError, "collection is not a Symbol" unless collection.is_a? Symbol
        raise ArgumentError, "collection is not a method of Dbsketch::Model::Database" unless old_database.respond_to? collection
        ###
        diffs = old_database.method(collection).call.select { |old_item| not new_database.has_item? old_item.name }.map do |old_item|
                comparator.compare(old_item, nil)
        end
        diffs << new_database.method(collection).call.select { |new_item| not old_database.has_item? new_item.name }.map do |new_item|
                comparator.compare(nil, new_item)
        end
        diffs << old_database.method(collection).call.select { |old_item| new_database.has_item? old_item.name }.map do |old_item|
                comparator.compare(old_item, new_database[old_item.name])
        end
        diffs.flatten.compact
end
indexes(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 79
def indexes old_database, new_database
        find_diffs old_database, new_database, @index_comparator, :indexes
end
operations(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 83
def operations old_database, new_database
        diffs = old_database.operations.select { |old_op| not new_database.has_item? old_op.name }.map do |old_op|
                (old_op.is_a? Dbsketch::Model::Function) ? @function_comparator.compare(old_op, nil) : @procedure_comparator.compare(old_op, nil)
        end
        diffs << new_database.operations.select { |new_op| not old_database.has_item? new_op.name }.map do |new_op|
                (new_op.is_a? Dbsketch::Model::Function) ? @function_comparator.compare(nil, new_op) : @procedure_comparator.compare(nil, new_op)
        end
        diffs << old_database.operations.select { |old_op| new_database.has_item? old_op.name }.map do |old_op|
                if old_op.class == new_database[old_op.name].class
                        (old_op.is_a? Dbsketch::Model::Function) ? @function_comparator.compare(old_op, new_database[old_op.name]) : @procedure_comparator.compare(old_op, new_database[old_op.name])
                else
                        Diff.new old_op, new_database[old_op.name]
                end
        end
        diffs.flatten.compact
end
tables(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 100
def tables old_database, new_database
        find_diffs old_database, new_database, @table_comparator, :tables
end
triggers(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 104
def triggers old_database, new_database
        find_diffs old_database, new_database, @trigger_comparator, :triggers
end
views(old_database, new_database) click to toggle source
# File lib/dbsketch/comparison/database_comparator.rb, line 108
def views old_database, new_database
        find_diffs old_database, new_database, @view_comparator, :views
end