Arquivo da tag: PostgreSQL

ruby on rails value Rails reset ALL Postgres sequences?

ActiveRecord::Base.connection.tables.each do |table|
  result = ActiveRecord::Base.connection.execute("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1") rescue ( puts "Warning: not procesing table #{table}. Id is missing?" ; next )
  ai_val = result.any? ? result.first['id'].to_i + 1 : 1
  puts "Resetting auto increment ID for #{table} to #{ai_val}"
  ActiveRecord::Base.connection.execute("ALTER SEQUENCE #{table}_id_seq RESTART WITH #{ai_val}")

How to reset PG Database on Heroku?

How to reset PG Database on Heroku?

  • Step 1: heroku restart
  • Step 2: heroku pg:reset DATABASE (no need to change the DATABASE)
  • Step 3: heroku run rake db:migrate
  • Step 4: heroku run rake db:seed (if you have seed)

One liner

heroku restart; heroku pg:reset DATABASE --confirm APP-NAME; heroku run rake db:migrate

Note 1

Heroku doesn’t allow users from using rake db:resetrake db:drop and rake db:create command. They only allow heroku pg:reset and rake db:migrate commands.

More info:

Note 2

If you have more than 1 remote, append --remote [your_remote_name] like this:

heroku run rake db:migrate --remote dev (dev is example remote here)