Why can't I use rake db: drop or rake db: purge successfully?

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 for enough time (or with STEP) my databases drop successfully, but not when I use db: drop / db: drop: all. db: migrate: reset and db: clear all data intact. Working with tracing, viewing development.log, system logs and viewing database queries through the database monitor (no SQL queries are run for these commands). There are also no errors.

Duration:

  • Rails 4.2
  • postgres 9.4
  • CentOS 7
+3


source to share


2 answers


You cannot delete the PG database if a connection exists.

If you don't 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 this in your intializers and run this command:

rake environment db:drop

      

The environment argument makes the rake check with initializers.

+1


source


I had the same problem as you and this answer helped me figure it out. You probably edited some kind of migration manually and it caused this problem. In the end I had to drop all the tables manually for this to work. But that might be enough to just drop the schema_migrations table, where rails stores migrations that have already been done. So try dropping all tables if you can afford it, or just drop the schema_migrations table.



EDIT: Unfortunately it still doesn't work, but manually dropping only solves the schema.rb rebuilding, but db: drop still fails. Therefore, the problem still persists.

0


source







All Articles