Navigation Menu

Skip to content

Commit

Permalink
differ: support diffing columns
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 8, 2016
1 parent cc4c87a commit 79b9f33
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
11 changes: 11 additions & 0 deletions lib/groonga-schema/column.rb
Expand Up @@ -36,6 +36,17 @@ def apply_command(command)
applier.apply
end

def ==(other)
return false unless other.is_a?(self.class)

@table_name == other.table_name and
@name == other.name and
@type == other.type and
@flags.sort == other.flags.sort and
@value_type == other.value_type and
@sources == other.sources
end

class CommandApplier
def initialize(column, command)
@column = column
Expand Down
24 changes: 24 additions & 0 deletions lib/groonga-schema/differ.rb
Expand Up @@ -61,6 +61,30 @@ def diff_tables(diff)
end

def diff_columns(diff)
@from.columns.each do |table_name, from_columns|
to_columns = @to.columns[table_name] || {}
from_columns.each do |name, from_column|
to_column = to_columns[name]
if to_column.nil?
diff.removed_columns[table_name] ||= {}
diff.removed_columns[table_name][name] = from_column
elsif from_column != to_column
diff.changed_columns[table_name] ||= {}
diff.changed_columns[table_name][name] = to_column
end
end
end

@to.columns.each do |table_name, to_columns|
from_columns = @from.columns[table_name] || {}
to_columns.each do |name, to_column|
from_column = from_columns[name]
if from_column.nil?
diff.added_columns[table_name] ||= {}
diff.added_columns[table_name][name] = to_column
end
end
end
end
end
end
53 changes: 53 additions & 0 deletions test/test-differ.rb
Expand Up @@ -79,5 +79,58 @@ def column_create(arguments)
expected.changed_tables["Words"] = @to.tables["Words"]
assert_equal(expected, @differ.diff)
end

test "column - add" do
arguments = {
"table" => "Words",
"name" => "entries_text",
"flags" => "COLUMN_INDEX|WITH_POSITION|WITH_SECTION|INDEX_TINY",
"type" => "Entries",
"source" => "title, content",
}
@to.apply_command(column_create(arguments))

expected = GroongaSchema::Diff.new
expected.added_columns["Words"] = {
"entries_text" => @to.columns["Words"]["entries_text"],
}
assert_equal(expected, @differ.diff)
end

test "column - remove" do
arguments = {
"table" => "Words",
"name" => "entries_text",
"flags" => "COLUMN_INDEX|WITH_POSITION|WITH_SECTION|INDEX_TINY",
"type" => "Entries",
"source" => "title, content",
}
@from.apply_command(column_create(arguments))

expected = GroongaSchema::Diff.new
expected.removed_columns["Words"] = {
"entries_text" => @from.columns["Words"]["entries_text"],
}
assert_equal(expected, @differ.diff)
end

test "column - change" do
from_arguments = {
"table" => "Words",
"name" => "entries_text",
"flags" => "COLUMN_INDEX|WITH_POSITION|WITH_SECTION|INDEX_TINY",
"type" => "Entries",
"source" => "title, content",
}
to_arguments = from_arguments.merge("source" => "title")
@from.apply_command(column_create(from_arguments))
@to.apply_command(column_create(to_arguments))

expected = GroongaSchema::Diff.new
expected.changed_columns["Words"] = {
"entries_text" => @to.columns["Words"]["entries_text"],
}
assert_equal(expected, @differ.diff)
end
end
end

0 comments on commit 79b9f33

Please sign in to comment.