简体   繁体   中英

Heroku] Dealing with database

I ran into a problem recently while dealing with heroku postgresql database.

I wanted to update an attribute of a certain user (make him an admin), so I pulled the db from heroku to update his attribute locally and push it back again.

heroku db:pull
UPDATE users SET user_type = 'admin' WHERE name = 'thisuser'; //in postgres
heroku db:push

Then, the problem below occurred.

!!! Caught Server Exception
HTTP CODE: 500
Taps Server Error: PGError: ERROR:  time zone displacement out of range: "2013-01-21 12:00:00.000000+5895153600"
LINE 1: ....facebook.com/thisuser', 'facebook', '2374538475', '2013-01-2...

So I ran the following commands again to recover from the error.

heroku db:pull
heroku db:push

And this resulted in wiping out entire my DB from both the production and development. I have two questions to ask.

  1. What is the proper way of updating an attribute of some user on Heroku environment? It seems a little too much to pull an entire DB just to update one attribute of a small object.
  2. Why did my actions result in wiping out everything from the DB?

Thank you for your help. I appreciate it a lot.

For your command, I'd advise you do this kind of small stuff in a console :

$ heroku run rails console

You can type one-off commands just as you would on your local machine.

About the instability: apparently it's a bug with Heroku on Ruby 1.9.3. On 1.9.2 you won't get the problem. So if you use rbenv or rvm, you can switch rubies before executing heroku. You might even want to write a little shell script eg:

#!/bin/sh
#safe_heroku.sh
HEROKU_CMD=/usr/local/heroku/bin/heroku   # Check which heroku
RBENV_CMD=/usr/bin/rbenv                  # Check which rbenv
SAFE_RUBY_VERION=1.9.2                    # Check rbenv versions 

$RBENV_CMD shell $SAFE_RUBY_VERSION
$HEROKU_CMD "$@"

Usage:

$ safe_heroku rake db:push   # Or whatever command you want to run...

I agree with the answer above however I try to use ruby to safely do any changes that I need for instance to add a role to a user I would do:

$ heroku run console
user = User.find(1)
Role = Role.find(1)
user.roles << role

or in your case

user = User.find_by_name('user name')
user.user_type = 'admin'
user.save!

Safer to use your tested code to make updates instead of hacking the DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM