diff --git a/bin/update b/bin/update index a2f3a7c..088f092 100755 --- a/bin/update +++ b/bin/update @@ -1,3 +1,4 @@ #!/bin/bash npm install +npm run migrate diff --git a/docker-compose.yml b/docker-compose.yml index 13bbe54..3e427f0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: @@ -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: diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 0000000..86615b6 --- /dev/null +++ b/knexfile.js @@ -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', + }, + }, +} diff --git a/migrations/20170308164751_create_analytics_data.js b/migrations/20170308164751_create_analytics_data.js new file mode 100644 index 0000000..12f4c0c --- /dev/null +++ b/migrations/20170308164751_create_analytics_data.js @@ -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') +}; diff --git a/migrations/20170316115145_add_analytics_data_indexes.js b/migrations/20170316115145_add_analytics_data_indexes.js new file mode 100644 index 0000000..19e4fe2 --- /dev/null +++ b/migrations/20170316115145_add_analytics_data_indexes.js @@ -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") + }) +}; diff --git a/migrations/20170522094056_rename_date_time_to_date.js b/migrations/20170522094056_rename_date_time_to_date.js new file mode 100644 index 0000000..519eace --- /dev/null +++ b/migrations/20170522094056_rename_date_time_to_date.js @@ -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") + }) +}; diff --git a/package.json b/package.json index 7b96692..76fafd8 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/test/support/database.js b/test/support/database.js index 061d0a4..7e2076c 100644 --- a/test/support/database.js +++ b/test/support/database.js @@ -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 }