Lab materials for 6 + 7
this is code you should run in a terminal
This is an aside, an optional or alternate explaination
-
Setup an account on heroku (You should have done this over the weekend)
-
Make sure the heroku CLI tools are installed with
heroku version
, if not then install the Heroku CLI tools -
Make sure Go is installed on your machine using
go version
, if not use this tutorial -
If you run into any issues, check heroku's help center
-
Fork the lab repo at https://github.com/uw-info340b-sp2016/lab6-7 by going to the page and clicking the "fork" button. Don't clone this repo! You won't be able to push to it, which is required to turn in your assignment!
-
Clone your newly forked repo from https://github.com/YOUR-USERNAME-HERE/lab6-7 to your machine usingSkip this and use the below stepsgo get github.com/YOUR_GITHUB_USERNAME/lab6-7
This step is the same as: (replace $GOPATH with %GOPATH% on windows)
cd $GOPATH/src/github.com
- (If this step fails with "No such directory" then run
cd $GOPATH
thenmkdir src
,cd src
,mkdir github.com
, and finallycd github.com
)
- (If this step fails with "No such directory" then run
mkdir YOUR_GITHUB_USERNAME
cd YOUR_GITHUB_USERNAME
git clone https://github.com/YOUR-USERNAME-HERE/lab6-7
cd lab6-7/cmd/lab7
go install
If you run into any issues with these steps, especially if you get an issue with "go cannot find GOPATH" or something similar, follow these steps on setting up Go
For this assignment we will be using a new tool known as PostgreSQL. This is different than SQLite in many ways, which, if you are interested, you can find out the exact differences at this site. Postgres is much better than SQLite in pretty much every way. It stores more datatypes, is well optimized, better checks on input, the list goes on. However, SQLite has a much smaller file size than Postgres so SQLite is best when you want one local DB running on a phone or simple desktop app. You only really want to use Postgres when you have a server running the DB, although there is nothing stopping you from running a local instance of Postgres.
-
Login to your heroku account and go to your dashboard
-
Create a new app ("+" in the top right corner, select "Create new app")
-
Set
app name = last-first-lab7
(Actually fill in your first and last name, lowercase only) andruntime selection = United States
-
Wait for the app to setup. This may take a minute or two.
-
While waiting, open your terminal and type
heroku login
then enter in your username + password to heroku. From now on don't close this terminal and run all "heroku" commands from here. If you do close it, you will need toheroku login
again. -
Open up your terminal and navigate to the project you cloned earlier.
cd $GOPATH/src/github.com/YOUR_GITHUB_USERNAME/lab6-7
-
Run
heroku git:remote -a last-first-lab7
-
Push this code up to your new heroku app with
git push heroku master
-
Open the app by running
heroku open
or by going to http://last-first-lab7.herokuapp.com/ The only error you should get is a missing database url, you can ignore that. Note: because the way heroku works, the free tier has a "dyno spin up time" if it is sleeping, and it may take up to 20 seconds for the page to load See this page on sleeping for more info -
Make a change to both the HTML + CSS, it doesn't have to be a huge change, but add some formatting or a new title. Whatever you feel like (DON'T delete the list element with ID='results', this is required for lab 7)
This next section will be basically the same steps as if you were pushing your code to GitHub, except it will be going to heroku instead
-
Run
git add .
,git commit -m "put a message here"
, and finallygit push heroku master
-
Admire your new changes by refreshing the page you were on
- Go to https://postgres.heroku.com/databases (this can also be accessed by clicking on the 6 boxes in the top left of your dashboard)
- Select "create database" in the top right. This will take a few minutes to prepare, but that's okay.
- Click on the database name and wait for the page to load. Once it does, click "show" next to the URL attribute under "Connection Settings"
- Copy this url. It should look something like postgres://z7fjdafjil:-dsFOSDJGc...
- Go back to your dashboard for heroku, click on your app, click on settings, then click "Reveal config vars"
- Add a new config var with the key
DATABASE_URL
and value as the prostgres url you copied. Then hit "add" - Refresh your heroku website and you should see the error message removed.
If you've gotten this far you are done for lab 6! I know it was a lot of setup, copying + pasting, and other boring things, but this is important as setting this stuff up takes quite a bit of time. Lab 7 will have you actually working with this database and application. For now though, there is nothing to turn in.
Run the following command once within your local reop. Once you do this you won't have to do it again (unless you work on lab machines)
git remote add upstream https://github.com/uw-info340b-sp2016/lab6-7.git
Then run the following command every time you want to get changes that I made in my repo
git pull upstream master
Depending on your text editor you may need to write a commit. In nano is is CTRL+O, CTRL+X and in vim this is :wq. There shouldn't be any merge conflicts, depending on what changes you made to the HTML.
In this lab you will be doing 2 things. First, you will plan out then create some tables and data within your database. After that you will create 3 queries that will operate on that data you inserted. I suggest working in groups for this assignment, so you can have enough data to insert into the table as well as assisting each other in creating questions for your queries. Word of warning: You will be doing this for the final project, so don't just copy someone else's code/queries. We will expect you to understand these queries for the final exam/project The main point of this lab is going to be a "mini-final project" where you have a DB design, you implement that design, and finally operate on that newly created DB.
- First, make sure you have pulled the changes from my repo.
- Open up a new terminal and run
heroku login
, then open up your databases in postgres.heroku.com. Under "Connection Settings" there is an attribute called "psql" copy that command (it should look likeheroku pg:psql --app ....
), paste, and run it in a terminal. You now can input SQL to create tables and insert data- Helpful notes:
\dt
lists out all tables in your database\d+ table_name
lists out the details of that table such as columns- Just like SQLite, end your queries with
;
- If you are getting an error, check you copied the right command and also that you have postgres installed
- Helpful notes:
- After you have setup your DB and inserted some data, come up with 3 questions to answer with this model. You are free to come up with any questions you like, as well as working with others to create questions. Your requirements for this are:
- One query must use a non-trivial subquery (so, no doing
WHERE id = (SELECT id FROM users WHERE name = "test")
, which is basically just like doing the inner query) - One query must use an aggregate function such as MIN, MAX, AVG
- Two of the three (I suggest the sub and aggregate) queries must be more complex than a single table where query such as SELECT * FROM table WHERE column = "something". If you feel unsure about a query being "complex enough" feel free to message me
- One query must use a non-trivial subquery (so, no doing
- Implement those queries, returning an HTML table with the results (outlined in main.go)
- Make sure this is displayed properly in your webpage, this includes adding in your question above your results
- Turn in a link to your webpage and a link to your github repo
I also suggest running your application before pushing it to heroku, to make sure it works. You can do this by doing the following:
- Create a new file in your project folder named
.env
then edit that file to include the lineDATABASE_URL=postgres://fj8xc9ejsdf:-asdfjisd...
replacing that with your actual URL - Run
cd cmd/lab7
from inside your project folder, thengo install
- Open a command prompt and run
heroku local
- If you get an error, add
export PATH=$PATH:$GOPATH/bin
to your bash startup script (nano ~/.bash_profile
on mac, edit environment variables on Windows changing:
to;
)
If you get an error from Go then you can use the command go get ...
adding in the package you are missing. For this project, these are go get github.com/gin-gonic/gin
and go get github.com/lib/pq
If you need to install your local app on your machine then navigate into the lab6-7/cmd/lab7 folder and run go install
If Heroku complains about a missing package, run go get github.com/tools/godep
then godep save ./...
If that fails, run $GOPATH/bin/godep save ./...
(use %GOPATH% for windows)