Skip to content

Instantly share code, notes, and snippets.

@nvd
Created June 20, 2012 22:49
Show Gist options
  • Save nvd/2962709 to your computer and use it in GitHub Desktop.
Save nvd/2962709 to your computer and use it in GitHub Desktop.
Cleans up, recreates and repopulates Postgres db in Rails app
#!/bin/sh
echo "Attempting to delete myapp_dev & myapp_test"
drop_the_database(){
local db=$1
local user=$2
psql $db $user << EOF
SELECT
pg_terminate_backend(procpid)
FROM
pg_stat_activity
WHERE
procpid <> pg_backend_pid();
EOF
dropdb $db
return $TRUE
}
drop_the_database "myapp_dev" "user_name"
drop_the_database "myapp_test" "user_name"
echo "... done"
echo "2.Create DBs again"
rake db:create:all
echo "... done"
echo "3.Running the migrations"
rake db:migrate
echo "... done"
echo "4.Seed db with test values"
rake db:seed
rake db:populate
echo "... done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment