Commit ad9b357b

mo khan <mo@mokhan.ca>
2013-11-10 16:49:09
fix rake db:restore task.
1 parent fb43dbf
Changed files (2)
config
lib
config/recipes/postgresql.rb
@@ -41,12 +41,5 @@ namespace :postgresql do
 
     run "PGPASSWORD='#{postgresql_password}' pg_dump -Fc --no-acl --no-owner -h #{postgresql_host} -U #{postgresql_user} #{postgresql_database} > #{backup_path}/#{filename}"
     download("#{backup_path}/#{filename}", "db/backups/", :via => :scp)
-    run_locally "cd tmp; rm database.dump; ln -s #{filename} database.dump"
-  end
-
-  task :restore do
-    dumpfile = "~/db/backups/latest"
-    upload("latest", "db/backups/latest", :via => :scp)
-    run "psql #{postgresql_database} < #{dumpfile}"
   end
 end
lib/tasks/database.rake
@@ -1,16 +1,13 @@
 namespace :db do
   task :restore do
-    dumpfile = "latest"
     config = Rails.configuration.database_configuration
     destination_db = config[Rails.env]["database"]
-    puts "restoring to #{destination_db}"
-    sh "psql #{destination_db} < #{dumpfile}"
-    #sh "pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{config[Rails.env]["username"]} -d #{destination_db} #{dumpfile}"
+
+    newest_backup = Dir['db/backups/*.dump'].max_by { |file| File.mtime(file) }
+    sh "PGPASSWORD=#{config[Rails.env]["password"]} pg_restore --verbose --clean --no-acl --no-owner -h localhost -U #{config[Rails.env]["username"]} -d #{destination_db} #{newest_backup}"
   end
 
   task :backup do
-    puts "Please specify RAILS_ENV:"; return unless ENV['RAILS_ENV']
-    puts "backup up #{ENV['RAILS_ENV']}"
     sh "cap #{ENV['RAILS_ENV']} postgresql:backup"
   end