Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
moved serializer into separate file
  • Loading branch information
ludicast committed Jul 23, 2009
1 parent f4d08cc commit 505d8b7
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 75 deletions.
24 changes: 24 additions & 0 deletions lib/csv_db.rb
@@ -0,0 +1,24 @@
module CsvDb
module Helper
def self.loader
Load
end

def self.dumper
Dump
end

def self.extension
"csv"
end
end

module Load

end

module Dump
end


end
83 changes: 56 additions & 27 deletions lib/serialization_helper.rb
@@ -1,39 +1,68 @@
class SerializationHelper
attr_reader :extension
attr_reader :extension

def initialize(helper)
@dumper = helper.dumper
@loader = helper.loader
@extension = helper.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|
file = File.new "#{dirname}/#{table}.#{@extension}", "w"
@dumper.dump_table file, table
end
end

def load(filename)
disable_logger
@loader.load(File.new(filename, "r"))
reenable_logger
end

def dump(filename)
disable_logger
@dumper.dump(File.new(filename, "w"))
reenable_logger
end
def disable_logger
@@old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
end

def dump_to_dir(dirname)
Dir.mkdir(dirname)
tables = @dumper.tables
def reenable_logger
ActiveRecord::Base.logger = @@old_logger
end

class Load

end

class Dump
def self.dump(io)
tables.each do |table|
file = File.new "#{dirname}/#{table}.#{@extension}", "w"
@dumper.dump_table file, table
dump_table(io, table)
end
end

def load(filename)
disable_logger
@loader.load(File.new(filename, "r"))
reenable_logger
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 disable_logger
@@old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil
end
end

def reenable_logger
ActiveRecord::Base.logger = @@old_logger
end
end
29 changes: 5 additions & 24 deletions lib/yaml_db.rb
@@ -1,9 +1,10 @@
require 'rubygems'
require 'yaml'
require 'active_record'
require 'serialization_helper'

module YamlDb
module SerializationHelper
module Helper
def self.loader
YamlDb::Load
end
Expand Down Expand Up @@ -62,24 +63,8 @@ def self.quote_table(table)
end
end

module Dump
def self.dump(io)
tables.each do |table|
dump_table(io, table)
end
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

class Dump < SerializationHelper::Dump

def self.dump_table_columns(io, table)
io.write("\n")
io.write({ table => { 'columns' => table_column_names(table) } }.to_yaml)
Expand All @@ -100,10 +85,6 @@ def self.table_record_header(io)
io.write(" records: \n")
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
Expand All @@ -126,7 +107,7 @@ def self.table_record_count(table)
end
end

module Load
class Load < SerializationHelper::Load
def self.load(io)
ActiveRecord::Base.connection.transaction do
YAML.load_documents(io) do |ydoc|
Expand Down
42 changes: 42 additions & 0 deletions spec/serialization_helper_spec.rb
@@ -0,0 +1,42 @@
require File.dirname(__FILE__) + '/base'

describe SerializationHelper do

before do
ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
ActiveRecord::Base.connection = mock('connection')
ActiveRecord::Base.connection.stub!(:tables).and_return([ 'mytable', 'schema_info', 'schema_migrations' ])
end

def stub_helper!
@helper = mock("MyHelper")
@helper.stub!(:dumper).and_return(@dumper)
@helper.stub!(:loader)
@helper.stub!(:extension).and_return("yml")
@dumper.stub!(:tables).and_return([ActiveRecord::Base.connection.tables[0]])
end

context "for multi-file dumps" do
before do
@io = StringIO.new
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.new(@helper).dump_to_dir "dir_name"
end

end

context "for multi-file loads" do
before do

end


end

end
21 changes: 0 additions & 21 deletions spec/yaml_dump_spec.rb
Expand Up @@ -12,9 +12,6 @@
YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
end

context "for single file yaml dumps" do


before(:each) do
File.stub!(:new).with('dump.yml', 'w').and_return(StringIO.new)
@io = StringIO.new
Expand Down Expand Up @@ -95,24 +92,6 @@
YamlDb::Dump.dump_table(@io, 'mytable')
end

end

context "for multi-file yaml dumps" do
before do
@io = StringIO.new
File.should_receive(:new).once.with("dir_name/mytable.yml", "w").and_return(@io)
Dir.should_receive(:mkdir).once.with("dir_name")
YamlDb::Dump.should_receive(:dump_table).once.with(@io, "mytable")
end

it "should create the number of files that there are tables" do
SerializationHelper.new(YamlDb::SerializationHelper).dump_to_dir "dir_name"
end

end





end
6 changes: 3 additions & 3 deletions tasks/yaml_db_tasks.rake
Expand Up @@ -16,18 +16,18 @@ namespace :db do

desc "Dump contents of database to db/data.yml"
task(:dump => :environment) do
SerializationHelper.new(YamlDb::SerializationHelper).dump db_dump_data_file
SerializationHelper.new(YamlDb::Helper).dump db_dump_data_file
end

desc "Dump contents of database to curr_dir_name/data.ym"
task(:dump_dir => :environment) do
time_dir = dump_dir "/#{Time.now.to_s.gsub(/ /, '_')}"
SerializationHelper.new(YamlDb::SerializationHelper).dump_to_dir time_dir
SerializationHelper.new(YamlDb::Helper).dump_to_dir time_dir
end

desc "Load contents of db/data.yml into database"
task(:load => :environment) do
SerializationHelper.new(YamlDb::SerializationHelper).load db_dump_data_file
SerializationHelper.new(YamlDb::Helper).load db_dump_data_file
end
end
end

0 comments on commit 505d8b7

Please sign in to comment.