[Groonga-commit] groonga/groonga-schema at 5daba2c [master] Support outputting changed tables

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Aug 15 16:37:06 JST 2016


Kouhei Sutou	2016-08-15 16:37:06 +0900 (Mon, 15 Aug 2016)

  New Revision: 5daba2c3042575f60747d3b984f7cb7154acbbc6
  https://github.com/groonga/groonga-schema/commit/5daba2c3042575f60747d3b984f7cb7154acbbc6

  Message:
    Support outputting changed tables

  Modified files:
    groonga-schema.gemspec
    lib/groonga-schema/column.rb
    lib/groonga-schema/diff.rb
    lib/groonga-schema/schema.rb
    lib/groonga-schema/table.rb
    test/test-diff.rb

  Modified: groonga-schema.gemspec (+1 -1)
===================================================================
--- groonga-schema.gemspec    2016-08-15 12:20:44 +0900 (88d140e)
+++ groonga-schema.gemspec    2016-08-15 16:37:06 +0900 (856ea68)
@@ -52,7 +52,7 @@ Gem::Specification.new do |spec|
   spec.licenses = ["LGPLv2.1+"]
   spec.require_paths = ["lib"]
 
-  spec.add_runtime_dependency("groonga-command", ">= 1.2.4")
+  spec.add_runtime_dependency("groonga-command", ">= 1.2.7")
   spec.add_runtime_dependency("groonga-command-parser")
 
   spec.add_development_dependency("test-unit")

  Modified: lib/groonga-schema/column.rb (+18 -0)
===================================================================
--- lib/groonga-schema/column.rb    2016-08-15 12:20:44 +0900 (8161ab2)
+++ lib/groonga-schema/column.rb    2016-08-15 16:37:06 +0900 (76db0f7)
@@ -42,6 +42,14 @@ module GroongaSchema
       applier.apply
     end
 
+    def apply_column(column)
+      self.type                 = column.type
+      self.flags                = column.flags
+      self.value_type           = column.value_type
+      self.sources              = column.sources
+      self.reference_value_type = column.reference_value_type?
+    end
+
     def ==(other)
       return false unless other.is_a?(self.class)
 
@@ -75,6 +83,16 @@ module GroongaSchema
       Groonga::Command::ColumnRemove.new(arguments)
     end
 
+    def to_copy_groonga_command(to_table_name, to_name)
+      arguments = {
+        "from_table" => @table_name,
+        "from_name"  => @name,
+        "to_table"   => to_table_name,
+        "to_name"    => to_name,
+      }
+      Groonga::Command::ColumnCopy.new(arguments)
+    end
+
     private
     def type_flag
       case @type

  Modified: lib/groonga-schema/diff.rb (+17 -0)
===================================================================
--- lib/groonga-schema/diff.rb    2016-08-15 12:20:44 +0900 (682b0aa)
+++ lib/groonga-schema/diff.rb    2016-08-15 16:37:06 +0900 (1e60a08)
@@ -83,6 +83,7 @@ module GroongaSchema
         convert_removed_columns
         convert_removed_tables
         convert_removed_plugins
+        convert_changed_tables
 
         meaningful_grouped_list = @grouped_list.reject do |group|
           group.empty?
@@ -195,6 +196,22 @@ module GroongaSchema
         @grouped_list << sorted_plugins.collect(&:to_unregister_groonga_command)
       end
 
+      def convert_changed_tables
+        sorted_tables =****@diff*****_tables.sort_by do |name, table|
+          [
+            table.reference_key_type? ? 1 : 0,
+            table.name,
+          ]
+        end
+
+        sorted_tables.each do |name, table|
+          @grouped_list << table.to_migrate_start_groonga_commands
+        end
+        sorted_tables.each do |name, table|
+          @grouped_list << table.to_migrate_finish_groonga_commands
+        end
+      end
+
       def format_command(command)
         case @options[:format]
         when :uri

  Modified: lib/groonga-schema/schema.rb (+4 -0)
===================================================================
--- lib/groonga-schema/schema.rb    2016-08-15 12:20:44 +0900 (32364c1)
+++ lib/groonga-schema/schema.rb    2016-08-15 16:37:06 +0900 (b80a7b7)
@@ -51,6 +51,10 @@ module GroongaSchema
         if****@table*****?(column.value_type)
           column.reference_value_type = true
         end
+        table = @tables[column.table_name]
+        if table
+          table.columns << column
+        end
         @columns[column.table_name] ||= {}
         @columns[column.table_name][column.name] = column
       end

  Modified: lib/groonga-schema/table.rb (+64 -4)
===================================================================
--- lib/groonga-schema/table.rb    2016-08-15 12:20:44 +0900 (71dd8fa)
+++ lib/groonga-schema/table.rb    2016-08-15 16:37:06 +0900 (2b199e4)
@@ -25,6 +25,7 @@ module GroongaSchema
     attr_accessor :normalizer
     attr_accessor :token_filters
     attr_writer :reference_key_type
+    attr_accessor :columns
     def initialize(name)
       @name = name
       @type = :no_key
@@ -35,6 +36,7 @@ module GroongaSchema
       @normalizer = nil
       @token_filters = []
       @reference_key_type = false
+      @columns = []
     end
 
     def reference_key_type?
@@ -60,11 +62,54 @@ module GroongaSchema
     end
 
     def to_create_groonga_command
