Kouhei Sutou
null+****@clear*****
Wed Mar 8 08:57:03 JST 2017
Kouhei Sutou 2017-03-08 08:57:03 +0900 (Wed, 08 Mar 2017) New Revision: 4075f667520da7e6a05ddf83738c3bb85dd9b739 https://github.com/ranguba/groonga-client-model/commit/4075f667520da7e6a05ddf83738c3bb85dd9b739 Message: migration: add exist? and reverting? Added files: test/unit/migration/test_exist.rb Modified files: lib/groonga_client_model/migration.rb Modified: lib/groonga_client_model/migration.rb (+25 -14) =================================================================== --- lib/groonga_client_model/migration.rb 2017-03-07 21:36:14 +0900 (851ea6a) +++ lib/groonga_client_model/migration.rb 2017-03-08 08:57:03 +0900 (67a1677) @@ -65,20 +65,6 @@ module GroongaClientModel end 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, type: nil, key_type: nil, @@ -287,6 +273,17 @@ module GroongaClientModel end end + def exist?(name) + @client.request(:object_exist). + parameter(:name, name). + response. + body + end + + def reverting? + @reverting + end + private def puts(*args) if @output @@ -305,6 +302,20 @@ module GroongaClientModel puts(" -> %.4fs" % time.real) 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 normalize_table_type(type) case type.to_s when "array", /\A(?:TABLE_)?NO_KEY\z/i Added: test/unit/migration/test_exist.rb (+47 -0) 100644 =================================================================== --- /dev/null +++ test/unit/migration/test_exist.rb 2017-03-08 08:57:03 +0900 (f01a25d) @@ -0,0 +1,47 @@ +# 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 + +class TestMigrationExist < Test::Unit::TestCase + include GroongaClientModel::TestHelper + include TestHelper::Migration + + test("table") do + expected_up_report = <<-REPORT +-- create_table(:posts, {:type=>"TABLE_NO_KEY"}) + -> 0.0s +-- add_column(:posts, :title, {:flags=>["COLUMN_SCALAR"], :value_type=>"ShortText"}) + -> 0.0s + REPORT + expected_down_report = <<-REPORT +-- remove_table(:posts) + -> 0.0s + REPORT + expected_dump = <<-DUMP.chomp +table_create posts TABLE_NO_KEY +column_create posts title COLUMN_SCALAR ShortText + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts) + unless reverting? + add_column(:posts, :title, :short_text) if exist?(:posts) + end + end + end + end +end -------------- next part -------------- HTML����������������������������...Download