Kouhei Sutou
null+****@clear*****
Tue Aug 9 12:18:07 JST 2016
Kouhei Sutou 2016-08-09 12:18:07 +0900 (Tue, 09 Aug 2016) New Revision: 6320652bb9ad748b0a1655c241b68a0c0545711c https://github.com/groonga/groonga-schema/commit/6320652bb9ad748b0a1655c241b68a0c0545711c Message: Support outputting added tables Modified files: lib/groonga-schema/diff.rb lib/groonga-schema/table.rb test/test-diff.rb Modified: lib/groonga-schema/diff.rb (+13 -0) =================================================================== --- lib/groonga-schema/diff.rb 2016-08-09 11:58:50 +0900 (cb66c07) +++ lib/groonga-schema/diff.rb 2016-08-09 12:18:07 +0900 (55f98ff) @@ -79,6 +79,7 @@ module GroongaSchema @grouped_list.clear convert_added_plugins + convert_added_tables convert_removed_plugins meaningful_grouped_list = @grouped_list.reject do |group| @@ -100,6 +101,18 @@ module GroongaSchema @grouped_list << sorted_plugins.collect(&:to_register_groonga_command) end + def convert_added_tables + sorted_tables =****@diff*****_tables.sort_by do |name, table| + [ + table.reference_key_type? ? 1 : 0, + table.name, + ] + end + @grouped_list << sorted_tables.collect do |name, table| + table.to_create_groonga_command + end + end + def convert_removed_plugins sorted_plugins =****@diff*****_plugins.sort_by(&:name) @grouped_list << sorted_plugins.collect(&:to_unregister_groonga_command) Modified: lib/groonga-schema/table.rb (+32 -0) =================================================================== --- lib/groonga-schema/table.rb 2016-08-09 11:58:50 +0900 (f6fe2ab) +++ lib/groonga-schema/table.rb 2016-08-09 12:18:07 +0900 (0500943) @@ -59,6 +59,38 @@ module GroongaSchema @token_filters == other.token_filters end + def to_create_groonga_command + flags_value = [type_flag, *flags].join("|") + token_filters_value = @token_filters.join("|") + token_filters_value = nil if token_filters_value.empty? + arguments = { + "name" => @name, + "flags" => flags_value, + "key_type" => @key_type, + "value_type" => @value_type, + "default_tokenizer" => @default_tokenizer, + "normalizer" => @normalizer, + "token_filters" => token_filters_value, + } + Groonga::Command::TableCreate.new(arguments) + end + + private + def type_flag + case @type + when :no_key + "TABLE_NO_KEY" + when :hash_key + "TABLE_HASH_KEY" + when :pat_key + "TABLE_PAT_KEY" + when :dat_key + "TABLE_DAT_KEY" + else + "TABLE_HASH_KEY" + end + end + class CommandApplier def initialize(table, command) @table = table Modified: test/test-diff.rb (+49 -0) =================================================================== --- test/test-diff.rb 2016-08-09 11:58:50 +0900 (2e4f2f2) +++ test/test-diff.rb 2016-08-09 12:18:07 +0900 (2cf3e7c) @@ -23,6 +23,14 @@ class DiffTest < Test::Unit::TestCase GroongaSchema::Plugin.new(name) end + def table(name, options) + table = GroongaSchema::Table.new(name) + options.each do |key, value| + table.__send__("#{key}=", value) + end + table + end + sub_test_case "#same?" do test "same" do assert do @@ -48,5 +56,46 @@ plugin_register --name "token_filters/stem" plugin_unregister --name "token_filters/stop_word" LIST end + + test "added table" do + 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_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) + + 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" + LIST + end end end -------------- next part -------------- HTML����������������������������... 다운로드