+      table_create_command(@name)
+    end
+
+    def to_remove_groonga_command
+      table_remove_command(@name)
+    end
+
+    def to_migrate_start_groonga_commands
+      commands = []
+      commands << table_create_command(new_name)
+      if****@colum*****?
+        commands << table_copy_command(@name, new_name)
+      else
+        sorted_columns =****@colum*****_by(&:name)
+        sorted_columns.each do |column|
+          new_column = Column.new(new_name, column.name)
+          new_column.apply_column(column)
+          commands << new_column.to_create_groonga_command
+          commands << column.to_copy_groonga_command(new_name,
+                                                     column.name)
+        end
+      end
+      commands << table_rename_command(@name, old_name)
+      commands << table_rename_command(new_name, @name)
+      commands
+    end
+
+    def to_migrate_finish_groonga_commands
+      [
+        table_remove_command(old_name),
+      ]
+    end
+
+    private
+    def old_name
+      "#{@name}_old"
+    end
+
+    def new_name
+      "#{@name}_new"
+    end
+
+    def table_create_command(name)
       flags_value = [type_flag, *flags].join("|")
       token_filters_value = @token_filters.join("|")
       token_filters_value = nil if token_filters_value.empty?
       arguments = {
-        "name"              => @name,
+        "name"              => name,
         "flags"             => flags_value,
         "key_type"          => @key_type,
         "value_type"        => @value_type,
@@ -75,14 +120,29 @@ module GroongaSchema
       Groonga::Command::TableCreate.new(arguments)
     end
 
-    def to_remove_groonga_command
+    def table_remove_command(name)
       arguments = {
-        "name" => @name,
+        "name" => name,
       }
       Groonga::Command::TableRemove.new(arguments)
     end
 
-    private
+    def table_copy_command(from_name, to_name)
+      arguments = {
+        "from_name" => from_name,
+        "to_name"   => to_name,
+      }
+      Groonga::Command::TableCopy.new(arguments)
+    end
+
+    def table_rename_command(name, new_name)
+      arguments = {
+        "name"     => name,
+        "new_name" => new_name,
+      }
+      Groonga::Command::TableRename.new(arguments)
+    end
+
     def type_flag
       case @type
       when :no_key

  Modified: test/test-diff.rb (+122 -0)
===================================================================
--- test/test-diff.rb    2016-08-15 12:20:44 +0900 (16cfa4e)
+++ test/test-diff.rb    2016-08-15 16:37:06 +0900 (33119ee)
@@ -230,5 +230,127 @@ table_remove \\
   --name "Names"
       LIST
     end
+
+    test "changed tables - without columns" do
+      @diff.changed_tables["Names"] = table("Names",
+                                            :type => :hash_key,
+                                            :flags => "KEY_LARGE",
+                                            :key_type => "ShortText",
+                                            :normalizer => "NormalizerAuto")
+      @diff.changed_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_new" \\
+  --normalizer "NormalizerAuto"
+table_copy \\
+  --from_name "Names" \\
+  --to_name "Names_new"
+table_rename \\
+  --name "Names" \\
+  --new_name "Names_old"
+table_rename \\
+  --name "Names_new" \\
+  --new_name "Names"
+
+table_create \\
+  --flags "TABLE_HASH_KEY" \\
+  --key_type "Names" \\
+  --name "Commands_new"
+table_copy \\
+  --from_name "Commands" \\
+  --to_name "Commands_new"
+table_rename \\
+  --name "Commands" \\
+  --new_name "Commands_old"
+table_rename \\
+  --name "Commands_new" \\
+  --new_name "Commands"
+
+table_remove \\
+  --name "Names_old"
+
+table_remove \\
+  --name "Commands_old"
+      LIST
+    end
+
+    test "changed tables - with columns" do
+      @diff.changed_tables["Names"] = table("Names",
+                                            :type => :hash_key,
+                                            :flags => "KEY_LARGE",
+                                            :key_type => "ShortText",
+                                            :normalizer => "NormalizerAuto")
+      commands_columns = [
+        column("Commands", "description",
+               :value_type => "Text"),
+        column("Commands", "comment",
+               :value_type => "ShortText"),
+      ]
+      @diff.changed_tables["Commands"] = table("Commands",
+                                               :type => :hash_key,
+                                               :key_type => "Names",
+                                               :reference_key_type => true,
+                                               :columns => commands_columns)
+
+      assert_equal(<<-LIST.gsub(/\\\n\s+/, ""), @diff.to_groonga_command_list)
+table_create \\
+  --flags "TABLE_HASH_KEY|KEY_LARGE" \\
+  --key_type "ShortText" \\
+  --name "Names_new" \\
+  --normalizer "NormalizerAuto"
+table_copy \\
+  --from_name "Names" \\
+  --to_name "Names_new"
+table_rename \\
+  --name "Names" \\
+  --new_name "Names_old"
+table_rename \\
+  --name "Names_new" \\
+  --new_name "Names"
+
+table_create \\
+  --flags "TABLE_HASH_KEY" \\
+  --key_type "Names" \\
+  --name "Commands_new"
+column_create \\
+  --flags "COLUMN_SCALAR" \\
+  --name "comment" \\
+  --table "Commands_new" \\
+  --type "ShortText"
+column_copy \\
+  --from_name "comment" \\
+  --from_table "Commands" \\
+  --to_name "comment" \\
+  --to_table "Commands_new"
+column_create \\
+  --flags "COLUMN_SCALAR" \\
+  --name "description" \\
+  --table "Commands_new" \\
+  --type "Text"
+column_copy \\
+  --from_name "description" \\
+  --from_table "Commands" \\
+  --to_name "description" \\
+  --to_table "Commands_new"
+table_rename \\
+  --name "Commands" \\
+  --new_name "Commands_old"
+table_rename \\
+  --name "Commands_new" \\
+  --new_name "Commands"
+
+table_remove \\
+  --name "Names_old"
+
+table_remove \\
+  --name "Commands_old"
+      LIST
+    end
   end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index