3

Commands that work:

  • rake db:migrate
  • rake db:rollback
  • rake db:seed

Commands that don't:

  • rake db:drop
  • rake db:drop:all
  • rake db:migrate:reset
  • rake db:purge

If I use db:rollback enough times (or with STEP), my databases drop successfully, but not when I use db:drop / db:drop:all. db:migrate:reset and db:purge leave all the data untouched. Running with trace, watching development.log, system logs and watching DB queries via the DB monitor (no SQL queries appear to run for those commands.) No errors, either.

Running:

  • Rails 4.2
  • postgres 9.4
  • CentOS 7
J B
  • 390
  • 1
  • 4
  • 16
  • Do you have any open connections to the database your are modifying? PG won't drop a database if there are open connections. – Philip Hallstrom Jan 08 '15 at 17:23
  • @PhilipHallstrom No, I don't. This happens even if I issue commands immediately after starting the DB (it's also a single user local VM with nothing else running.) – J B Jan 09 '15 at 00:06
  • I have the same problem, when I change database.yml to SQLite, it recreates the database and update schema.rb. When I connect to Postgres, It does nothing. This is not a problem with another session being in the database (I've investigated it), this is something different. As J B, I don't see any output at all. – Giron Jan 20 '15 at 20:23

3 Answers3

3

You can't drop a PG database if a connection exists.

Unless you use this magic:

# config/initializers/postgresql_database_tasks.rb
module ActiveRecord
  module Tasks
    class PostgreSQLDatabaseTasks
      def drop
        establish_master_connection
        connection.select_all "select pg_terminate_backend(pg_stat_activity.pid) from pg_stat_activity where datname='#{configuration['database']}' AND state='idle';"
        connection.drop_database configuration['database']
      end
    end
  end
end

put that in your intializers and run this command:

rake environment db:drop

The environment argument makes rake check the initializers.

Please note that with Rails >= 6, you must replace configuration['database'] with configuration_hash[:database]

SylvainB
  • 394
  • 7
  • 16
Mirror318
  • 11,875
  • 14
  • 64
  • 106
0

I had the same problem as you and this answer helped me to figure it out. You were probably editing some migration by hand and that caused this problem. In the end I had to drop all the tables by hand, that it worked. But it could be enough to just drop the schema_migrations table, where rails stores already run migrations. So try to drop all the tables if you can afford it or just drop the schema_migrations table.

EDIT: Unfortunately, It still doesn't work, dropping by hand solved just the recreation of schema.rb, but the db:drop is still not run. So the problem still persist.

Community
  • 1
  • 1
Giron
  • 350
  • 3
  • 16
0

If you have a backup of your db, you can reset your db using pg_restore:

pg_restore -U db_user -d db_name -h db_host .path/to/db_dump

This will reset your db with the contents of the backup.

ethaning
  • 386
  • 4
  • 12