Skip to content

Commit

Permalink
Add postgres db to docker compose
Browse files Browse the repository at this point in the history
This commit adds a postgres db to the docker compose configuration so the analytics-reporter can write to a db when running in docker compose.

A tradeoff is that the migrations had to be copied into this app from analytics-reporter-api. Hopefully, if we integrate analytics-reporter-api into our docker-compose configuration, we can remove the migrations from this repo and use the ones in the API repo.
  • Loading branch information
jmhooper committed Jun 13, 2017
1 parent ef09624 commit 53d9c60
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions bin/update
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/bash

npm install
npm run migrate
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
version: "3.0"
services:
db:
image: postgres:9.6
environment:
- POSTGRES_DB=analytics-reporter
- POSTGRES_USER=analytics
volumes:
- pgdata:/var/lib/postgresql/data/
reporter:
command: node ./deploy/cron.js
environment:
Expand All @@ -15,10 +22,16 @@ services:
- AWS_REGION=${AWS_REGION}
- AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY}
- BUCKET_NAME=${BUCKET_NAME}
- POSTGRES_HOST=db
- POSTGRES_USER=analytics
- POSTGRES_DATABASE=analytics-reporter
image: node:7.7
links:
- db
volumes:
- .:/usr/src/reporter
- node_modules:/usr/src/reporter/node_modules
working_dir: /usr/src/reporter
volumes:
node_modules:
pgdata:
17 changes: 17 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const config = require("./src/config")

module.exports = {
development: {
client: 'postgresql',
connection: config.postgres,
},
test: {
client: 'postgresql',
connection: {
database: process.env.TRAVIS ? "travis_ci_test" : "analytics_reporter_test",
},
migrations: {
tableName: 'knex_migrations',
},
},
}
14 changes: 14 additions & 0 deletions migrations/20170308164751_create_analytics_data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex) {
return knex.schema.createTable("analytics_data", table => {
table.increments("id")
table.string("report_name")
table.string("report_agency")
table.dateTime("date_time")
table.jsonb("data")
table.timestamps(true, true)
})
};

exports.down = function(knex) {
return knex.schema.dropTable('analytics_data')
};
14 changes: 14 additions & 0 deletions migrations/20170316115145_add_analytics_data_indexes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
exports.up = function(knex) {
return knex.schema.table("analytics_data", table => {
table.index(["report_name", "report_agency"])
}).then(() => {
return knex.schema.raw("CREATE INDEX analytics_data_date_time_desc ON analytics_data (date_time DESC NULLS LAST)")
})
};

exports.down = function(knex, Promise) {
return knex.schema.table("analytics_data", table => {
table.dropIndex(["report_name", "report_agency"])
table.dropIndex("date_time", "analytics_data_date_time_desc")
})
};
12 changes: 12 additions & 0 deletions migrations/20170522094056_rename_date_time_to_date.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.up = function(knex, Promise) {
return knex.schema.raw("ALTER TABLE analytics_data RENAME COLUMN date_time TO date").then(() => {
return knex.schema.raw("ALTER TABLE analytics_data ALTER COLUMN date TYPE date")
})
};

exports.down = function(knex, Promise) {
return knex.schema.raw("ALTER TABLE analytics_data RENAME COLUMN date TO date_time").then(() => {
return knex.schema.raw("ALTER TABLE analytics_data ALTER COLUMN date_time TYPE timestamp with time zone")
})
};
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"homepage": "https://github.com/18F/analytics-reporter",
"license": "CC0-1.0",
"scripts": {
"migrate": "`npm bin`/knex migrate:latest",
"pretest": "NODE_ENV=test npm run migrate",
"start": "node app.js",
"test": "`npm bin`/mocha test/*.test.js test/**/*.test.js"
},
Expand Down
11 changes: 1 addition & 10 deletions test/support/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@ const connection = {

const resetSchema = () => {
const db = knex({ client: "pg", connection })
return db.schema.dropTableIfExists(ANALYTICS_DATA_TABLE_NAME).then(() => {
return db.schema.createTable(ANALYTICS_DATA_TABLE_NAME, (table) => {
table.increments("id")
table.string("report_name")
table.string("report_agency")
table.date("date")
table.jsonb("data")
table.timestamps(true, true)
})
})
return db("analytics_data").delete()
}

module.exports = { connection, resetSchema }

0 comments on commit 53d9c60

Please sign in to comment.