Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Sara Miller
Sara Miller
7,954 Points

Updating Heroku Database?

I pushed an initial rails app to Heroku and it's working fine. I've added a feature on my local machine that needed a new column in the user table with a default. In my local database (sqlite), I just dumped the fake data I had and now when I create a new user, the default value is included in the new column.

But when I go to push to Heroku with the new feature, I don't want to delete the data that's there, but I do want to add the column (I assume that migrating the database would add the new column just like it did locally) WITH the default (a string, in this case). Is there a way to cycle through the rows that already exist and add a default value for the new column?

I found this information in Heroku's Dev Center and I think (haven't tested this) that I can put the site down for maintenance, backup the current database, create a new database (with my defaulted column), and then migrate the old data from the backup to the new database. Am I over-complicating this? Is there a better way?

4 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Sara,

There might be an easier way, can you give me a more detailed example of what you're trying to do and I'll try and help you out?

Sara Miller
Sara Miller
7,954 Points

Sure. I have an app here (don't laugh! I'm brand stinking new at this and just made a tester app to play with the stuff I'm learning at Treehouse). Basically, users can create parties and other users can join parties (I set up a table, Rsvps, similar to the User_friendships in your rails videos).

I want to add a column to Users, :level, that describes how many items a party attendee has volunteered to bring (starts at "mooch", continues to "selfless giver"). I have 4 users. If I create a migration that adds a column with a default value of "mooch", the value for those 4 users would be "nil". Only new users would have a default value of "mooch". Am I wrong about that?

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Nope, you're absolutely right. What you could do is launch a console on heroku:

heroku run console

Then do the following:

User.all.each {|user| user.update_attribute :level, 'mooch'}
Sara Miller
Sara Miller
7,954 Points

Sweet! You are so awesome. Wow, was I ever overthinking things.

Thank you so much!