Kouhei Sutou
null+****@clear*****
Mon Aug 15 18:19:42 JST 2016
Kouhei Sutou 2016-08-15 18:19:42 +0900 (Mon, 15 Aug 2016) New Revision: fa6bee796d2c031f7e155717cff18905f2ba2a4d https://github.com/groonga/groonga-schema/commit/fa6bee796d2c031f7e155717cff18905f2ba2a4d Message: Support detecting related tables and columns Modified files: lib/groonga-schema/column.rb lib/groonga-schema/command-line/groonga-schema-diff.rb lib/groonga-schema/diff.rb lib/groonga-schema/differ.rb lib/groonga-schema/schema.rb lib/groonga-schema/table.rb test/test-diff.rb Modified: lib/groonga-schema/column.rb (+2 -0) =================================================================== --- lib/groonga-schema/column.rb 2016-08-15 17:18:09 +0900 (a9926d0) +++ lib/groonga-schema/column.rb 2016-08-15 18:19:42 +0900 (d4d02c3) @@ -23,6 +23,7 @@ module GroongaSchema attr_accessor :value_type attr_accessor :sources attr_writer :reference_value_type + attr_accessor :related_columns def initialize(table_name, name) @table_name = table_name @name = name @@ -31,6 +32,7 @@ module GroongaSchema @value_type = "ShortText" @sources = [] @reference_value_type = false + @related_columns = [] end def reference_value_type? Modified: lib/groonga-schema/command-line/groonga-schema-diff.rb (+2 -2) =================================================================== --- lib/groonga-schema/command-line/groonga-schema-diff.rb 2016-08-15 17:18:09 +0900 (31321e4) +++ lib/groonga-schema/command-line/groonga-schema-diff.rb 2016-08-15 18:19:42 +0900 (322efd6) @@ -40,8 +40,8 @@ module GroongaSchema from_schema = parse_schema(@from_path) to_schema = parse_schema(@to_path) differ = GroongaSchema::Differ.new(from_schema, to_schema) - diff = differ.diff(:format => @format) - $stdout.print(diff.to_groonga_command_list) + diff = differ.diff + $stdout.print(diff.to_groonga_command_list(:format => @format)) if diff.same? 0 Modified: lib/groonga-schema/diff.rb (+21 -8) =================================================================== --- lib/groonga-schema/diff.rb 2016-08-15 17:18:09 +0900 (2e3d4bc) +++ lib/groonga-schema/diff.rb 2016-08-15 18:19:42 +0900 (6edf999) @@ -105,21 +105,34 @@ module GroongaSchema end def convert_added_tables - sorted_tables =****@diff*****_tables.sort_by do |name, table| - [ - table.reference_key_type? ? 1 : 0, - table.name, - ] + reference_table_names = [] + no_reference_table_names = [] + @diff.added_tables.each do |name, table| + if table.reference_key_type? + reference_table_names << name + else + no_reference_table_names << name + end end + no_reference_table_names |= + (@diff.added_columns.keys - reference_table_names) - sorted_tables.each do |name, table| + sorted_reference_table_names = reference_table_names.sort + sorted_no_reference_table_names = no_reference_table_names.sort + + sorted_table_names = + sorted_no_reference_table_names + + sorted_reference_table_names + + sorted_table_names.each do |name| group = [] - group << table.to_create_groonga_command + table =****@diff*****_tables[name] + group << table.to_create_groonga_command if table group.concat(convert_added_columns(name, false)) @grouped_list << group end - sorted_tables.each do |name, table| + sorted_table_names.each do |name| @grouped_list << convert_added_columns(name, true) end end Modified: lib/groonga-schema/differ.rb (+32 -4) =================================================================== --- lib/groonga-schema/differ.rb 2016-08-15 17:18:09 +0900 (3bb2fbc) +++ lib/groonga-schema/differ.rb 2016-08-15 18:19:42 +0900 (f90bb11) @@ -46,9 +46,9 @@ module GroongaSchema from_tables.each do |name, from_table| to_table = to_tables[name] if to_table.nil? - diff.removed_tables[name] = from_table + diff.removed_tables[from_table.name] = from_table elsif from_table != to_table - diff.changed_tables[name] = to_table + diff_changed_table(diff, to_table) end end @@ -60,6 +60,20 @@ module GroongaSchema end end + def diff_changed_table(diff, to_table) + diff.changed_tables[to_table.name] = to_table + + to_table.related_tables.each do |table| + diff.changed_tables[table.name] = table + end + + to_table.related_columns.each do |column| + table_name = column.table_name + diff.changed_columns[table_name] ||= {} + diff.changed_columns[table_name][column.name] = column + end + end + def diff_columns(diff) @from.columns.each do |table_name, from_columns| to_columns =****@to*****[table_name] @@ -71,8 +85,7 @@ module GroongaSchema diff.removed_columns[table_name] ||= {} diff.removed_columns[table_name][name] = from_column elsif from_column != to_column - diff.changed_columns[table_name] ||= {} - diff.changed_columns[table_name][name] = to_column + diff_changed_column(diff, to_column) end end end @@ -88,5 +101,20 @@ module GroongaSchema end end end + + def diff_changed_column(diff, to_column) + table_name = to_column.table_name + name = to_column.name + + diff.changed_columns[table_name] ||= {} + diff.changed_columns[table_name][name] = to_column + + to_column.related_columns.each do |related_column| + related_table_name = related_column.table_name + related_name = related_column.name + diff.changed_columns[related_table_name] ||= {} + diff.changed_columns[related_table_name][related_name] = related_column + end + end end end Modified: lib/groonga-schema/schema.rb (+41 -17) =================================================================== --- lib/groonga-schema/schema.rb 2016-08-15 17:18:09 +0900 (b80a7b7) +++ lib/groonga-schema/schema.rb 2016-08-15 18:19:42 +0900 (8bfdda7) @@ -38,26 +38,50 @@ module GroongaSchema plugin = Plugin.new(command.name) @plugins << plugin when "table_create" - table = Table.new(command.name) - table.apply_command(command) - if****@table*****?(table.key_type) - table.reference_key_type = true - end - @tables[table.name] = table - @columns[table.name] ||= {} + apply_command_table_create(command) when "column_create" - column = Column.new(command.table, command.name) - column.apply_command(command) - if****@table*****?(column.value_type) - column.reference_value_type = true - end - table = @tables[column.table_name] - if table - table.columns << column + apply_command_column_create(command) + end + end + + private + def apply_command_table_create(command) + table = Table.new(command.name) + table.apply_command(command) + refered_table = @tables[table.key_type] + if refered_table + table.reference_key_type = true + refered_table.related_tables << table + end + @tables[table.name] = table + @columns[table.name] ||= {} + end + + def apply_command_column_create(command) + column = Column.new(command.table, command.name) + column.apply_command(command) + + table = @tables[column.table_name] + if table + table.columns << column + end + + refered_table = @tables[column.value_type] + if refered_table + column.reference_value_type = true + refered_table.related_columns << column + refered_columns = @columns[column.value_type] || {} + column.sources.each do |source| + next if source == "_key" + source_column = refered_columns[source] + if source_column + source_column.related_columns << column + end end - @columns[column.table_name] ||= {} - @columns[column.table_name][column.name] = column end + + @columns[column.table_name] ||= {} + @columns[column.table_name][column.name] = column end end end Modified: lib/groonga-schema/table.rb (+4 -0) =================================================================== --- lib/groonga-schema/table.rb 2016-08-15 17:18:09 +0900 (2b199e4) +++ lib/groonga-schema/table.rb 2016-08-15 18:19:42 +0900 (566a271) @@ -26,6 +26,8 @@ module GroongaSchema attr_accessor :token_filters attr_writer :reference_key_type attr_accessor :columns + attr_accessor :related_tables + attr_accessor :related_columns def initialize(name) @name = name @type = :no_key @@ -37,6 +39,8 @@ module GroongaSchema @token_filters = [] @reference_key_type = false @columns = [] + @related_tables = [] + @related_columns = [] end def reference_key_type? Modified: test/test-diff.rb (+30 -0) =================================================================== --- test/test-diff.rb 2016-08-15 17:18:09 +0900 (1acfa44) +++ test/test-diff.rb 2016-08-15 18:19:42 +0900 (4b15df4) @@ -175,6 +175,36 @@ column_create \\ LIST end + test "added columns" do + @diff.added_columns["Commands"] = { + "description" => column("Commands", "description", + :value_type => "Text"), + } + @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) +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 + test "removed columns" do @diff.removed_columns["Words"] = { "weight" => column("Words", "weight", -------------- next part -------------- HTML����������������������������... Download