Kouhei Sutou
null+****@clear*****
Mon Aug 15 17:18:09 JST 2016
Kouhei Sutou 2016-08-15 17:18:09 +0900 (Mon, 15 Aug 2016) New Revision: f770595144cbce9011c20c41a22a209ceae08abf https://github.com/groonga/groonga-schema/commit/f770595144cbce9011c20c41a22a209ceae08abf Message: Support outputting changed columns Modified files: lib/groonga-schema/column.rb lib/groonga-schema/diff.rb test/test-diff.rb Modified: lib/groonga-schema/column.rb (+51 -5) =================================================================== --- lib/groonga-schema/column.rb 2016-08-15 16:37:06 +0900 (76db0f7) +++ lib/groonga-schema/column.rb 2016-08-15 17:18:09 +0900 (a9926d0) @@ -62,12 +62,50 @@ module GroongaSchema end def to_create_groonga_command + column_create_command(@name) + end + + def to_remove_groonga_command + column_remove_command(@name) + end + + def to_copy_groonga_command(to_table_name, to_name) + column_copy_command(to_table_name, to_name) + end + + def to_migrate_start_groonga_commands + commands = [] + commands << column_create_command(new_name) + if type != :index + commands << column_copy_command(@table_name, new_name) + end + commands << column_rename_command(@name, old_name) + commands << column_rename_command(new_name, @name) + commands + end + + def to_migrate_finish_groonga_commands + [ + column_remove_command(old_name), + ] + end + + private + def old_name + "#{@name}_old" + end + + def new_name + "#{@name}_new" + end + + def column_create_command(name) flags_value = [type_flag, *flags].join("|") sources_value =****@sourc*****(",") sources_value = nil if sources_value.empty? arguments = { "table" => @table_name, - "name" => @name, + "name" => name, "flags" => flags_value, "type" => @value_type, "source" => sources_value, @@ -75,15 +113,15 @@ module GroongaSchema Groonga::Command::ColumnCreate.new(arguments) end - def to_remove_groonga_command + def column_remove_command(name) arguments = { "table" => @table_name, - "name" => @name, + "name" => name, } Groonga::Command::ColumnRemove.new(arguments) end - def to_copy_groonga_command(to_table_name, to_name) + def column_copy_command(to_table_name, to_name) arguments = { "from_table" => @table_name, "from_name" => @name, @@ -93,7 +131,15 @@ module GroongaSchema Groonga::Command::ColumnCopy.new(arguments) end - private + def column_rename_command(name, new_name) + arguments = { + "table" => @table_name, + "name" => name, + "new_name" => new_name, + } + Groonga::Command::ColumnRename.new(arguments) + end + def type_flag case @type when :scalar Modified: lib/groonga-schema/diff.rb (+30 -0) =================================================================== --- lib/groonga-schema/diff.rb 2016-08-15 16:37:06 +0900 (1e60a08) +++ lib/groonga-schema/diff.rb 2016-08-15 17:18:09 +0900 (2e3d4bc) @@ -207,11 +207,41 @@ module GroongaSchema sorted_tables.each do |name, table| @grouped_list << table.to_migrate_start_groonga_commands end + convert_changed_columns sorted_tables.each do |name, table| @grouped_list << table.to_migrate_finish_groonga_commands end end + def convert_changed_columns + all_columns = [] + @diff.changed_columns.each do |table_name, columns| + all_columns.concat(columns.values) + end + + sorted_columns = all_columns.sort_by do |column| + [ + (column.type == :index) ? 1 : 0, + column.table_name, + column.name, + ] + end + sorted_columns.each do |column| + @grouped_list << column.to_migrate_start_groonga_commands + end + + sorted_columns = all_columns.sort_by do |column| + [ + (column.type == :index) ? 0 : 1, + column.table_name, + column.name, + ] + end + sorted_columns.each do |column| + @grouped_list << column.to_migrate_finish_groonga_commands + end + end + def format_command(command) case @options[:format] when :uri Modified: test/test-diff.rb (+109 -0) =================================================================== --- test/test-diff.rb 2016-08-15 16:37:06 +0900 (33119ee) +++ test/test-diff.rb 2016-08-15 17:18:09 +0900 (1acfa44) @@ -352,5 +352,114 @@ table_remove \\ --name "Commands_old" LIST end + + test "changed columns" do + @diff.changed_columns["Words"] = { + "weight" => column("Words", "weight", + :value_type => "Float"), + "commands_description" => column("Words", "commands_description", + :type => :index, + :flags => ["WITH_POSITION"], + :value_type => "Commands", + :sources => ["description"], + :reference_value_type => true), + } + @diff.changed_columns["Commands"] = { + "description" => column("Commands", "description", + :value_type => "Text"), + "comment" => column("Commands", "comment", + :value_type => "ShortText"), + } + + assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list) +column_create \\ + --flags "COLUMN_SCALAR" \\ + --name "comment_new" \\ + --table "Commands" \\ + --type "ShortText" +column_copy \\ + --from_name "comment" \\ + --from_table "Commands" \\ + --to_name "comment_new" \\ + --to_table "Commands" +column_rename \\ + --name "comment" \\ + --new_name "comment_old" \\ + --table "Commands" +column_rename \\ + --name "comment_new" \\ + --new_name "comment" \\ + --table "Commands" + +column_create \\ + --flags "COLUMN_SCALAR" \\ + --name "description_new" \\ + --table "Commands" \\ + --type "Text" +column_copy \\ + --from_name "description" \\ + --from_table "Commands" \\ + --to_name "description_new" \\ + --to_table "Commands" +column_rename \\ + --name "description" \\ + --new_name "description_old" \\ + --table "Commands" +column_rename \\ + --name "description_new" \\ + --new_name "description" \\ + --table "Commands" + +column_create \\ + --flags "COLUMN_SCALAR" \\ + --name "weight_new" \\ + --table "Words" \\ + --type "Float" +column_copy \\ + --from_name "weight" \\ + --from_table "Words" \\ + --to_name "weight_new" \\ + --to_table "Words" +column_rename \\ + --name "weight" \\ + --new_name "weight_old" \\ + --table "Words" +column_rename \\ + --name "weight_new" \\ + --new_name "weight" \\ + --table "Words" + +column_create \\ + --flags "COLUMN_INDEX|WITH_POSITION" \\ + --name "commands_description_new" \\ + --source "description" \\ + --table "Words" \\ + --type "Commands" +column_rename \\ + --name "commands_description" \\ + --new_name "commands_description_old" \\ + --table "Words" +column_rename \\ + --name "commands_description_new" \\ + --new_name "commands_description" \\ + --table "Words" + +column_remove \\ + --name "commands_description_old" \\ + --table "Words" + +column_remove \\ + --name "comment_old" \\ + --table "Commands" + +column_remove \\ + --name "description_old" \\ + --table "Commands" + +column_remove \\ + --name "weight_old" \\ + --table "Words" + LIST + end end end -------------- next part -------------- HTML����������������������������... Download