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