Skip to content
This repository has been archived by the owner on Oct 21, 2019. It is now read-only.

Commit

Permalink
Merge fe01004 into 5992633
Browse files Browse the repository at this point in the history
  • Loading branch information
bobheadxi committed Jun 17, 2018
2 parents 5992633 + fe01004 commit 0762083
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 32 deletions.
13 changes: 11 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ env:
secure: mFPx3xxBJEl6JLz+135oTxmjd/iBnf+GbrvrvzYmIav/2SgS6mlUchSNgU/ru4v/PFZNi5UcZ2iuZoJ/S4cautuaQUfxUcb6c8kSTmmnGOxNusvPmmgr/FsIFJ+QKabA4jsO3PdSgm0ZiHCfFop2dohofdJEGZRFrCvMJmvPzwTWPVlDSuyBlFsS19pEbzPZJsx9EtA9G4dh60LASCQDcO+GgafOBn9gTYjHuNxWqbQEeT02HpgRCxSMAOK5aEuQIl3E5MtatFH/LszH9543VEj7zyo+sqqd5kPECLq78gpebl57kcnqlVnJglZd7/aM0NSDWto1eIR6k0IV/KKcBYhXeITcXjmpoBAz3JU217O8ouPCwvLcvZ1AwZytn+odinrcsRpwpNu9osuWoCYeJ/1QQg5IHp4l1VK4FdJ8P79wBV2r16ueCtj0oF+OhcxXT5WsCAfzpZCOwKdlYA97oNy563cbQ98I7e03GV2F/1Oq25Vvqa74yFCG93vEdjgHOsVD/eoAiUppLT7GnITt1Esa3+WuwQiiN0X2BKjU/1NVrLo/mmW05D5Onzgajst2GFcebCLzpy13zzZrdPnnbMI/bOpJ3PfYe0UZ4S7l1EmIUApeGljF4+78zegs1WYbuNvLSdojYLSUbhIDgdBS1MNmDs9KCjTFGzRsr3LEE4g=

before_install:
- go get -u github.com/mattn/goveralls
- go get -u github.com/golang/dep/cmd/dep
- go get github.com/mattn/goveralls
- go get github.com/golang/dep/cmd/dep

install:
- dep ensure

services:
- postgresql

before_script:
- psql -c 'create database rocket_test_db;' -U postgres
- psql -d rocket_test_db -a -f ./schema/tables.sql

script:
- go test -race -coverprofile=coverage.out ./...
- goveralls -coverprofile=coverage.out -service=travis-ci -repotoken "$COVERALLS_TOKEN"
Expand Down
23 changes: 19 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
.PHONY: deps clean rocket test

all: rocket

.PHONY: rocket
rocket:
go install

.PHONY: deps
deps:
glide install

.PHONY: clean
clean:
rm rocket
rm -f rocket
pg_ctl -D /usr/local/var/postgres stop -s -m fast

.PHONY: test
test:
go test ./... -short -cover

.PHONY: test-integration
test-integration: mock-db
go test ./... -cover
make clean

# Sets up a local database for testing
.PHONY: mock-db
mock-db:
sh mock_db.sh

.PHONY: docker
docker:
docker-compose up -d

.PHONY: build
build:
docker-compose up -d --build rocket
docker-compose up -d --build rocket
71 changes: 48 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@

