yaml-db-0.2.3/0000755000175000017500000000000012133002661012357 5ustar ondrejondrejyaml-db-0.2.3/spec/0000755000175000017500000000000012133002661013311 5ustar ondrejondrejyaml-db-0.2.3/spec/serialization_helper_dump_spec.rb0000644000175000017500000000505212133002661022113 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' describe SerializationHelper::Dump do before do silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object) ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a', :name => 'a', :type => :string), mock('b', :name => 'b', :type => :string) ]) ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"}) ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) SerializationHelper::Utils.stub!(:quote_table).with('mytable').and_return('mytable') end before(:each) do File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new) @io = StringIO.new end it "should return a list of column names" do SerializationHelper::Dump.table_column_names('mytable').should == [ 'a', 'b' ] end it "should return a list of tables without the rails schema table" do SerializationHelper::Dump.tables.should == ['mytable'] end it "should return the total number of records in a table" do SerializationHelper::Dump.table_record_count('mytable').should == 2 end it "should return all records from the database and return them when there is only 1 page" do SerializationHelper::Dump.each_table_page('mytable') do |records| records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ] end end it "should paginate records from the database and return them" do ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 } ], [ { 'a' => 3, 'b' => 4 } ]) records = [ ] SerializationHelper::Dump.each_table_page('mytable', 1) do |page| page.size.should == 1 records.concat(page) end records.should == [ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ] end it "should dump a table's contents to yaml" do SerializationHelper::Dump.should_receive(:dump_table_columns) SerializationHelper::Dump.should_receive(:dump_table_records) SerializationHelper::Dump.dump_table(@io, 'mytable') end it "should not dump a table's contents when the record count is zero" do SerializationHelper::Dump.stub!(:table_record_count).with('mytable').and_return(0) SerializationHelper::Dump.should_not_receive(:dump_table_columns) SerializationHelper::Dump.should_not_receive(:dump_table_records) SerializationHelper::Dump.dump_table(@io, 'mytable') end end yaml-db-0.2.3/spec/yaml_load_spec.rb0000644000175000017500000000217312133002661016614 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' require 'active_support/core_ext/kernel/debugger' describe YamlDb::Load do before do SerializationHelper::Utils.stub!(:quote_table).with('mytable').and_return('mytable') silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object) ActiveRecord::Base.connection.stub!(:transaction).and_yield end before(:each) do @io = StringIO.new end it "should call load structure for each document in the file" do YAML.should_receive(:load_documents).with(@io).and_yield({ 'mytable' => { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] } } ) YamlDb::Load.should_receive(:load_table).with('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] },true) YamlDb::Load.load(@io) end it "should not call load structure when the document in the file contains no records" do YAML.should_receive(:load_documents).with(@io).and_yield({ 'mytable' => nil }) YamlDb::Load.should_not_receive(:load_table) YamlDb::Load.load(@io) end end yaml-db-0.2.3/spec/serialization_helper_base_spec.rb0000644000175000017500000000347012133002661022062 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' describe SerializationHelper::Base do def prestub_active_record end before do @io = StringIO.new silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(mock('connection')) ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) end def stub_helper! @helper = mock("MyHelper") @dumper = mock("MyDumper"); @loader = mock("MyLoader"); @helper.stub!(:dumper).and_return(@dumper) @helper.stub!(:loader).and_return(@loader) @helper.stub!(:extension).and_return("yml") @dumper.stub!(:tables).and_return([ActiveRecord::Base.connection.tables[0]]) @dumper.stub!(:before_table).and_return(nil) @dumper.stub!(:after_table).and_return(nil) end context "for multi-file dumps" do before do File.should_receive(:new).once.with("dir_name/mytable.yml", "w").and_return(@io) Dir.should_receive(:mkdir).once.with("dir_name") stub_helper! @dumper.should_receive(:dump_table).once.with(@io, "mytable") end it "should create the number of files that there are tables" do SerializationHelper::Base.new(@helper).dump_to_dir "dir_name" end end context "for multi-file loads" do before do stub_helper! @loader.should_receive(:load).once.with(@io, true) File.should_receive(:new).once.with("dir_name/mytable.yml", "r").and_return(@io) Dir.stub!(:entries).and_return(["mytable.yml"]) end it "should insert into then umber of tables that there are files" do SerializationHelper::Base.new(@helper).load_from_dir "dir_name" end end end yaml-db-0.2.3/spec/serialization_helper_load_spec.rb0000644000175000017500000001034312133002661022064 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' describe SerializationHelper::Load do before do SerializationHelper::Utils.stub!(:quote_table).with('mytable').and_return('mytable') silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object) ActiveRecord::Base.connection.stub!(:transaction).and_yield @io = StringIO.new end it "should truncate the table" do ActiveRecord::Base.connection.stub!(:execute).with("TRUNCATE mytable").and_return(true) ActiveRecord::Base.connection.should_not_receive(:execute).with("DELETE FROM mytable") SerializationHelper::Load.truncate_table('mytable') end it "should delete the table if truncate throws an exception" do ActiveRecord::Base.connection.should_receive(:execute).with("TRUNCATE mytable").and_raise() ActiveRecord::Base.connection.should_receive(:execute).with("DELETE FROM mytable").and_return(true) SerializationHelper::Load.truncate_table('mytable') end it "should call reset pk sequence if the connection adapter is postgres" do ActiveRecord::Base.connection.should_receive(:respond_to?).with(:reset_pk_sequence!).and_return(true) ActiveRecord::Base.connection.should_receive(:reset_pk_sequence!).with('mytable') SerializationHelper::Load.reset_pk_sequence!('mytable') end it "should not call reset pk sequence for other adapters" do ActiveRecord::Base.connection.should_receive(:respond_to?).with(:reset_pk_sequence!).and_return(false) ActiveRecord::Base.connection.should_not_receive(:reset_pk_sequence!) SerializationHelper::Load.reset_pk_sequence!('mytable') end it "should insert records into a table" do mca = mock('a',:name => 'a') mcb = mock('b', :name => 'b') ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([mca , mcb ]) ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a') ActiveRecord::Base.connection.stub!(:quote_column_name).with('b').and_return('b') ActiveRecord::Base.connection.stub!(:quote).with(1, mca).and_return("'1'") ActiveRecord::Base.connection.stub!(:quote).with(2, mcb).and_return("'2'") ActiveRecord::Base.connection.stub!(:quote).with(3, mca).and_return("'3'") ActiveRecord::Base.connection.stub!(:quote).with(4, mcb).and_return("'4'") ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('1','2')") ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,b) VALUES ('3','4')") SerializationHelper::Load.load_records('mytable', ['a', 'b'], [[1, 2], [3, 4]]) end it "should quote column names that correspond to sql keywords" do mca = mock('a',:name => 'a') mccount = mock('count', :name => 'count') ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([mca , mccount ]) ActiveRecord::Base.connection.stub!(:quote_column_name).with('a').and_return('a') ActiveRecord::Base.connection.stub!(:quote_column_name).with('count').and_return('"count"') ActiveRecord::Base.connection.stub!(:quote).with(1, mca).and_return("'1'") ActiveRecord::Base.connection.stub!(:quote).with(2, mccount).and_return("'2'") ActiveRecord::Base.connection.stub!(:quote).with(3, mca).and_return("'3'") ActiveRecord::Base.connection.stub!(:quote).with(4, mccount).and_return("'4'") ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('1','2')") ActiveRecord::Base.connection.should_receive(:execute).with("INSERT INTO mytable (a,\"count\") VALUES ('3','4')") SerializationHelper::Load.load_records('mytable', ['a', 'count'], [[1, 2], [3, 4]]) end it "should truncate the table and then load the records into the table" do SerializationHelper::Load.should_receive(:truncate_table).with('mytable') SerializationHelper::Load.should_receive(:load_records).with('mytable', ['a', 'b'], [[1, 2], [3, 4]]) SerializationHelper::Load.should_receive(:reset_pk_sequence!).with('mytable') SerializationHelper::Load.load_table('mytable', { 'columns' => [ 'a', 'b' ], 'records' => [[1, 2], [3, 4]] }) end end yaml-db-0.2.3/spec/serialization_utils_spec.rb0000644000175000017500000000423512133002661020751 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' describe SerializationHelper::Utils, " convert records utility method" do before do silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object) end it "returns an array of hash values using an array of ordered keys" do SerializationHelper::Utils.unhash({ 'a' => 1, 'b' => 2 }, [ 'b', 'a' ]).should == [ 2, 1 ] end it "should unhash each hash an array using an array of ordered keys" do SerializationHelper::Utils.unhash_records([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ], [ 'b', 'a' ]).should == [ [ 2, 1 ], [ 4, 3 ] ] end it "should return true if it is a boolean type" do SerializationHelper::Utils.is_boolean(true).should == true SerializationHelper::Utils.is_boolean('true').should_not == true end it "should return an array of boolean columns" do ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a',:name => 'a',:type => :string), mock('b', :name => 'b',:type => :boolean) ]) SerializationHelper::Utils.boolean_columns('mytable').should == ['b'] end it "should quote the table name" do ActiveRecord::Base.connection.should_receive(:quote_table_name).with('values').and_return('`values`') SerializationHelper::Utils.quote_table('values').should == '`values`' end it "should convert ruby booleans to true and false" do SerializationHelper::Utils.convert_boolean(true).should == true SerializationHelper::Utils.convert_boolean(false).should == false end it "should convert ruby string t and f to true and false" do SerializationHelper::Utils.convert_boolean('t').should == true SerializationHelper::Utils.convert_boolean('f').should == false end it "should convert ruby string 1 and 0 to true and false" do SerializationHelper::Utils.convert_boolean('1').should == true SerializationHelper::Utils.convert_boolean('0').should == false end it "should convert ruby integer 1 and 0 to true and false" do SerializationHelper::Utils.convert_boolean(1).should == true SerializationHelper::Utils.convert_boolean(0).should == false end end yaml-db-0.2.3/spec/yaml_dump_spec.rb0000644000175000017500000000310612133002661016637 0ustar ondrejondrejrequire File.dirname(__FILE__) + '/base' describe YamlDb::Dump do before do silence_warnings { ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true) } ActiveRecord::Base.stub(:connection).and_return(stub('connection').as_null_object) ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ]) ActiveRecord::Base.connection.stub!(:columns).with('mytable').and_return([ mock('a',:name => 'a', :type => :string), mock('b', :name => 'b', :type => :string) ]) ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"}) ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ]) YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable') end before(:each) do File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new) @io = StringIO.new end it "should return a formatted string" do YamlDb::Dump.table_record_header(@io) @io.rewind @io.read.should == " records: \n" end it "should return a yaml string that contains a table header and column names" do YamlDb::Dump.stub!(:table_column_names).with('mytable').and_return([ 'a', 'b' ]) YamlDb::Dump.dump_table_columns(@io, 'mytable') @io.rewind @io.read.should == <= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Adam Wiggins", "Orion Henry"] s.date = "2012-04-30" s.description = "\nYamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml.\nThis can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design.\nAny database that has an ActiveRecord adapter should work\n" s.email = "nate@ludicast.com" s.extra_rdoc_files = [ "README.markdown" ] s.files = [ ".travis.yml", "README.markdown", "Rakefile", "VERSION", "about.yml", "init.rb", "lib/csv_db.rb", "lib/serialization_helper.rb", "lib/tasks/yaml_db_tasks.rake", "lib/yaml_db.rb", "spec/base.rb", "spec/serialization_helper_base_spec.rb", "spec/serialization_helper_dump_spec.rb", "spec/serialization_helper_load_spec.rb", "spec/serialization_utils_spec.rb", "spec/yaml_dump_spec.rb", "spec/yaml_load_spec.rb", "spec/yaml_utils_spec.rb", "yaml_db.gemspec" ] s.homepage = "http://github.com/ludicast/yaml_db" s.require_paths = ["lib"] s.rubygems_version = "1.8.17" s.summary = "yaml_db allows export/import of database into/from yaml files" if s.respond_to? :specification_version then s.specification_version = 3 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then else end else end end yaml-db-0.2.3/Rakefile0000644000175000017500000000216712133002661014032 0ustar ondrejondrejrequire 'rake' require "rspec/core/rake_task" begin require 'jeweler' Jeweler::Tasks.new do |gem| gem.name = "yaml_db" gem.summary = %Q{yaml_db allows export/import of database into/from yaml files} gem.description = %Q{ YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml. This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design. Any database that has an ActiveRecord adapter should work } gem.email = "nate@ludicast.com" gem.homepage = "http://github.com/ludicast/yaml_db" gem.authors = ["Adam Wiggins","Orion Henry"] end Jeweler::GemcutterTasks.new rescue LoadError puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler" end RSpec::Core::RakeTask.new(:spec) do |spec| spec.pattern = 'spec/*_spec.rb' spec.rspec_opts = ['--backtrace'] end task :default => :spec yaml-db-0.2.3/VERSION0000644000175000017500000000000612133002661013423 0ustar ondrejondrej0.2.3 yaml-db-0.2.3/about.yml0000644000175000017500000000031112133002661014207 0ustar ondrejondrejauthor: Orion Henry and Adam Wiggins of Heroku summary: Dumps and loads a database-independent data dump format in db/data.yml. homepage: http://opensource.heroku.com/ license: MIT rails_version: 1.2+ yaml-db-0.2.3/lib/0000755000175000017500000000000012133002661013125 5ustar ondrejondrejyaml-db-0.2.3/lib/yaml_db.rb0000644000175000017500000000305412133002661015063 0ustar ondrejondrejrequire 'rubygems' require 'yaml' require 'active_record' require 'serialization_helper' require 'active_support/core_ext/kernel/reporting' require 'rails/railtie' module YamlDb module Helper def self.loader YamlDb::Load end def self.dumper YamlDb::Dump end def self.extension "yml" end end module Utils def self.chunk_records(records) yaml = [ records ].to_yaml yaml.sub!(/---\s\n|---\n/, '') yaml.sub!('- - -', ' - -') yaml end end class Dump < SerializationHelper::Dump def self.dump_table_columns(io, table) io.write("\n") io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml) end def self.dump_table_records(io, table) table_record_header(io) column_names = table_column_names(table) each_table_page(table) do |records| rows = SerializationHelper::Utils.unhash_records(records, column_names) io.write(YamlDb::Utils.chunk_records(records)) end end def self.table_record_header(io) io.write(" records: \n") end end class Load < SerializationHelper::Load def self.load_documents(io, truncate = true) YAML.load_documents(io) do |ydoc| ydoc.keys.each do |table_name| next if ydoc[table_name].nil? load_table(table_name, ydoc[table_name], truncate) end end end end class Railtie < Rails::Railtie rake_tasks do load File.expand_path('../tasks/yaml_db_tasks.rake', __FILE__) end end end yaml-db-0.2.3/lib/serialization_helper.rb0000644000175000017500000001257712133002661017702 0ustar ondrejondrejmodule SerializationHelper class Base attr_reader :extension def initialize(helper) @dumper = helper.dumper @loader = helper.loader @extension = helper.extension end def dump(filename) disable_logger @dumper.dump(File.new(filename, "w")) reenable_logger end def dump_to_dir(dirname) Dir.mkdir(dirname) tables = @dumper.tables tables.each do |table| io = File.new "#{dirname}/#{table}.#{@extension}", "w" @dumper.before_table(io, table) @dumper.dump_table io, table @dumper.after_table(io, table) end end def load(filename, truncate = true) disable_logger @loader.load(File.new(filename, "r"), truncate) reenable_logger end def load_from_dir(dirname, truncate = true) Dir.entries(dirname).each do |filename| if filename =~ /^[.]/ next end @loader.load(File.new("#{dirname}/#{filename}", "r"), truncate) end end def disable_logger @@old_logger = ActiveRecord::Base.logger ActiveRecord::Base.logger = nil end def reenable_logger ActiveRecord::Base.logger = @@old_logger end end class Load def self.load(io, truncate = true) ActiveRecord::Base.connection.transaction do load_documents(io, truncate) end end def self.truncate_table(table) begin ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}") rescue Exception ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}") end end def self.load_table(table, data, truncate = true) column_names = data['columns'] if truncate truncate_table(table) end load_records(table, column_names, data['records']) reset_pk_sequence!(table) end def self.load_records(table, column_names, records) if column_names.nil? return end columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}} quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',') quoted_table_name = SerializationHelper::Utils.quote_table(table) records.each do |record| quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',') ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})") end end def self.reset_pk_sequence!(table_name) if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!) ActiveRecord::Base.connection.reset_pk_sequence!(table_name) end end end module Utils def self.unhash(hash, keys) keys.map { |key| hash[key] } end def self.unhash_records(records, keys) records.each_with_index do |record, index| records[index] = unhash(record, keys) end records end def self.convert_booleans(records, columns) records.each do |record| columns.each do |column| next if is_boolean(record[column]) record[column] = convert_boolean(record[column]) end end records end def self.convert_boolean(value) ['t', '1', true, 1].include?(value) end def self.boolean_columns(table) columns = ActiveRecord::Base.connection.columns(table).reject { |c| silence_warnings { c.type != :boolean } } columns.map { |c| c.name } end def self.is_boolean(value) value.kind_of?(TrueClass) or value.kind_of?(FalseClass) end def self.quote_table(table) ActiveRecord::Base.connection.quote_table_name(table) end end class Dump def self.before_table(io, table) end def self.dump(io) tables.each do |table| before_table(io, table) dump_table(io, table) after_table(io, table) end end def self.after_table(io, table) end def self.tables ActiveRecord::Base.connection.tables.reject { |table| ['schema_info', 'schema_migrations'].include?(table) } end def self.dump_table(io, table) return if table_record_count(table).zero? dump_table_columns(io, table) dump_table_records(io, table) end def self.table_column_names(table) ActiveRecord::Base.connection.columns(table).map { |c| c.name } end def self.each_table_page(table, records_per_page=1000) total_count = table_record_count(table) pages = (total_count.to_f / records_per_page).ceil - 1 id = table_column_names(table).first boolean_columns = SerializationHelper::Utils.boolean_columns(table) quoted_table_name = SerializationHelper::Utils.quote_table(table) (0..pages).to_a.each do |page| query = Arel::Table.new(table).order(id).skip(records_per_page*page).take(records_per_page).project(Arel.sql('*')) records = ActiveRecord::Base.connection.select_all(query) records = SerializationHelper::Utils.convert_booleans(records, boolean_columns) yield records end end def self.table_record_count(table) ActiveRecord::Base.connection.select_one("SELECT COUNT(*) FROM #{SerializationHelper::Utils.quote_table(table)}").values.first.to_i end end end yaml-db-0.2.3/lib/csv_db.rb0000644000175000017500000000326012133002661014713 0ustar ondrejondrej#require 'FasterCSV' module CsvDb module Helper def self.loader Load end def self.dumper Dump end def self.extension "csv" end end class Load < SerializationHelper::Load def self.load_documents(io, truncate = true) tables = {} curr_table = nil io.each do |line| if /BEGIN_CSV_TABLE_DECLARATION(.+)END_CSV_TABLE_DECLARATION/ =~ line curr_table = $1 tables[curr_table] = {} else if tables[curr_table]["columns"] tables[curr_table]["records"] << FasterCSV.parse(line)[0] else tables[curr_table]["columns"] = FasterCSV.parse(line)[0] tables[curr_table]["records"] = [] end end end tables.each_pair do |table_name, contents| load_table(table_name, contents, truncate) end end end class Dump < SerializationHelper::Dump def self.before_table(io,table) io.write "BEGIN_CSV_TABLE_DECLARATION#{table}END_CSV_TABLE_DECLARATION\n" end def self.dump(io) tables.each do |table| before_table(io, table) dump_table(io, table) after_table(io, table) end end def self.after_table(io,table) io.write "" end def self.dump_table_columns(io, table) io.write(table_column_names(table).to_csv) end def self.dump_table_records(io, table) column_names = table_column_names(table) each_table_page(table) do |records| rows = SerializationHelper::Utils.unhash_records(records, column_names) records.each do |record| io.write(record.to_csv) end end end end endyaml-db-0.2.3/lib/tasks/0000755000175000017500000000000012133002661014252 5ustar ondrejondrejyaml-db-0.2.3/lib/tasks/yaml_db_tasks.rake0000644000175000017500000000321312133002661017731 0ustar ondrejondrejnamespace :db do desc "Dump schema and data to db/schema.rb and db/data.yml" task(:dump => [ "db:schema:dump", "db:data:dump" ]) desc "Load schema and data from db/schema.rb and db/data.yml" task(:load => [ "db:schema:load", "db:data:load" ]) namespace :data do def db_dump_data_file (extension = "yml") "#{dump_dir}/data.#{extension}" end def dump_dir(dir = "") "#{Rails.root}/db#{dir}" end desc "Dump contents of database to db/data.extension (defaults to yaml)" task :dump => :environment do format_class = ENV['class'] || "YamlDb::Helper" helper = format_class.constantize SerializationHelper::Base.new(helper).dump db_dump_data_file helper.extension end desc "Dump contents of database to curr_dir_name/tablename.extension (defaults to yaml)" task :dump_dir => :environment do 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 format_class = ENV['class'] || "YamlDb::Helper" helper = format_class.constantize SerializationHelper::Base.new(helper).load(db_dump_data_file helper.extension) end desc "Load contents of db/data_dir into database" task :load_dir => :environment do dir = ENV['dir'] || "base" format_class = ENV['class'] || "YamlDb::Helper" SerializationHelper::Base.new(format_class.constantize).load_from_dir dump_dir("/#{dir}") end end end yaml-db-0.2.3/README.markdown0000644000175000017500000000474612133002661015073 0ustar ondrejondrej# YamlDb YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml. This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design. Any database that has an ActiveRecord adapter should work. This gem is now Rails 3 only. For Rails 2, clone and checkout the Rails2 branch. [![Build Status](https://secure.travis-ci.org/ludicast/yaml_db.png)](http://travis-ci.org/ludicast/yaml_db) ## Installation Simply add to your Gemfile: gem 'yaml_db' All rake tasks will then be available to you. ## Usage rake db:data:dump -> Dump contents of Rails database to db/data.yml rake db:data:load -> Load contents of db/data.yml into the database Further, there are tasks db:dump and db:load which do the entire database (the equivalent of running db:schema:dump followed by db:data:load). Also, there are other tasks recently added that allow the export of the database contents to/from multiple files (each one named after the table being dumped or loaded). 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 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 One common use would be to switch your data from one database backend to another. For example, let's say you wanted to switch from SQLite to MySQL. You might execute the following steps: 1. rake db:dump 2. Edit config/database.yml and change your adapter to mysql, set up database params 3. mysqladmin create [database name] 4. rake db:load ## Credits Created by Orion Henry and Adam Wiggins. Major updates by Ricardo Chimal, Jr. Patches contributed by Michael Irwin, Tom Locke, and Tim Galeckas. Send questions, feedback, or patches to the Heroku mailing list: http://groups.google.com/group/heroku yaml-db-0.2.3/metadata.yml0000644000175000017500000000357412133002661014673 0ustar ondrejondrej--- !ruby/object:Gem::Specification name: yaml_db version: !ruby/object:Gem::Version version: 0.2.3 prerelease: platform: ruby authors: - Adam Wiggins - Orion Henry autorequire: bindir: bin cert_chain: [] date: 2012-04-30 00:00:00.000000000 Z dependencies: [] description: ! ' YamlDb is a database-independent format for dumping and restoring data. It complements the the database-independent schema format found in db/schema.rb. The data is saved into db/data.yml. This can be used as a replacement for mysqldump or pg_dump, but only for the databases typically used by Rails apps. Users, permissions, schemas, triggers, and other advanced database features are not supported - by design. Any database that has an ActiveRecord adapter should work ' email: nate@ludicast.com executables: [] extensions: [] extra_rdoc_files: - README.markdown files: - .travis.yml - README.markdown - Rakefile - VERSION - about.yml - init.rb - lib/csv_db.rb - lib/serialization_helper.rb - lib/tasks/yaml_db_tasks.rake - lib/yaml_db.rb - spec/base.rb - spec/serialization_helper_base_spec.rb - spec/serialization_helper_dump_spec.rb - spec/serialization_helper_load_spec.rb - spec/serialization_utils_spec.rb - spec/yaml_dump_spec.rb - spec/yaml_load_spec.rb - spec/yaml_utils_spec.rb - yaml_db.gemspec homepage: http://github.com/ludicast/yaml_db licenses: [] post_install_message: rdoc_options: [] require_paths: - lib required_ruby_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' required_rubygems_version: !ruby/object:Gem::Requirement none: false requirements: - - ! '>=' - !ruby/object:Gem::Version version: '0' requirements: [] rubyforge_project: rubygems_version: 1.8.17 signing_key: specification_version: 3 summary: yaml_db allows export/import of database into/from yaml files test_files: [] yaml-db-0.2.3/init.rb0000644000175000017500000000002212133002661013641 0ustar ondrejondrejrequire 'yaml_db'