Kouhei Sutou
null+****@clear*****
Tue Aug 9 18:39:44 JST 2016
Kouhei Sutou 2016-08-09 18:39:44 +0900 (Tue, 09 Aug 2016) New Revision: ff5fc854997b64ef7dd6ee4640c341be4748675c https://github.com/groonga/groonga-schema/commit/ff5fc854997b64ef7dd6ee4640c341be4748675c Message: Support outputting added columns Modified files: lib/groonga-schema/column.rb lib/groonga-schema/diff.rb test/test-diff.rb Modified: lib/groonga-schema/column.rb (+28 -0) =================================================================== --- lib/groonga-schema/column.rb 2016-08-09 12:18:07 +0900 (8ccff13) +++ lib/groonga-schema/column.rb 2016-08-09 18:39:44 +0900 (f11fba8) @@ -53,6 +53,34 @@ module GroongaSchema @sources == other.sources end + def to_create_groonga_command + flags_value = [type_flag, *flags].join("|") + sources_value =****@sourc*****(",") + 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 Modified: lib/groonga-schema/diff.rb (+30 -2) =================================================================== --- lib/groonga-schema/diff.rb 2016-08-09 12:18:07 +0900 (55f98ff) +++ lib/groonga-schema/diff.rb 2016-08-09 18:39:44 +0900 (2a09540) @@ -108,9 +108,37 @@ module GroongaSchema 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*****_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 Modified: test/test-diff.rb (+78 -1) =================================================================== --- test/test-diff.rb 2016-08-09 12:18:07 +0900 (2cf3e7c) +++ test/test-diff.rb 2016-08-09 18:39:44 +0900 (2be9000) @@ -31,6 +31,14 @@ class DiffTest < Test::Unit::TestCase 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 @@ -57,7 +65,7 @@ plugin_unregister --name "token_filters/stop_word" LIST end - test "added table" do + test "added tables - without column" do token_filters = [ "TokenFilterStopWord", "TokenFilterStem", @@ -84,6 +92,7 @@ table_create \\ --key_type "ShortText" \\ --name "Names" \\ --normalizer "NormalizerAuto" + table_create \\ --default_tokenizer "TokenBigram" \\ --flags "TABLE_PAT_KEY" \\ @@ -91,11 +100,79 @@ table_create \\ --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 -------------- next part -------------- HTML����������������������������...Download