Kouhei Sutou
null+****@clear*****
Mon Mar 6 21:00:39 JST 2017
Kouhei Sutou 2017-03-06 21:00:39 +0900 (Mon, 06 Mar 2017) New Revision: a320e244cf89de752b2040ff2ac88bf146dd0a70 https://github.com/ranguba/groonga-client-model/commit/a320e244cf89de752b2040ff2ac88bf146dd0a70 Message: generator migration: support config_set and config_delete Added files: lib/rails/generators/groonga_client_model/migration/templates/delete_config_migration.rb lib/rails/generators/groonga_client_model/migration/templates/set_config_migration.rb test/apps/rails4/test/generators/migration_generator_test.rb test/apps/rails5/test/generators/migration_generator_test.rb Modified files: lib/rails/generators/groonga_client_model/migration/templates/migration.rb lib/rails/generators/groonga_client_model/migration_generator.rb test/apps/rails4/.gitignore test/apps/rails5/.gitignore Added: lib/rails/generators/groonga_client_model/migration/templates/delete_config_migration.rb (+9 -0) 100644 =================================================================== --- /dev/null +++ lib/rails/generators/groonga_client_model/migration/templates/delete_config_migration.rb 2017-03-06 21:00:39 +0900 (48181f9) @@ -0,0 +1,9 @@ +class <%= migration_class_name %> < GroongaClientModel::Migration + def up + config_delete "<%= @config_key %>" + end + + def down + # config_set "<%= @config_key %>", "old value" + end +end Modified: lib/rails/generators/groonga_client_model/migration/templates/migration.rb (+3 -3) =================================================================== --- lib/rails/generators/groonga_client_model/migration/templates/migration.rb 2017-03-06 16:46:48 +0900 (b0727ac) +++ lib/rails/generators/groonga_client_model/migration/templates/migration.rb 2017-03-06 21:00:39 +0900 (c29cb8e) @@ -2,10 +2,10 @@ class <%= migration_class_name %> < GroongaClientModel::Migration def change <%- attributes.each do |attribute| -%> <%- case @migration_action -%> - <%- when "add" -%> + <%- when :add -%> add_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> - <%- when "remove" -%> - remove_column :<%= table_name %>, :<%= attribute.name %>, :<%= attribute.type %><%= attribute.inject_options %> + <%- when :remove -%> + remove_column :<%= table_name %>, :<%= attribute.name %> <%- end -%> <%- end -%> end Added: lib/rails/generators/groonga_client_model/migration/templates/set_config_migration.rb (+10 -0) 100644 =================================================================== --- /dev/null +++ lib/rails/generators/groonga_client_model/migration/templates/set_config_migration.rb 2017-03-06 21:00:39 +0900 (5b8eeb2) @@ -0,0 +1,10 @@ +class <%= migration_class_name %> < GroongaClientModel::Migration + def up + config_set "<%= @config_key %>", "new value" + end + + def down + # config_set "<%= @config_key %>", "old value" + # config_delete "<%= @config_key %>" + end +end Modified: lib/rails/generators/groonga_client_model/migration_generator.rb (+16 -3) =================================================================== --- lib/rails/generators/groonga_client_model/migration_generator.rb 2017-03-06 16:46:48 +0900 (f1af24f) +++ lib/rails/generators/groonga_client_model/migration_generator.rb 2017-03-06 21:00:39 +0900 (5b33e7a) @@ -63,9 +63,12 @@ module GroongaClientModel @migration_action = nil @key_type = nil case output_file_name - when /\A(add|remove)_.*_(?:to|from)_(.*)\z/ - @migration_action = $1 - @table_name = normalize_table_name($2) + when /\Aadd_.*_to_(.*)\z/ + @migration_action = :add + @table_name = normalize_table_name($1) + when /\Aremove_.*_from_(.*)\z/ + @migration_action = :remove + @table_name = normalize_table_name($1) when /\Acreate_(.+)\z/ @table_name = normalize_table_name($1) @migration_template = "create_table_migration.rb" @@ -75,6 +78,12 @@ module GroongaClientModel break end end + when /\Aset_config_(.*)\z/ + @migration_template = "set_config_migration.rb" + @config_key = normalize_config_key($1) + when /\Adelete_config_(.*)\z/ + @migration_template = "delete_config_migration.rb" + @config_key = normalize_config_key($1) end end @@ -99,6 +108,10 @@ module GroongaClientModel attribute.name == "_key" end end + + def normalize_config_key(key) + key.gsub(/_/, ".") + end end end end Modified: test/apps/rails4/.gitignore (+3 -0) =================================================================== --- test/apps/rails4/.gitignore 2017-03-06 16:46:48 +0900 (5b61ab0) +++ test/apps/rails4/.gitignore 2017-03-06 21:00:39 +0900 (81ce0b9) @@ -11,3 +11,6 @@ /log/* !/log/.keep /tmp + +# Ignore temporary files for test. +/test/tmp/ Added: test/apps/rails4/test/generators/migration_generator_test.rb (+73 -0) 100644 =================================================================== --- /dev/null +++ test/apps/rails4/test/generators/migration_generator_test.rb 2017-03-06 21:00:39 +0900 (f480add) @@ -0,0 +1,73 @@ +require "test_helper" +require "rails/generators/groonga_client_model/migration_generator" + +class MigrationGeneratorTest < Rails::Generators::TestCase + tests GroongaClientModel::Generators::MigrationGenerator + destination File.expand_path("../tmp", __dir__) + setup :prepare_destination + + test "add_column" do + run_generator(["add_title_to_posts", "title:short_text"]) + assert_migration("db/groonga/migrate/add_title_to_posts.rb", <<-MIGRATION) +class AddTitleToPosts < GroongaClientModel::Migration + def change + add_column :posts, :title, :short_text + end +end + MIGRATION + end + + test "remove_column" do + run_generator(["remove_title_from_posts", "title"]) + assert_migration("db/groonga/migrate/remove_title_from_posts.rb", <<-MIGRATION) +class RemoveTitleFromPosts < GroongaClientModel::Migration + def change + remove_column :posts, :title + end +end + MIGRATION + end + + test "create_table" do + run_generator(["create_posts"]) + assert_migration("db/groonga/migrate/create_posts.rb", <<-MIGRATION) +class CreatePosts < GroongaClientModel::Migration + def change + create_table :posts do |t| + end + end +end + MIGRATION + end + + test "set_config" do + run_generator(["set_config_alias_column"]) + assert_migration("db/groonga/migrate/set_config_alias_column.rb", <<-MIGRATION) +class SetConfigAliasColumn < GroongaClientModel::Migration + def up + config_set "alias.column", "new value" + end + + def down + # config_set "alias.column", "old value" + # config_delete "alias.column" + end +end + MIGRATION + end + + test "delete_config" do + run_generator(["delete_config_alias_column"]) + assert_migration("db/groonga/migrate/delete_config_alias_column.rb", <<-MIGRATION) +class DeleteConfigAliasColumn < GroongaClientModel::Migration + def up + config_delete "alias.column" + end + + def down + # config_set "alias.column", "old value" + end +end + MIGRATION + end +end Modified: test/apps/rails5/.gitignore (+3 -0) =================================================================== --- test/apps/rails5/.gitignore 2017-03-06 16:46:48 +0900 (48fb168) +++ test/apps/rails5/.gitignore 2017-03-06 21:00:39 +0900 (08b265a) @@ -15,3 +15,6 @@ # Ignore Byebug command history file. .byebug_history + +# Ignore temporary files for test. +/test/tmp/ Added: test/apps/rails5/test/generators/migration_generator_test.rb (+73 -0) 100644 =================================================================== --- /dev/null +++ test/apps/rails5/test/generators/migration_generator_test.rb 2017-03-06 21:00:39 +0900 (f480add) @@ -0,0 +1,73 @@ +require "test_helper" +require "rails/generators/groonga_client_model/migration_generator" + +class MigrationGeneratorTest < Rails::Generators::TestCase + tests GroongaClientModel::Generators::MigrationGenerator + destination File.expand_path("../tmp", __dir__) + setup :prepare_destination + + test "add_column" do + run_generator(["add_title_to_posts", "title:short_text"]) + assert_migration("db/groonga/migrate/add_title_to_posts.rb", <<-MIGRATION) +class AddTitleToPosts < GroongaClientModel::Migration + def change + add_column :posts, :title, :short_text + end +end + MIGRATION + end + + test "remove_column" do + run_generator(["remove_title_from_posts", "title"]) + assert_migration("db/groonga/migrate/remove_title_from_posts.rb", <<-MIGRATION) +class RemoveTitleFromPosts < GroongaClientModel::Migration + def change + remove_column :posts, :title + end +end + MIGRATION + end + + test "create_table" do + run_generator(["create_posts"]) + assert_migration("db/groonga/migrate/create_posts.rb", <<-MIGRATION) +class CreatePosts < GroongaClientModel::Migration + def change + create_table :posts do |t| + end + end +end + MIGRATION + end + + test "set_config" do + run_generator(["set_config_alias_column"]) + assert_migration("db/groonga/migrate/set_config_alias_column.rb", <<-MIGRATION) +class SetConfigAliasColumn < GroongaClientModel::Migration + def up + config_set "alias.column", "new value" + end + + def down + # config_set "alias.column", "old value" + # config_delete "alias.column" + end +end + MIGRATION + end + + test "delete_config" do + run_generator(["delete_config_alias_column"]) + assert_migration("db/groonga/migrate/delete_config_alias_column.rb", <<-MIGRATION) +class DeleteConfigAliasColumn < GroongaClientModel::Migration + def up + config_delete "alias.column" + end + + def down + # config_set "alias.column", "old value" + end +end + MIGRATION + end +end -------------- next part -------------- HTML����������������������������...Download