Kouhei Sutou
null+****@clear*****
Fri Mar 3 10:22:45 JST 2017
Kouhei Sutou 2017-03-03 10:22:45 +0900 (Fri, 03 Mar 2017) New Revision: d2cdb908237fe48f3ad3bb8c2285380d2678e68e https://github.com/ranguba/groonga-client-model/commit/d2cdb908237fe48f3ad3bb8c2285380d2678e68e Message: Support index creation Modified files: lib/groonga_client_model/migration.rb lib/groonga_client_model/schema.rb test/unit/test_migration.rb Modified: lib/groonga_client_model/migration.rb (+32 -9) =================================================================== --- lib/groonga_client_model/migration.rb 2017-03-03 09:39:53 +0900 (ea0f5b6) +++ lib/groonga_client_model/migration.rb 2017-03-03 10:22:45 +0900 (42e62ba) @@ -94,25 +94,38 @@ module GroongaClientModel remove_table_raw(name) end - def add_column(table_name, column_name, value_type, options={}) + def add_column(table_name, column_name, value_type, + flags: nil, + type: nil, + sources: nil, + source: nil) return remove_column_raw(name) if @reverting value_type = normalize_type(value_type) - flags = [] - flags << normalize_column_type(options[:type] || :scalar) - arguments = [ - table_name, - column_name, + type = normalize_column_type(type || :scalar) + flags = Array(flags) | [type] + if type == "COLUMN_INDEX" + schema = GroongaClientModel::Schema.new + case schema.tables[table_name].tokenizer + when nil, "TokenDelimit" + # do nothing + else + flags << "WITH_POSITION" + end + end + sources ||= source + options = { flags: flags, value_type: value_type, - ] - report(__method__, arguments) do + } + options[:sources] = sources unless sources.blank? + report(__method__, [table_name, column_name, options]) do @client.request(:column_create). parameter(:table, table_name). parameter(:name, column_name). flags_parameter(:flags, flags). parameter(:type, value_type). - values_parameter(:source, options[:source]). + values_parameter(:source, sources). response end end @@ -261,6 +274,16 @@ module GroongaClientModel def long_text(column_name, options={}) @migration.add_column(@table_name, column_name, :long_text, options) end + + def index(source_table_name, source_column_names, options={}) + source_column_names = Array(source_column_names) + column_name = [source_table_name, *source_column_names].join("_") + @migration.add_column(@table_name, + column_name, + source_table_name, + options.merge(:type => :index, + :sources => source_column_names)) + end end end end Modified: lib/groonga_client_model/schema.rb (+8 -0) =================================================================== --- lib/groonga_client_model/schema.rb 2017-03-03 09:39:53 +0900 (0c633fe) +++ lib/groonga_client_model/schema.rb 2017-03-03 10:22:45 +0900 (989670f) @@ -70,6 +70,14 @@ module GroongaClientModel Columns.new(@raw_schema, @raw_table.columns.merge(raw_columns)) end + def tokenizer + @raw_table.tokenizer + end + + def normalizer + @raw_table.normalizer + end + private def create_pseudo_column(name, value_type) raw_column = { Modified: test/unit/test_migration.rb (+45 -0) =================================================================== --- test/unit/test_migration.rb 2017-03-03 09:39:53 +0900 (481691e) +++ test/unit/test_migration.rb 2017-03-03 10:22:45 +0900 (cbef845) @@ -240,6 +240,51 @@ column_create posts content COLUMN_SCALAR LongText end end end + + sub_test_case("#index") do + test("for full text search") do + expected_up_report = <<-REPORT +-- create_table(:posts, {:type=>"TABLE_NO_KEY"}) + -> 0.0s +-- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"Text"}) + -> 0.0s +-- create_table(:terms, {:type=>"TABLE_PAT_KEY", :key_type=>"ShortText", :tokenizer=>"TokenBigram", :normalizer=>"NormalizerAuto"}) + -> 0.0s +-- add_column(:terms, "posts_content", {:flags=>["COLUMN_INDEX", "WITH_POSITION"], :value_type=>:posts, :sources=>[:content]}) + -> 0.0s + REPORT + expected_down_report = <<-REPORT +-- remove_table(:posts) + -> 0.0s +-- remove_table(:terms) + -> 0.0s + REPORT + expected_dump = <<-DUMP.chomp +table_create posts TABLE_NO_KEY +column_create posts content COLUMN_SCALAR Text + +table_create terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto + +column_create terms posts_content COLUMN_INDEX|WITH_POSITION posts content + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) do |table| + table.text(:content) + end + + create_table(:terms, + :type => :patricia_trie, + :tokenizer => :bigram, + :normalizer => :auto) do |table| + table.index(:posts, :content) + end + end + end + end + end end end end -------------- next part -------------- HTML����������������������������... Download