Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adds compression to file saving and db restore

  • Loading branch information...
commit 3d8d68cd1e83a883cd912c77a575c165358c7b83 1 parent d3705b9
lazylester authored
36 app/models/application_database.rb
@@ -16,6 +16,11 @@ def self.save_to_file(file)
16 16 system(sql_dump_to_file(file))
17 17 end
18 18
  19 + def self.zip_and_save_to_file(file)
  20 + new
  21 + system(sql_dump_to_zipfile(file))
  22 + end
  23 +
19 24 # argument is an instance of BackupFile
20 25 def self.restore_from_file(backfile)
21 26 new
@@ -23,6 +28,12 @@ def self.restore_from_file(backfile)
23 28 $?.exitstatus.zero?
24 29 end
25 30
  31 + def self.restore_from_zipfile(backfile)
  32 + new
  33 + system(sql_restore_from_zipfile(backfile.filename))
  34 + $?.exitstatus.zero?
  35 + end
  36 +
26 37 private
27 38 def self.sql_dump_to_file(file)
28 39 sql_cmd =<<-SQL
@@ -39,6 +50,21 @@ def self.sql_dump_to_file(file)
39 50 SQL
40 51 end
41 52
  53 + def self.sql_dump_to_zipfile(file)
  54 + sql_cmd =<<-SQL
  55 + #{@@db_config['path']}mysqldump\
  56 + --user=#{@@db_config['username']}\
  57 + --password=#{@@password}\
  58 + --single-transaction\
  59 + --quote-names\
  60 + --add-drop-table\
  61 + --add-locks=FALSE\
  62 + --lock-tables=FALSE\
  63 + --hex-blob\
  64 + #{@@db_config['database']} | gzip -c > #{file}
  65 + SQL
  66 + end
  67 +
42 68 def self.sql_restore(filename)
43 69 sql_cmd =<<-SQL
44 70 #{@@db_config['path']}mysql\
@@ -50,4 +76,14 @@ def self.sql_restore(filename)
50 76 SQL
51 77 end
52 78
  79 + def self.sql_restore_from_zipfile(filename)
  80 + sql_cmd =<<-SQL
  81 + gunzip < #{filename} | #{@@db_config['path']}mysql\
  82 + --database #{@@db_config['database']}\
  83 + --host=#{@@db_config['host']}\
  84 + --user=#{@@db_config['username']}\
  85 + --password=#{@@password};
  86 + SQL
  87 + end
  88 +
53 89 end
55 spec/models/application_database_spec.rb
@@ -19,16 +19,42 @@
19 19 ActiveRecord::Base.connection.create_table :test do |t|
20 20 t.column :foo, :string
21 21 end
  22 + ApplicationDatabase.save_to_file( Rails.root.join('tmp','sql_test.sql') )
22 23 end
23 24
24 25 it "file should contain database contents" do
25   - ApplicationDatabase.extract_contents.should match "Table structure for table `test`"
  26 + File.read( Rails.root.join('tmp','sql_test.sql')).should match "Table structure for table `test`"
  27 + end
  28 +
  29 + after do
  30 + File.delete(Rails.root.join('tmp','sql_test.sql'))
  31 + end
  32 +end
  33 +
  34 +describe ".zip_and_save_to_file" do
  35 + before do
  36 + ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test`;")
  37 + ActiveRecord::Base.connection.create_table :test do |t|
  38 + t.column :foo, :string
  39 + end
  40 + ApplicationDatabase.zip_and_save_to_file( Rails.root.join('tmp','sql_test.sql.gz') )
  41 + end
  42 +
  43 + it "file should contain database contents" do
  44 + system("gunzip #{ Rails.root.join('tmp','sql_test.sql.gz')}")
  45 + File.read( Rails.root.join('tmp','sql_test.sql')).should match "Table structure for table `test`"
  46 + end
  47 +
  48 + after do
  49 + ['sql_test.sql','sql_test.sql.gz'].each do |filename|
  50 + file = Rails.root.join('tmp',filename)
  51 + File.delete(file) if File.exists? file
  52 + end
26 53 end
27 54 end
28 55
29 56 describe ".restore_from_file" do
30 57 before do
31   - BACKUP_DIR = Rails.root.join('tmp')
32 58 sql =<<-SQL
33 59 drop table if exists `test`;
34 60 create table test ( foo varchar(255));
@@ -47,3 +73,28 @@
47 73 File.delete(Rails.root.join('tmp','sql_test.sql'))
48 74 end
49 75 end
  76 +
  77 +describe ".restore_from_zipfile" do
  78 + before do
  79 + sql =<<-SQL
  80 + drop table if exists `test`;
  81 + create table test ( foo varchar(255));
  82 + insert into test set foo = 'bar';
  83 + SQL
  84 + file = Rails.root.join('tmp','sql_test.sql')
  85 + File.write(file, sql)
  86 + system("gzip #{file}")
  87 + ApplicationDatabase.restore_from_zipfile(BackupFile.new(:filename => file.to_s + ".gz"))
  88 + end
  89 +
  90 + it "should restore the database contents from file" do
  91 + ActiveRecord::Base.connection.execute("select * from test").first[0].should == 'bar'
  92 + end
  93 +
  94 + after do
  95 + ['sql_test.sql','sql_test.sql.gz'].each do |filename|
  96 + file = Rails.root.join('tmp',filename)
  97 + File.delete(file) if File.exists? file
  98 + end
  99 + end
  100 +end
1  spec/spec_helper.rb
@@ -6,6 +6,7 @@
6 6 require 'ruby-debug'
7 7
8 8 ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
  9 +BackupFile::BACKUP_DIR = Rails.root.join('tmp')
9 10
10 11 # Requires supporting ruby files with custom matchers and macros, etc,
11 12 # in spec/support/ and its subdirectories.

0 comments on commit 3d8d68c

Please sign in to comment.
Something went wrong with that request. Please try again.