Navigation Menu

Skip to content

Commit

Permalink
Support outputting added columns
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Aug 9, 2016
1 parent 6320652 commit ff5fc85
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
28 changes: 28 additions & 0 deletions lib/groonga-schema/column.rb
Expand Up @@ -53,6 +53,34 @@ def ==(other)
@sources == other.sources
end

def to_create_groonga_command
flags_value = [type_flag, *flags].join("|")
sources_value = @sources.join(",")
sources_value = nil if sources_value.empty?
arguments = {
"table" => @table_name,
"name" => @name,
"flags" => flags_value,
"type" => @value_type,
"source" => sources_value,
}
Groonga::Command::ColumnCreate.new(arguments)
end

private
def type_flag
case @type
when :scalar
"COLUMN_SCALAR"
when :vector
"COLUMN_VECTOR"
when :index
"COLUMN_INDEX"
else
"COLUMN_SCALAR"
end
end

class CommandApplier
def initialize(column, command)
@column = column
Expand Down
32 changes: 30 additions & 2 deletions lib/groonga-schema/diff.rb
Expand Up @@ -108,9 +108,37 @@ def convert_added_tables
table.name,
]
end
@grouped_list << sorted_tables.collect do |name, table|
table.to_create_groonga_command

sorted_tables.each do |name, table|
group = []
group << table.to_create_groonga_command
group.concat(convert_added_columns(name, false))
@grouped_list << group
end

sorted_tables.each do |name, table|
@grouped_list << convert_added_columns(name, true)
end
end

def convert_added_columns(name, target_is_reference_type)
columns = @diff.added_columns[name]
return [] if columns.nil?

sorted_columns = columns.sort_by do |column_name,|
column_name
end

group = []
sorted_columns.each do |column_name, column|
if target_is_reference_type
next unless column.reference_value_type?
else
next if column.reference_value_type?
end
group << column.to_create_groonga_command
end
group
end

def convert_removed_plugins
Expand Down
79 changes: 78 additions & 1 deletion test/test-diff.rb
Expand Up @@ -31,6 +31,14 @@ def table(name, options)
table
end

def column(table_name, name, options)
column = GroongaSchema::Column.new(table_name, name)
options.each do |key, value|
column.__send__("#{key}=", value)
end
column
end

sub_test_case "#same?" do
test "same" do
assert do
Expand All @@ -57,7 +65,7 @@ def table(name, options)
LIST
end

test "added table" do
test "added tables - without column" do
token_filters = [
"TokenFilterStopWord",
"TokenFilterStem",
Expand All @@ -84,18 +92,87 @@ def table(name, options)
--key_type "ShortText" \\
--name "Names" \\
--normalizer "NormalizerAuto"
table_create \\
--default_tokenizer "TokenBigram" \\
--flags "TABLE_PAT_KEY" \\
--key_type "ShortText" \\
--name "Words" \\
--normalizer "NormalizerAuto" \\
--token_filters "TokenFilterStopWord|TokenFilterStem"
table_create \\
--flags "TABLE_HASH_KEY" \\
--key_type "Names" \\
--name "Commands"
LIST
end

test "added tables - with column" do
@diff.added_tables["Names"] = table("Names",
:type => :hash_key,
:flags => "KEY_LARGE",
:key_type => "ShortText",
:normalizer => "NormalizerAuto")
@diff.added_tables["Commands"] = table("Commands",
:type => :hash_key,
:key_type => "Names",
:reference_key_type => true)
@diff.added_columns["Commands"] = {
"description" => column("Commands", "description",
:value_type => "Text"),
}
token_filters = [
"TokenFilterStopWord",
"TokenFilterStem",
]
@diff.added_tables["Words"] = table("Words",
:type => :pat_key,
:key_type => "ShortText",
:default_tokenizer => "TokenBigram",
:normalizer => "NormalizerAuto",
:token_filters => token_filters)
@diff.added_columns["Words"] = {
"commands_description" => column("Words", "commands_description",
:type => :index,
:flags => ["WITH_POSITION"],
:value_type => "Commands",
:sources => ["description"],
:reference_value_type => true),
}

assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
table_create \\
--flags "TABLE_HASH_KEY|KEY_LARGE" \\
--key_type "ShortText" \\
--name "Names" \\
--normalizer "NormalizerAuto"
table_create \\
--default_tokenizer "TokenBigram" \\
--flags "TABLE_PAT_KEY" \\
--key_type "ShortText" \\
--name "Words" \\
--normalizer "NormalizerAuto" \\
--token_filters "TokenFilterStopWord|TokenFilterStem"
table_create \\
--flags "TABLE_HASH_KEY" \\
--key_type "Names" \\
--name "Commands"
column_create \\
--flags "COLUMN_SCALAR" \\
--name "description" \\
--table "Commands" \\
--type "Text"
column_create \\
--flags "COLUMN_INDEX|WITH_POSITION" \\
--name "commands_description" \\
--source "description" \\
--table "Words" \\
--type "Commands"
LIST
end
end
end

0 comments on commit ff5fc85

Please sign in to comment.