Kouhei Sutou
null+****@clear*****
Tue Mar 7 15:39:26 JST 2017
Kouhei Sutou 2017-03-07 15:39:26 +0900 (Tue, 07 Mar 2017) New Revision: 7b3ed8648aa3d2dad6ab205b85e8c8e8a32a15f2 https://github.com/ranguba/groonga-client-model/commit/7b3ed8648aa3d2dad6ab205b85e8c8e8a32a15f2 Message: migration: support load Added files: test/unit/migration/test_load.rb Modified files: lib/groonga_client_model/migration.rb Modified: lib/groonga_client_model/migration.rb (+23 -0) =================================================================== --- lib/groonga_client_model/migration.rb 2017-03-07 15:13:55 +0900 (8be8aaa) +++ lib/groonga_client_model/migration.rb 2017-03-07 15:39:26 +0900 (affa521) @@ -216,6 +216,29 @@ module GroongaClientModel end end + def load(table_name, values, options={}) + if @reverting + message = "can't revert load(#{table_name.inspect})" + raise IrreversibleMigrationError, message + end + + case values + when Hash + json_values = [values].to_json + when Array + json_values = values.to_json + else + json_values = values + end + report(__method__, [table_name]) do + @client.request(:load). + parameter(:table, table_name). + values_parameter(:columns, options[:columns]). + parameter(:values, json_values). + response + end + end + private def puts(*args) if @output Added: test/unit/migration/test_load.rb (+83 -0) 100644 =================================================================== --- /dev/null +++ test/unit/migration/test_load.rb 2017-03-07 15:39:26 +0900 (2ce787c) @@ -0,0 +1,83 @@ +# 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 TestMigrationLoad < Test::Unit::TestCase + include GroongaClientModel::TestHelper + include TestHelper::Migration + + def assert_migrate_load(values, expected_values_dump) + expected_up_report = <<-REPORT +-- create_table(:posts, {:type=>"TABLE_HASH_KEY", :key_type=>"ShortText"}) + -> 0.0s +-- add_column(:posts, :content, {:flags=>["COLUMN_SCALAR"], :value_type=>"Text"}) + -> 0.0s +-- load(:posts) + -> 0.0s + REPORT + expected_down_report = nil + expected_dump = <<-DUMP.chomp +table_create posts TABLE_HASH_KEY ShortText +column_create posts content COLUMN_SCALAR Text + +load --table posts +[ +["_key","content"], +#{expected_values_dump} +] + DUMP + assert_migrate(expected_up_report, + expected_down_report, + expected_dump) do |migration| + migration.instance_eval do + create_table(:posts, + :type => :hash_table, + :key_type => :short_text) do |t| + t.text(:content) + end + load(:posts, values) + end + end + end + + test("Hash") do + assert_migrate_load({"_key" => "Groonga", "content" => "Very good!"}, + <<-DUMP.chomp) +["Groonga","Very good!"] + DUMP + end + + test("Array") do + assert_migrate_load([ + {"_key" => "Groonga", "content" => "Very good!"}, + {"_key" => "Ruby", "content" => "Very exciting!"}, + ], + <<-DUMP.chomp) +["Groonga","Very good!"], +["Ruby","Very exciting!"] + DUMP + end + + test("JSON") do + assert_migrate_load([ + {"_key" => "Groonga", "content" => "Very good!"}, + {"_key" => "Ruby", "content" => "Very exciting!"}, + ].to_json, + <<-DUMP.chomp) +["Groonga","Very good!"], +["Ruby","Very exciting!"] + DUMP + end +end -------------- next part -------------- HTML����������������������������...Download