[Groonga-commit] ranguba/groonga-client-model at fa15db1 [master] Support multiple action in a change

Back to archive index

Kouhei Sutou null+****@clear*****
Fri Mar 3 10:28:30 JST 2017


Kouhei Sutou	2017-03-03 10:28:30 +0900 (Fri, 03 Mar 2017)

  New Revision: fa15db1e33cb8d9c74c3de7d17ef784c3434f9e3
  https://github.com/ranguba/groonga-client-model/commit/fa15db1e33cb8d9c74c3de7d17ef784c3434f9e3

  Message:
    Support multiple action in a change

  Modified files:
    lib/groonga_client_model/migration.rb
    test/unit/test_migration.rb

  Modified: lib/groonga_client_model/migration.rb (+25 -21)
===================================================================
--- lib/groonga_client_model/migration.rb    2017-03-03 10:22:45 +0900 (42e62ba)
+++ lib/groonga_client_model/migration.rb    2017-03-03 10:28:30 +0900 (be762da)
@@ -30,6 +30,7 @@ module GroongaClientModel
       @client = client
       @output = nil
       @reverting = false
+      @pending_actions = []
     end
 
     def up
@@ -43,12 +44,17 @@ module GroongaClientModel
     end
 
     def revert
+      @pending_actions.clear
       @reverting = true
       begin
         yield
       ensure
         @reverting = false
       end
+      @pending_actions.reverse_each do |action|
+        public_send(*action)
+      end
+      @pending_actions.clear
     end
 
     def create_table(name,
@@ -57,7 +63,10 @@ module GroongaClientModel
                      tokenizer: nil,
                      default_tokenizer: nil,
                      normalizer: nil)
-      return remove_table_raw(name) if @reverting
+      if @reverting
+        @pending_actions << [:remove_table, name]
+        return
+      end
 
       type = normalize_table_type(type || :array)
       if type != "TABLE_NO_KEY" and key_type.nil?
@@ -91,7 +100,11 @@ module GroongaClientModel
         raise IrreversibleMigrationError, "can't revert remove_table(#{name})"
       end
 
-      remove_table_raw(name)
+      report(__method__, [name]) do
+        @client.request(:table_remove).
+          parameter(:name, name).
+          response
+      end
     end
 
     def add_column(table_name, column_name, value_type,
@@ -99,7 +112,10 @@ module GroongaClientModel
                    type: nil,
                    sources: nil,
                    source: nil)
-      return remove_column_raw(name) if @reverting
+      if @reverting
+        @pending_actions << [:remove_column, table_name, column_name]
+        return
+      end
 
       value_type = normalize_type(value_type)
       type = normalize_column_type(type || :scalar)
@@ -136,7 +152,12 @@ module GroongaClientModel
         raise IrreversibleMigrationError, message
       end
 
-      remove_column_raw(table_name, column_name)
+      report(__method__, [table_name, column_name]) do
+        @client.request(:column_remove).
+          parameter(:table_name, table_name).
+          parameter(:name, column_name).
+          response
+      end
     end
 
     private
@@ -240,23 +261,6 @@ module GroongaClientModel
       end
     end
 
-    def remove_table_raw(name)
-      report(:remove_table, [name]) do
-        @client.request(:table_remove).
-          parameter(:name, name).
-          response
-      end
-    end
-
-    def remove_column_raw(table_name, column_name)
-      report(:remove_column, [table_name, column_name]) do
-        @client.request(:column_remove).
-          parameter(:table_name, table_name).
-          parameter(:name, column_name).
-          response
-      end
-    end
-
     class CreateTableMigration
       def initialize(migration, table_name)
         @migration = migration

  Modified: test/unit/test_migration.rb (+2 -2)
===================================================================
--- test/unit/test_migration.rb    2017-03-03 10:22:45 +0900 (cbef845)
+++ test/unit/test_migration.rb    2017-03-03 10:28:30 +0900 (0ca6003)
@@ -254,10 +254,10 @@ column_create posts content COLUMN_SCALAR LongText
    -> 0.0s
           REPORT
           expected_down_report = <<-REPORT
--- remove_table(:posts)
-   -> 0.0s
 -- remove_table(:terms)
    -> 0.0s
+-- remove_table(:posts)
+   -> 0.0s
           REPORT
           expected_dump = <<-DUMP.chomp
 table_create posts TABLE_NO_KEY
-------------- next part --------------
HTML����������������������������...
Download 



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