[Groonga-commit] ranguba/groonga-client-model at 74435e1 [master] Support migration

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Fri Mar 3 20:48:37 JST 2017


Kouhei Sutou	2017-03-03 20:48:37 +0900 (Fri, 03 Mar 2017)

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

  Message:
    Support migration

  Added files:
    lib/rails/generators/groonga_client_model/migration/templates/create_table_migration.rb
    lib/rails/generators/groonga_client_model/migration/templates/migration.rb
    lib/rails/generators/groonga_client_model/migration_generator.rb
  Modified files:
    lib/groonga_client_model/migration.rb
    lib/groonga_client_model/migrator.rb
    lib/rails/generators/groonga_client_model/model_generator.rb

  Modified: lib/groonga_client_model/migration.rb (+21 -0)
===================================================================
--- lib/groonga_client_model/migration.rb    2017-03-03 10:48:44 +0900 (3b5aa4c)
+++ lib/groonga_client_model/migration.rb    2017-03-03 20:48:37 +0900 (8031840)
@@ -23,6 +23,27 @@ module GroongaClientModel
   class IrreversibleMigrationError < MigrationError
   end
 
+  class IllegalMigrationNameError < MigrationError
+    class << self
+      def validate(name)
+        case name
+        when /\A[_a-z0-9]+\z/
+          # OK
+        else
+          raise new(name)
+        end
+      end
+    end
+
+    attr_reader :name
+    def initialize(name)
+      @name = name
+      message = "Illegal name for migration file: #{name}\n"
+      message << "\t(only lower case letters, numbers, and '_' allowed)."
+      super(message)
+    end
+  end
+
   class Migration
     attr_accessor :output
 

  Modified: lib/groonga_client_model/migrator.rb (+6 -0)
===================================================================
--- lib/groonga_client_model/migrator.rb    2017-03-03 10:48:44 +0900 (278a39e)
+++ lib/groonga_client_model/migrator.rb    2017-03-03 20:48:37 +0900 (34d6e55)
@@ -18,6 +18,12 @@ require "groonga_client_model/migration"
 
 module GroongaClientModel
   class Migrator
+    class << self
+      def next_migration_number(number)
+        [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % number].max
+      end
+    end
+
     def initialize(search_paths, target_version)
       @search_paths = Array(search_paths)
       @target_version = target_version

  Added: lib/rails/generators/groonga_client_model/migration/templates/create_table_migration.rb (+12 -0) 100644
===================================================================
--- /dev/null
+++ lib/rails/generators/groonga_client_model/migration/templates/create_table_migration.rb    2017-03-03 20:48:37 +0900 (e4e83cf)
@@ -0,0 +1,12 @@
+class <%= migration_class_name %> < GroongaClientModel::Migration
+  def change
+    create_table :<%= table_name %><%= create_table_options %> do |t|
+<% target_attributes.each do |attribute| -%>
+      t.<%= attribute.type %> :<%= attribute.name %><%= attribute.inject_options %>
+<% end -%>
+<% if options[:timestamps] -%>
+      t.timestamps
+<% end -%>
+    end
+  end
+end

  Added: lib/rails/generators/groonga_client_model/migration/templates/migration.rb (+11 -0) 100644
===================================================================
--- /dev/null
+++ lib/rails/generators/groonga_client_model/migration/templates/migration.rb    2017-03-03 20:48:37 +0900 (31ef293)
@@ -0,0 +1,11 @@
+class <%= migration_class_name %> < GroongaClientModel::Migration
+  def change
+<%- attributes.each do |attribute| -%>
+  <%- if migration_action == "add" -%>
+    add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
+  <%- else -%>
+    remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %>
+  <%- end -%>
+<%- end -%>
+  end
+end

  Added: lib/rails/generators/groonga_client_model/migration_generator.rb (+103 -0) 100644
===================================================================
--- /dev/null
+++ lib/rails/generators/groonga_client_model/migration_generator.rb    2017-03-03 20:48:37 +0900 (3acd94f)
@@ -0,0 +1,103 @@
+# Copyright (C) 2017  Kouhei Sutou <kou �� clear-code.com>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+require "rails/generators/named_base"
+require "rails/generators/migration"
+
+require "groonga-client-model"
+require "groonga_client_model/migration"
+require "groonga_client_model/migrator"
+
+module GroongaClientModel
+  module Generators
+    class MigrationGenerator < Rails::Generators::NamedBase
+      include Rails::Generators::Migration
+
+      class << self
+        def next_migration_number(dirname)
+          next_migration_number = current_migration_number(dirname) + 1
+          Migrator.next_migration_number(next_migration_number)
+        end
+      end
+
+      source_root File.join(__dir__, "migration", "templates")
+
+      argument :attributes,
+               type: :array,
+               default: [],
+               banner: "name:type name:type"
+
+      class_option :table_type,
+                   type: :string,
+                   desc: "The table type (array, hash_table, patricia_trie or double_array_trie)"
+      class_option :table_propose,
+                   type: :string,
+                   desc: "The table propose (full_text_search)"
+      class_option :timestamps,
+                   type: :boolean,
+                   desc: "Whether creating columns for timestamps or not"
+
+      def create_migration_file
+        IllegalMigrationNameError.validate(file_name)
+        decide_template
+        migration_template(@migration_template,
+                           File.join("db/groonga/migrate", "#{file_name}.rb"))
+      end
+
+      private
+      def decide_template
+        @migration_template = "migration.rb"
+        @key_type = nil
+        case file_name
+        when /\A(add|remove)_.*_(?:to|from)_(.*)\z/
+          @migration_action = $1
+          @table_name = normalize_table_name($2)
+        when /\Acreate_(.+)\z/
+          @table_name = normalize_table_name($1)
+          @migration_template = "create_table_migration.rb"
+          attributes.each do |attribute|
+            if attribute.name == "_key"
+              @key_type = attribute.type
+              break
+            end
+          end
+        end
+      end
+
+      def normalize_table_name(name)
+        name.pluralize
+      end
+
+      def create_table_options
+        table_type = @options[:table_type]
+        table_propose = @options[:table_propose]
+        options_text = ""
+        if table_type
+          options_text = ", type: :#{table_type}"
+          options_text << ", key_type: :#{@key_type}" if @key_type
+        end
+        options_text << ", propose: :#{table_propose}" if table_propose
+        options_text
+      end
+
+      def target_attributes
+        attributes.reject do |attribute|
+          attribute.name == "_key"
+        end
+      end
+    end
+  end
+end

  Modified: lib/rails/generators/groonga_client_model/model_generator.rb (+2 -2)
===================================================================
--- lib/rails/generators/groonga_client_model/model_generator.rb    2017-03-03 10:48:44 +0900 (4dcaeb6)
+++ lib/rails/generators/groonga_client_model/model_generator.rb    2017-03-03 20:48:37 +0900 (2811137)
@@ -22,6 +22,8 @@ require "groonga-client-model"
 module GroongaClientModel
   module Generators
     class ModelGenerator < Rails::Generators::NamedBase
+      check_class_collision
+
       source_root File.join(__dir__, "model", "templates")
 
       argument :attributes,
@@ -29,8 +31,6 @@ module GroongaClientModel
                default: [],
                banner: "field[:type][:index] field[:type][:index]"
 
-      check_class_collision
-
       class_option :parent,
                    type: :string,
                    desc: "The parent class for the generated model"
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index