Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Last active July 4, 2025 14:22
Show Gist options
  • Save hopsoft/56ba6f55fe48ad7f8b90 to your computer and use it in GitHub Desktop.
Save hopsoft/56ba6f55fe48ad7f8b90 to your computer and use it in GitHub Desktop.

Revisions

  1. hopsoft renamed this gist Jul 2, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. hopsoft created this gist Jul 2, 2014.
    35 changes: 35 additions & 0 deletions db.rake
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # lib/tasks/db.rake
    namespace :db do

    desc "Dumps the database to db/APP_NAME.dump"
    task :dump => :environment do
    cmd = nil
    with_config do |app, host, db, user|
    cmd = "pg_dump --host #{host} --username #{user} --verbose --clean --no-owner --no-acl --format=c #{db} > #{Rails.root}/db/#{app}.dump"
    end
    puts cmd
    exec cmd
    end

    desc "Restores the database dump at db/APP_NAME.dump."
    task :restore => :environment do
    cmd = nil
    with_config do |app, host, db, user|
    cmd = "pg_restore --verbose --host #{host} --username #{user} --clean --no-owner --no-acl --dbname #{db} #{Rails.root}/db/#{app}.dump"
    end
    Rake::Task["db:drop"].invoke
    Rake::Task["db:create"].invoke
    puts cmd
    exec cmd
    end

    private

    def with_config
    yield Rails.application.class.parent_name.underscore,
    ActiveRecord::Base.connection_config[:host],
    ActiveRecord::Base.connection_config[:database],
    ActiveRecord::Base.connection_config[:username]
    end

    end
    12 changes: 12 additions & 0 deletions gistfile1.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    # dump the development db
    rake db:dump

    # dump the production db
    RAILS_ENV=production rake db:dump

    # dump the production db & restore it to the development db
    RAILS_ENV=production rake db:dump
    rake db:restore

    # note: config/database.yml is used for database configuration,
    # but you will be prompted for the database user's password