Permalink
Browse files

adds compression to file saving and db restore

  • Loading branch information...
1 parent d3705b9 commit 3d8d68cd1e83a883cd912c77a575c165358c7b83 @lazylester committed Mar 21, 2013
Showing with 90 additions and 2 deletions.
  1. +36 −0 app/models/application_database.rb
  2. +53 −2 spec/models/application_database_spec.rb
  3. +1 −0 spec/spec_helper.rb
View
36 app/models/application_database.rb
@@ -16,13 +16,24 @@ def self.save_to_file(file)
system(sql_dump_to_file(file))
end
+ def self.zip_and_save_to_file(file)
+ new
+ system(sql_dump_to_zipfile(file))
+ end
+
# argument is an instance of BackupFile
def self.restore_from_file(backfile)
new
system(sql_restore(backfile.filename))
$?.exitstatus.zero?
end
+ def self.restore_from_zipfile(backfile)
+ new
+ system(sql_restore_from_zipfile(backfile.filename))
+ $?.exitstatus.zero?
+ end
+
private
def self.sql_dump_to_file(file)
sql_cmd =<<-SQL
@@ -39,6 +50,21 @@ def self.sql_dump_to_file(file)
SQL
end
+ def self.sql_dump_to_zipfile(file)
+ sql_cmd =<<-SQL
+ #{@@db_config['path']}mysqldump\
+ --user=#{@@db_config['username']}\
+ --password=#{@@password}\
+ --single-transaction\
+ --quote-names\
+ --add-drop-table\
+ --add-locks=FALSE\
+ --lock-tables=FALSE\
+ --hex-blob\
+ #{@@db_config['database']} | gzip -c > #{file}
+ SQL
+ end
+
def self.sql_restore(filename)
sql_cmd =<<-SQL
#{@@db_config['path']}mysql\
@@ -50,4 +76,14 @@ def self.sql_restore(filename)
SQL
end
+ def self.sql_restore_from_zipfile(filename)
+ sql_cmd =<<-SQL
+ gunzip < #{filename} | #{@@db_config['path']}mysql\
+ --database #{@@db_config['database']}\
+ --host=#{@@db_config['host']}\
+ --user=#{@@db_config['username']}\
+ --password=#{@@password};
+ SQL
+ end
+
end
View
55 spec/models/application_database_spec.rb
@@ -19,16 +19,42 @@
ActiveRecord::Base.connection.create_table :test do |t|
t.column :foo, :string
end
+ ApplicationDatabase.save_to_file( Rails.root.join('tmp','sql_test.sql') )
end
it "file should contain database contents" do
- ApplicationDatabase.extract_contents.should match "Table structure for table `test`"
+ File.read( Rails.root.join('tmp','sql_test.sql')).should match "Table structure for table `test`"
+ end
+
+ after do
+ File.delete(Rails.root.join('tmp','sql_test.sql'))
+ end
+end
+
+describe ".zip_and_save_to_file" do
+ before do
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test`;")
+ ActiveRecord::Base.connection.create_table :test do |t|
+ t.column :foo, :string
+ end
+ ApplicationDatabase.zip_and_save_to_file( Rails.root.join('tmp','sql_test.sql.gz') )
+ end
+
+ it "file should contain database contents" do
+ system("gunzip #{ Rails.root.join('tmp','sql_test.sql.gz')}")
+ File.read( Rails.root.join('tmp','sql_test.sql')).should match "Table structure for table `test`"
+ end
+
+ after do
+ ['sql_test.sql','sql_test.sql.gz'].each do |filename|
+ file = Rails.root.join('tmp',filename)
+ File.delete(file) if File.exists? file
+ end
end
end
describe ".restore_from_file" do
before do
- BACKUP_DIR = Rails.root.join('tmp')
sql =<<-SQL
drop table if exists `test`;
create table test ( foo varchar(255));
@@ -47,3 +73,28 @@
File.delete(Rails.root.join('tmp','sql_test.sql'))
end
end
+
+describe ".restore_from_zipfile" do
+ before do
+ sql =<<-SQL
+ drop table if exists `test`;
+ create table test ( foo varchar(255));
+ insert into test set foo = 'bar';
+ SQL
+ file = Rails.root.join('tmp','sql_test.sql')
+ File.write(file, sql)
+ system("gzip #{file}")
+ ApplicationDatabase.restore_from_zipfile(BackupFile.new(:filename => file.to_s + ".gz"))
+ end
+
+ it "should restore the database contents from file" do
+ ActiveRecord::Base.connection.execute("select * from test").first[0].should == 'bar'
+ end
+
+ after do
+ ['sql_test.sql','sql_test.sql.gz'].each do |filename|
+ file = Rails.root.join('tmp',filename)
+ File.delete(file) if File.exists? file
+ end
+ end
+end
View
1 spec/spec_helper.rb
@@ -6,6 +6,7 @@
require 'ruby-debug'
ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
+BackupFile::BACKUP_DIR = Rails.root.join('tmp')
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.

0 comments on commit 3d8d68c

Please sign in to comment.