Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for single table dump/load with a parameter on the rake ta... #59

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ 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 or load 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
rake db:data:load["table_name"] -> Load contents of db/data.yml into the database table "table_name"

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
Expand Down
18 changes: 12 additions & 6 deletions lib/serialization_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,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

Expand All @@ -26,9 +26,9 @@ def dump_to_dir(dirname)
end
end

def load(filename, truncate = true)
def load(filename, truncate = true, table = nil)
disable_logger
@loader.load(File.new(filename, "r"), truncate)
@loader.load(File.new(filename, "r"), truncate, table)
reenable_logger
end

Expand All @@ -52,7 +52,7 @@ def reenable_logger
end

class Load
def self.load(io, truncate = true)
def self.load(io, truncate = true, table = nil)
ActiveRecord::Base.connection.transaction do
load_documents(io, truncate)
end
Expand Down Expand Up @@ -145,7 +145,13 @@ def self.before_table(io, table)

end

def self.dump(io)
def self.dump(io, table=nil)
if table
before_table(io, table)
dump_table(io, table)
after_table(io, table)
return
end
tables.each do |table|
before_table(io, table)
dump_table(io, table)
Expand Down
12 changes: 6 additions & 6 deletions lib/tasks/yaml_db_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,28 @@ namespace :db do
end

desc "Dump contents of database to db/data.extension (defaults to yaml)"
task :dump => :environment do
task :dump, [:table] => :environment do |t, args|
format_class = ENV['class'] || "YamlDb::Helper"
helper = format_class.constantize
SerializationHelper::Base.new(helper).dump db_dump_data_file helper.extension
SerializationHelper::Base.new(helper).dump(db_dump_data_file, args[:table])
end

desc "Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)"
task :dump_dir => :environment do
task :dump_dir, [:table] => :environment do |t, args|
format_class = ENV['class'] || "YamlDb::Helper"
dir = ENV['dir'] || "#{Time.now.to_s.gsub(/ /, '_')}"
SerializationHelper::Base.new(format_class.constantize).dump_to_dir dump_dir("/#{dir}")
end

desc "Load contents of db/data.extension (defaults to yaml) into database"
task :load => :environment do
task :load, [:table] => :environment do |t, args|
format_class = ENV['class'] || "YamlDb::Helper"
helper = format_class.constantize
SerializationHelper::Base.new(helper).load(db_dump_data_file helper.extension)
SerializationHelper::Base.new(helper).load(db_dump_data_file, helper.extension, args[:table])
end

desc "Load contents of db/data_dir into database"
task :load_dir => :environment do
task :load_dir, [:table] => :environment do |t, args|
dir = ENV['dir'] || "base"
format_class = ENV['class'] || "YamlDb::Helper"
SerializationHelper::Base.new(format_class.constantize).load_from_dir dump_dir("/#{dir}")
Expand Down
8 changes: 7 additions & 1 deletion lib/yaml_db.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ def self.table_record_header(io)
end

class Load < SerializationHelper::Load
def self.load_documents(io, truncate = true)
def self.load_documents(io, truncate = true, table)
YAML.load_documents(io) do |ydoc|
ydoc.keys.each do |table_name|
next if ydoc[table_name].nil?
if table
if table==table_name
load_table(table_name, ydoc[table_name], truncate)
return
end
end
load_table(table_name, ydoc[table_name], truncate)
end
end
Expand Down