Arquivo da tag: Rails

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: https://devcenter.heroku.com/articles/rake

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)

https://gist.github.com/zulhfreelancer/ea140d8ef9292fa9165e#how-to-reset-pg-database-on-heroku

Explicitly Set Table Name

https://til.hashrocket.com/posts/11cd334000-explicitly-set-table-name

ActiveRecord includes a table_name method that infers your database table name based on a class name. For an empty model called AlternativePost, here’s what it comes up with:

[0] > AlternativePost.table_name
=> "alternative_posts"

If this isn’t right, you’re in trouble (Postgres example):

[1] > AlternativePost.new
PG::UndefinedTable: ERROR:  relation "alternative_posts" does not exist

Luckily, you can set the table name explicitly with the table_name= method.

class AlternativePost < ActiveRecord::Base
  self.table_name = 'posts'
end

Now Rails knows which database table to refer to:

[2] > AlternativePost.table_name
=> "posts"
[3] > AlternativePost.new
=> #<AlternativePost:0x007f8554dcbd98 id: nil, title: nil...

A practical application would be a model Post that corresponds to a table organized inside a Postgres schema admin.posts.

Rails API + JWT Authentication

https://medium.com/binar-academy/rails-api-jwt-authentication-a04503ea3248

Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing