Browse files

with rspec specs for ApplicationDatabase model

  • Loading branch information...
1 parent 5e2b509 commit 758781fef3cdf6a3e80d743747cf65a578c89bd5 @lazylester committed Mar 19, 2013
Showing with 66 additions and 20 deletions.
  1. +10 −8 README.rdoc
  2. +29 −10 app/models/application_database.rb
  3. +27 −2 spec/models/application_database_spec.rb
View
18 README.rdoc
@@ -10,13 +10,15 @@ gem 'get_back'
Mount the get_back routes in your config.routes.rb with:
mount GetBack::Engine => '/get_back', :as => 'get_back', and the following routes are added
- backups_backups GET /backups/backups(.:format) backups/backups#index
- POST /backups/backups(.:format) backups/backups#create
- new_backups_backup GET /backups/backups/new(.:format) backups/backups#new
-edit_backups_backup GET /backups/backups/:id/edit(.:format) backups/backups#edit
- backups_backup GET /backups/backups/:id(.:format) backups/backups#show
- PUT /backups/backups/:id(.:format) backups/backups#update
- DELETE /backups/backups/:id(.:format) backups/backups#destroy
+ backup_restore POST /backups/:backup_id/restore(.:format) get_back/backups#restore
+ backups GET /backups(.:format) get_back/backups#index
+ POST /backups(.:format) get_back/backups#create
+ new_backup GET /backups/new(.:format) get_back/backups#new
+ edit_backup GET /backups/:id/edit(.:format) get_back/backups#edit
+ backup GET /backups/:id(.:format) get_back/backups#show
+ PUT /backups/:id(.:format) get_back/backups#update
+ DELETE /backups/:id(.:format) get_back/backups#destroy
-An admin page is provided at the url: '/get_back/index'
+
+An admin page is provided at the url: '/get_back/backups'
View
39 app/models/application_database.rb
@@ -12,21 +12,40 @@ def self.extract_contents
end
def self.save_to_file(file)
- # TODO instead of creating a copy of the db in memory and writing to the file,
- # should try command output redirection (>) in the unix shell directly to the file, in the ApplicationController.extract_contents
- # method. This might avoid the memory problems that are causing the backups to fail.
- # see http://blog.craigambrose.com/articles/2007/03/01/a-rake-task-for-database-backups
new
- %x[#{@@db_config['path']}mysqldump -u #{@@db_config['username']} #{@@password} --single-transaction -Q --add-drop-table -O add-locks=FALSE -O lock-tables=FALSE --hex-blob #{@@db_config['database']} > #{file} ]
+ system(sql_dump_to_file(file))
end
+ # argument is an instance of BackupFile
def self.restore_from_file(backfile)
- # when it has been tested in production mode, replace the next two lines with new
- new_back = backfile.filename
- @db_config = ActiveRecord::Base.configurations["development"] # TODO hard coded here... MUST CHANGE THIS AFTER TESTING
- password = @db_config['password'].nil? ? '' : "--password=#{@db_config['password']}"
- %x[#{@db_config['path']}mysql --database #{@db_config['database']} --host=#{@db_config['host']} --user=#{@db_config['username']} #{password} -e \"source #{new_back}\";]
+ new
+ system(sql_restore(backfile.filename))
$?.exitstatus.zero?
end
+private
+ def self.sql_dump_to_file(file)
+ sql_cmd =<<-SQL
+ #{@@db_config['path']}mysqldump\
+ -u #{@@db_config['username']} #{@@password}\
+ --single-transaction\
+ -Q\
+ --add-drop-table\
+ -O\
+ add-locks=FALSE\
+ lock-tables=FALSE\
+ --hex-blob #{@@db_config['database']} > #{file} ]
+ SQL
+ end
+
+ def self.sql_restore(filename)
+ sql_cmd =<<-SQL
+ #{@@db_config['path']}mysql\
+ --database #{@@db_config['database']}\
+ --host=#{@@db_config['host']}\
+ --user=#{@@db_config['username']} #{@@password}\
+ -e \"source #{filename}\";
+ SQL
+ end
+
end
View
29 spec/models/application_database_spec.rb
@@ -2,7 +2,7 @@
describe ".extract_contents" do
before do
- ActiveRecord::Base.connection.drop_table :test
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test`;")
ActiveRecord::Base.connection.create_table :test do |t|
t.column :foo, :string
end
@@ -15,7 +15,7 @@
describe ".save_to_file" do
before do
- ActiveRecord::Base.connection.drop_table :test
+ ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test`;")
ActiveRecord::Base.connection.create_table :test do |t|
t.column :foo, :string
end
@@ -25,3 +25,28 @@
ApplicationDatabase.extract_contents.should match "Table structure for table `test`"
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));
+ insert into test set foo = 'bar';
+ SQL
+ file = Rails.root.join('tmp','sql_test.sql')
+ File.write(file, sql)
+ #ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS `test`;")
+ #ActiveRecord::Base.connection.execute("create table test ( foo varchar(255))")
+ #ActiveRecord::Base.connection.execute("insert into test set foo = 'bar'")
+ ApplicationDatabase.restore_from_file(BackupFile.new(:filename => file))
+ end
+
+ it "should restore the database contents from file" do
+ ActiveRecord::Base.connection.execute("select * from test").first[0].should == 'bar'
+ end
+
+ after do
+ File.delete(Rails.root.join('tmp','sql_test.sql'))
+ end
+end

0 comments on commit 758781f

Please sign in to comment.