Rocket is the management and onboarding system for UBC Launch Pad. More information can be found in the [Wiki](https://github.com/ubclaunchpad/rocket/wiki).

## Development

To get started, download the Rocket codebase:

```bash
$ go get github.com/ubclaunchpad/rocket
$ make test # run unit tests
```

Additional integration tests can be run if you have `postgres` installed (for Mac users, an easy way is to `brew install postgresql`):

```bash
$ make test-integration # runs integration tests
```

Make sure you mark integration tests as `-short`-skippable:

```go
func TestMyIntegratedFunction(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
// ...
}
```

## Architecture

### Rocket
Expand Down Expand Up @@ -73,29 +99,7 @@ When specifying an option for a command you'll need to fill in the following fie

We use the [go-pg](https://github.com/go-pg/pg) for querying our Postgres database from Rocket. The `dal` package provides an interface to querying our database. The `model` package holds all our data structures that are used by the `dal` package in our queries.

### Postgres

#### Schema

Our schema is defined in [tables.go](schema/tables.sql). If you're starting the database for the first time you'll need to execute that script:

```bash
# Copy tables.sql into the /tmp folder in the Postgres container
$ docker cp schema/tables.sql <Postgres container ID>:/tmp/
# Run a shell in the Postgres container
$ docker-compose exec postgres bash
# Execute the SQL script against the database
$ psql -U <ROCKET_POSTGRESUSER> -d <ROCKET_POSTGRESDATABASE> < /tmp/tables.sql
# Exit the container
$ exit
```

Note that all the data stored in the DB is mounted into the Postgres container from a directory called `pgdata` in the root folder of this project. This means you can kill the Postgres container and bring it up again and none of your data will be lost.

#### Migrations

If you're updating the DB schema because you want to store a new resource or update an existing one:
you'll need to create a migration script under [schema/migrations](schema/migrations) and run it against the DB the same way you would run `schema.sql`. Don't forget to update `schema.sql` to include any changes you apply in your migrations.
The database schema is defined in [tables.go](schema/tables.sql).

## Docker Setup

Expand Down Expand Up @@ -124,3 +128,24 @@ Before deploying you will have to create two config files using the templates pr
* `POSTGRES_DB`: the name of the DB to create, but `rocket` is the most sensical choice - make sure this matches `ROCKET_POSTGRESDATABASE`

These variables are propagated to their respective Docker containers when you do `docker-compose up` via the `env_file` property, so make sure `env_file` for the `rocket` service points to your app environment variables file, and `env_file` for the `postgres` service points to your DB environment variables file. Our `docker-compose.yml` points to `.app.env.example` and `.db.env.example` by default. Note that data is mounted into the Postgres container from a directory called `pgdata` in the `rocket` directory. The first time you do `docker-compose up` this directory will be created for you, the rest of the time it will be re-used. This was if the DB container goes down your data is still on the host machine in the `pgdata` directory.

#### Database Setup

If you're starting the database for the first time you'll need to execute the script defining Rocket's schemas in `schema/tables.sql`:

```bash
# Copy tables.sql into the /tmp folder in the Postgres container
$ docker cp schema/tables.sql <Postgres container ID>:/tmp/
# Run a shell in the Postgres container
$ docker-compose exec postgres bash
# Execute the SQL script against the database
$ psql -U <ROCKET_POSTGRESUSER> -d <ROCKET_POSTGRESDATABASE> < /tmp/tables.sql
# Exit the container
$ exit
```

Note that all the data stored in the DB is mounted into the Postgres container from a directory called `pgdata` in the root folder of this project. This means you can kill the Postgres container and bring it up again and none of your data will be lost.

#### Migrations

If you're updating the DB schema because you want to store a new resource or update an existing one: you'll need to create a migration script under [schema/migrations](schema/migrations) and run it against the DB the same way you would run `schema.sql`. Don't forget to update `schema.sql` to include any changes you apply in your migrations.
33 changes: 33 additions & 0 deletions data/dal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package data

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/ubclaunchpad/rocket/config"
)

func newTestDBConnection() *DAL {
cfg := &config.Config{
PostgresHost: "localhost",
PostgresPort: "5432",
PostgresDatabase: "rocket_test_db",
}
if os.Getenv("TRAVIS") == "true" {
cfg.PostgresUser = "postgres"
} else {
cfg.PostgresUser = "rocket_test"
}
return New(cfg)
}

func TestNewDAL(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
dal := newTestDBConnection()
defer dal.Close()
err := dal.Ping()
assert.Nil(t, err)
}
49 changes: 49 additions & 0 deletions data/member_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package data

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/ubclaunchpad/rocket/model"
)

func TestMemberCreateGetUpdateRemove(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
dal := newTestDBConnection()
defer dal.Close()

// Create a new member
member := &model.Member{
SlackID: "1234",
Name: "Big Bruno",
}
err := dal.CreateMember(member)
assert.Nil(t, err)

// Get existing member
memberGet := &model.Member{SlackID: "1234"}
err = dal.GetMemberBySlackID(memberGet)
assert.Nil(t, err)
assert.Equal(t, member.Name, memberGet.Name)

// Update existing member
memberUpdated := &model.Member{
SlackID: "1234",
Name: "Little Bruno",
Position: "A REAL GUY",
}
err = dal.UpdateMember(memberUpdated)
assert.Nil(t, err)
err = dal.GetMemberBySlackID(memberGet)
assert.Nil(t, err)
assert.Equal(t, memberUpdated.Name, memberGet.Name)
assert.Equal(t, memberUpdated.Position, memberGet.Position)

// Delete existing member
err = dal.DeleteMember(&model.Member{SlackID: "1234"})
assert.Nil(t, err)
err = dal.GetMemberBySlackID(&model.Member{SlackID: "1234"})
assert.NotNil(t, err)
}
26 changes: 26 additions & 0 deletions mock_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This script handles local database creation to make testing easier.
# Run with `make db`

postgres -V
if [ "$?" -gt "0" ]; then
echo "MAKE: Postgres not installed!"
exit 1
fi

echo "MAKE: Killing existing postgres processes..."
pg_ctl -D /usr/local/var/postgres stop -s -m fast
pg_ctl -D /usr/local/var/postgres start
while true; do
pg_ctl -D /usr/local/var/postgres status
if [ "$?" -gt "0" ]; then
echo "MAKE: Waiting for postgres to start..."
sleep 3
else
break
fi
done

createuser -s rocket_test
createdb rocket_test_db
psql -d rocket_test_db -a -f ./schema/tables.sql
echo "MAKE: Local database ready to go!"
6 changes: 3 additions & 3 deletions schema/tables.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DROP TABLE IF EXISTS members;
DROP TABLE IF EXISTS members CASCADE;
CREATE TABLE members (
slack_id TEXT PRIMARY KEY,
name TEXT,
Expand All @@ -13,15 +13,15 @@ CREATE TABLE members (
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc')
);

DROP TABLE IF EXISTS teams;
DROP TABLE IF EXISTS teams CASCADE;
CREATE TABLE teams (
name TEXT UNIQUE,
github_team_id INTEGER PRIMARY KEY,
platform TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc')
);

DROP TABLE IF EXISTS team_members;
DROP TABLE IF EXISTS team_members CASCADE;
CREATE TABLE team_members (
team_github_team_id INTEGER REFERENCES teams(github_team_id) ON DELETE CASCADE,
member_slack_id TEXT REFERENCES members(slack_id) ON DELETE CASCADE,
Expand Down

0 comments on commit 0762083

Please sign in to comment.