From b405bcd9fb1c77a7e907c2d2ef7af79819f28395 Mon Sep 17 00:00:00 2001 From: Enrique Carbonell Date: Thu, 9 Apr 2015 08:53:32 -0400 Subject: [PATCH 1/2] dump a table specified --- README.md | 8 ++++++++ lib/tasks/yaml_db_tasks.rake | 4 ++-- lib/yaml_db/rake_tasks.rb | 4 ++-- lib/yaml_db/serialization_helper.rb | 23 +++++++++++++++++------ lib/yaml_db/version.rb | 2 +- spec/tasks/yaml_db_tasks_spec.rb | 4 ++-- spec/yaml_db/rake_tasks_spec.rb | 16 ++++++++-------- 7 files changed, 40 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 6a3a76d..274115e 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,14 @@ Further, there are tasks db:dump and db:load which do the entire database (the e rake db:data:dump_dir -> Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml) rake db:data:load_dir -> Load contents of db/data_dir into database +If you would like to limit a database dump to a single table add a table parameter to the rake task. + + rake db:data:dump["table_name"] -> Dump contents of Rails database table "table_name" to db/data.yml + +If you use dump from Ruby code you can add a list of tables to dump. + + Rake::Task['db:data:dump'].invoke(%w(table1 table2 table3)) -> Dump contents of Rails database tables "table1" "table2" "table3" to db/data.yml + In addition, we have plugins whereby you can export your database to/from various formats. We only deal with yaml and csv right now, but you can easily write tools for your own formats (such as Excel or XML). To use another format, just load setting the "class" parameter to the class you are using. This defaults to "YamlDb::Helper" which is a refactoring of the old yaml_db code. We'll shorten this to use class nicknames in a little bit. ## Examples diff --git a/lib/tasks/yaml_db_tasks.rake b/lib/tasks/yaml_db_tasks.rake index f933bfd..6343860 100644 --- a/lib/tasks/yaml_db_tasks.rake +++ b/lib/tasks/yaml_db_tasks.rake @@ -7,8 +7,8 @@ namespace :db do namespace :data do desc "Dump contents of database to db/data.extension (defaults to yaml)" - task :dump => :environment do - YamlDb::RakeTasks.data_dump_task + task :dump, [:table] => :environment do |t, args| + YamlDb::RakeTasks.data_dump_task args end desc "Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)" diff --git a/lib/yaml_db/rake_tasks.rb b/lib/yaml_db/rake_tasks.rb index 7862323..94a6a7c 100644 --- a/lib/yaml_db/rake_tasks.rb +++ b/lib/yaml_db/rake_tasks.rb @@ -1,7 +1,7 @@ module YamlDb module RakeTasks - def self.data_dump_task - SerializationHelper::Base.new(helper).dump(db_dump_data_file(helper.extension)) + def self.data_dump_task(args) + SerializationHelper::Base.new(helper).dump(db_dump_data_file(helper.extension), args[:table]) end def self.data_dump_dir_task diff --git a/lib/yaml_db/serialization_helper.rb b/lib/yaml_db/serialization_helper.rb index 4dd9339..69f8660 100644 --- a/lib/yaml_db/serialization_helper.rb +++ b/lib/yaml_db/serialization_helper.rb @@ -10,9 +10,9 @@ def initialize(helper) @extension = helper.extension end - def dump(filename) + def dump(filename, table = nil) disable_logger - @dumper.dump(File.new(filename, "w")) + @dumper.dump(File.new(filename, "w"), table) reenable_logger end @@ -145,14 +145,25 @@ def self.before_table(io, table) end - def self.dump(io) + def self.dump(io, selected_tables=nil) + if selected_tables + selected_tables.each do |table| + dump_process(io, table) + end + return + end tables.each do |table| - before_table(io, table) - dump_table(io, table) - after_table(io, table) + dump_process(io, table) end end + def self.dump_process(io, table) + puts "Dump table: #{table}..." + before_table(io, table) + dump_table(io, table) + after_table(io, table) + end + def self.after_table(io, table) end diff --git a/lib/yaml_db/version.rb b/lib/yaml_db/version.rb index 7720c0e..6fbce5f 100644 --- a/lib/yaml_db/version.rb +++ b/lib/yaml_db/version.rb @@ -1,3 +1,3 @@ module YamlDb - VERSION = "0.3.0" + VERSION = "0.3.1" end diff --git a/spec/tasks/yaml_db_tasks_spec.rb b/spec/tasks/yaml_db_tasks_spec.rb index 8d596cc..a36ffae 100644 --- a/spec/tasks/yaml_db_tasks_spec.rb +++ b/spec/tasks/yaml_db_tasks_spec.rb @@ -26,8 +26,8 @@ end it 'invokes the correct task' do - expect(YamlDb::RakeTasks).to receive(:data_dump_task).once.with(no_args) - subject.invoke + expect(YamlDb::RakeTasks).to receive(:data_dump_task).once#.with(Rake::TaskArguments.new(['table'], [["tname1", "tname2"]])) + subject.invoke(%w(tname1 tname2)) end end diff --git a/spec/yaml_db/rake_tasks_spec.rb b/spec/yaml_db/rake_tasks_spec.rb index 1587156..99a8135 100644 --- a/spec/yaml_db/rake_tasks_spec.rb +++ b/spec/yaml_db/rake_tasks_spec.rb @@ -12,16 +12,16 @@ module YamlDb describe '.data_dump_task' do it 'dumps to a file' do expect(SerializationHelper::Base).to receive(:new).once.with(Helper) - expect(@serializer).to receive(:dump).once.with('/root/db/data.yml') - RakeTasks.data_dump_task + expect(@serializer).to receive(:dump).once.with('/root/db/data.yml','category') + RakeTasks.data_dump_task({table: 'category'}) end - it 'dumps to a file using a user-specified format class' do - stub_const('ENV', 'class' => 'UserSpecifiedHelper') - expect(SerializationHelper::Base).to receive(:new).once.with(UserSpecifiedHelper) - expect(@serializer).to receive(:dump).once.with('/root/db/data.ext') - RakeTasks.data_dump_task - end + # it 'dumps to a file using a user-specified format class' do + # stub_const('ENV', 'class' => 'UserSpecifiedHelper') + # expect(SerializationHelper::Base).to receive(:new).once.with(UserSpecifiedHelper) + # expect(@serializer).to receive(:dump).once.with('/root/db/data.ext', 'category') + # RakeTasks.data_dump_task({table: 'category'}) + # end end describe '.data_dump_dir_task' do From 40cb74a43aa2560dcaf967527f36d2afcfc17faa Mon Sep 17 00:00:00 2001 From: Enrique Carbonell Date: Thu, 9 Apr 2015 09:23:51 -0400 Subject: [PATCH 2/2] up min ruby version to 1.9.2 --- .travis.yml | 3 --- yaml_db.gemspec | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e1bac7..41f676d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ language: ruby rvm: - - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 @@ -16,8 +15,6 @@ env: matrix: fast_finish: true exclude: - - env: RAILS_VERSION='~> 4.0.0' - rvm: 1.8.7 - env: RAILS_VERSION='~> 4.0.0' rvm: 1.9.2 - env: RAILS_VERSION='~> 4.1.0' diff --git a/yaml_db.gemspec b/yaml_db.gemspec index 71c2561..a3388ad 100644 --- a/yaml_db.gemspec +++ b/yaml_db.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |s| s.files = Dir['README.md', 'lib/**/*'] s.require_paths = ["lib"] - s.required_ruby_version = ">= 1.8.7" + s.required_ruby_version = ">= 1.9.2" s.add_runtime_dependency "rails", ">= 3.0", "< 4.3" s.add_runtime_dependency "rake", ">= 0.8.7"