How to Manage a Database7:15 with Jim Hoskins
In this video, we will set up the database for our deployed application on Heroku. We will both migrate the database, and learn how we can push and pull data to and from Heroku's database.
[?music?] 0:00 [Master Class: Designer and Developer Workflow] 0:02 [Fourth Sprint: Managing the Database] 0:04 [Jim Hoskins] So we've deployed our application, we got it up and running, 0:07 and when we viewed the application in the browser, 0:10 we got a 500 error--something went wrong. 0:13 Now, this is probably due to the fact that we haven't set up the database. 0:17 There is a database configured, but we haven't migrated it to our current schema, 0:20 but let's just double-check that. 0:26 One of the important commands while using Heroku is $heroku logs. 0:28 You can type in $heroku logs 0:32 and this will pull down the most recent log from your application. 0:36 We can see that there was a 500 error, 0:40 so if we scroll up, you should be able to find the error that caused it 0:43 and it's an ActiveRecord error: the relation "jobs" does not exist 0:49 or the table jobs has not been created 0:52 and this makes sense since we haven't migrated our database. 0:56 So this brings us to the next of the Heroku commands that you'll be using quite often, 0:59 $heroku run 1:04 and this takes some arguments which will be the command to run on our application 1:06 on Heroku. 1:11 So normally, we would run rake db:migrate, but we need to run db:migrate 1:14 on our Heroku server, so we'll prefix $heroku run 1:18 and then rake dbmigrate. 1:21 So it's going to run dbmigrate, and it should put out any of the information 1:28 that runs on the Heroku server to our terminal. 1:32 So it looks like a migration ran just fine, 1:36 and let's see if that alleviated the problems in our application. 1:39 So I will refresh the page. 1:43 It looks like we've got some stuff coming in 1:46 and we got Easy Jobs! 1:48 Now there are no jobs because this is a fresh application 1:50 so we would have to register and create new jobs. 1:53 We could do that all by hand, but I'll show you a simpler way to do this shortly. 1:59 Now we've seen how to use the heroku run command to do rake db:migrate, 2:03 but this could be run for pretty much anything. 2:07 For instance, if we wanted to run the Rails console on the server, 2:11 we could do $heroku run console. 2:13 So what this does is it sets up a Rails console 2:20 that is connected to our terminal, but it's actually running on Heroku. 2:23 So we could, for instance, just do the things we would do on a normal console, 2:28 so we could take a look at the Job.all 2:31 and we could see that there are no jobs. 2:35 There's probably no users, either. 2:39 But we could, for instance, create a user or a job via the console right here. 2:43 Now, there's nothing I particularly want to do here, 2:48 but just to show you that $heroku run can run all sorts of different processes 2:51 on the Heroku server connected to the Heroku database from your local computer. 2:56 So if you ever have to do debugging, 3:00 this is how you would get into your production application environment. 3:02 Type in exit and it will quit right out. 3:06 Now, since Heroku sets up our database for us, 3:11 we don't really have a username and password to connect to it. 3:13 For instance, if we had a utility like phpMyAdmin for MySQL, 3:17 we wouldn't be able to just set it up to point to that database. 3:22 However, there are tools available that allow us to push and pull the database 3:26 between our production environment and our development environment. 3:31 In order to use it, we'll need to install a Gem called taps. 3:35 So we'll type in $sudo gem install taps. 3:39 So now we have the taps Gem installed, 3:45 so we'll be able to use the Heroku db:push and pull commands. 3:48 These commands are issued like $heroku db:push 3:52 or db:pull 3:57 and these work a lot like Git's push and pull. 4:00 If we push, it's going to take the database that it finds in our local database.yml file. 4:03 In this case, it points to a SQLite database, 4:10 and it's going to take all of the schema information as well as all the data 4:13 and push it to our Heroku application. 4:18 Alternatively, pull will take the database from our production application 4:22 and pull it into our development database that it finds in the database.yml file. 4:27 Now, if you want to push and pull from a specific database 4:33 that's not in your database.yml file, 4:36 you could put the URL of your local database after this. 4:39 For instance, if you want to do db:pull sqlite://development.db. 4:43 Or if you're using MySQL or Postgre, the URI would be different. 4:53 In this case, we'll use our normal development database 4:58 since that has all of the information we've been using in development 5:01 and we want to push it out to see if our application works properly. 5:05 So I'll do $heroku db:push. 5:09 Now what we've seen here is that the application is running 5:13 and it is giving us a helpful warning because what we're about to do 5:16 is we're about to overwrite all the data in our production application. 5:20 If we were to accidentally type this when we didn't mean to, 5:24 we could destroy all of the information in our production database. 5:27 Now, since right now it's empty and I definitely do want to take all of the development data 5:32 and put it into our production one, 5:36 we will go ahead and confirm, and we can do this by simply typing the application name 5:38 of our Heroku application, which right now is deep-meadow-7939. 5:43 We could also override it by doing - - confirm with that same string 5:49 when we run the application, but I like this way better because it will always give us 5:53 the warning when we're about to do something that could be destructive. 5:58 So we'll just type in deep-meadow-7939 6:02 and now it's sending the schema information, indexes, as well as the data, 6:08 so it's sending over jobs, users, as well as a schema migration. 6:14 So at this point, the production database should match exactly 6:20 to my current development database. 6:23 So if we open up the Heroku version of our site 6:25 and we refresh, we should see some jobs. 6:28 All right, we have Ice Cream Tester, Senior Fashion Police Officer, 6:32 and a lot of other really easy jobs. 6:36 And it hopefully imported our user information as well, 6:39 so I should be able to type in my username and password 6:42 and it looks like we have a problem, 6:48 so let's take a look at our Heroku logs and see if we can't glean 6:50 exactly what that problem is. 6:54 So we'll type in $heroku logs. 6:56 So it looks like we have a NoMethodError on our user for "valid_password?" 7:00 Now, this is interesting; we'll have to go ahead and debug this in the next video. 7:10
You need to sign up for Treehouse in order to download course files.Sign up