Skip to content

Commit

Permalink
Support index creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kou committed Mar 3, 2017
1 parent ed0e501 commit d2cdb90
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
41 changes: 32 additions & 9 deletions lib/groonga_client_model/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,38 @@ def remove_table(name)
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
Expand Down Expand Up @@ -261,6 +274,16 @@ def text(column_name, options={})
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
8 changes: 8 additions & 0 deletions lib/groonga_client_model/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ def columns
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 = {
Expand Down
45 changes: 45 additions & 0 deletions test/unit/test_migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,51 @@ def assert_migrate(expected_up_report,
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

0 comments on commit d2cdb90

Please sign in to comment.