add gulp tasks for DB operations (issue #311); swap DB when running tests (issue #312)

This commit is contained in:
Christopher Van 2015-01-02 16:40:11 -08:00
Родитель 8c67a1edde
Коммит 5de8d8786b
7 изменённых файлов: 151 добавлений и 52 удалений

Просмотреть файл

@ -1,5 +1,9 @@
language: node_js
services:
- postgresql
node_js:
- "0.10"
before_script:
- cp ./settings_test.js.dist ./settings_test.js
addons:
postgresql: "9.3"

Просмотреть файл

@ -74,51 +74,44 @@ Initialise settings:
cp ./settings_test.js.dist ./settings_test.js
Set these environment variables:
NODE_ENV=test
GALAXY_API_SETTINGS=./settings_test.js
DATABASE_URL='postgres://localhost/galaxy-api'
To run tests:
npm test
To run tests without coverage and linting:
To run tests without destroying the database first:
npm run lab
npm run test-keepdb
To run tests with coverage and linting:
npm run test-verbose
## Database
If you haven't already, create a PostgreSQL database:
### `gulp` tasks
createdb galaxy-api
These are the available `gulp` tasks for PostgreSQL database and migration operations:
* `gulp createdb` - create a PostgreSQL database using `settings.POSTGRES_URL`.
* `gulp dropdb` - delete the database.
* `gulp migratedb` - run migrations.
* `gulp migratedb-create --name <name>` - create a new migration file called `<name>`.
* `gulp migratedb-up` - run all up migrations from the current state.
* `gulp migratedb-up --num <num>` - run `<num>` up migrations from the current state.
* `gulp migratedb-down` - run a single down migration.
* `gulp migratedb-down --num <num>` - run `<num>` down migrations from the current state.
### `psql` commands
To access the PostgreSQL prompt:
psql -d galaxy-api
To run migrations, run this from the shell:
These are a few helpful PostgreSQL commands:
node node_modules/.bin/pg-migrate up
To create a new migration:
node node_modules/.bin/pg-migrate create <migrationName>
To get a list the tables:
\dt+
To get a table's schema:
\d+ games
To delete a table:
drop table <table_name>;
To view a table in "extended display":
\x on
* `\h` - view list of available commands.
* `\dt+` - list all tables in the database.
* `\d+ <table_name>` - show a table's schema.
* `drop table <table_name>` - delete a table.
* `\x on` - view a table in "extended display" mode.

Просмотреть файл

@ -1,19 +1,113 @@
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var exec = require('child_process').exec;
var argv = require('yargs').argv; // Using instead of deprecated `gulp.env`.
var inquirer = require('inquirer');
var Promise = require('es6-promise').Promise;
var stylish = require('jshint-stylish');
var gulp = require('gulp');
var jshint = require('gulp-jshint');
var runSequence = require('run-sequence');
var shell = require('gulp-shell');
var sources = {
scripts: [
'*.js',
'api/**/*.js',
'lib/**/*.js'
]
var settings = require('./settings');
var internals = {
dbUrl: settings.POSTGRES_URL,
dbName: settings.POSTGRES_URL.substring(
settings.POSTGRES_URL.lastIndexOf('/') + 1
),
sources: {
scripts: [
'*.js',
'api/**/*.js',
'lib/**/*.js'
]
}
};
gulp.task('lint', function () {
return gulp.src(sources.scripts)
return gulp.src(internals.sources.scripts)
.pipe(jshint({esnext: true}))
.pipe(jshint.reporter(stylish));
});
gulp.task('createdb', shell.task([
'createdb ' + internals.dbName
]));
gulp.task('dropdb', shell.task([
'dropdb --if-exists ' + internals.dbName
]));
gulp.task('migratedb', ['migratedb-up']);
// Sample usage:
//
// gulp migrate-up
// gulp migrate-up --num 3
//
gulp.task('migratedb-up', shell.task([
'DATABASE_URL="' + internals.dbUrl + '" ' +
'node node_modules/.bin/pg-migrate up ' +
(argv.num || argv.number || argv.n || '')
]));
// Sample usage:
//
// gulp migrate-down
// gulp migrate-down --num 3
//
gulp.task('migratedb-down', shell.task([
'DATABASE_URL="' + internals.dbUrl + '" ' +
'node node_modules/.bin/pg-migrate down ' +
(argv.num || argv.number || argv.n || '')
]));
// Sample usage:
//
// gulp migrate-down
// gulp migrate-down --name addTimestampColumn
//
gulp.task('migratedb-create', shell.task([
'DATABASE_URL="' + internals.dbUrl + '" ' +
'node node_modules/.bin/pg-migrate create ' +
(argv.name || argv.n || '')
]));
gulp.task('refreshdb', function (cb) {
var runNow = function () {
return runSequence(
'dropdb',
'createdb',
'migratedb',
cb
);
};
if (process.argv.indexOf('--no-prompt') !== -1) {
return runNow();
}
inquirer.prompt({
type: 'confirm',
name: 'val',
message: 'Are you sure you want to drop the database?',
default: false
}, function (res) {
if (!res.val) {
return cb();
}
runNow();
});
});

Просмотреть файл

@ -4,7 +4,7 @@ var Hapi = require('hapi');
var routes = require('./api/routes');
var server = new Hapi.Server();
var server = module.exports = new Hapi.Server();
server.connection({
host: settings.HOST,
port: settings.PORT,
@ -16,11 +16,15 @@ server.connection({
}
}
});
routes(server);
server.start(function () {
console.log('Listening on %s', server.info.uri);
});
// Do not start the server when this script is required by another script.
if (!module.parent) {
server.start(function () {
console.log('Listening on %s', server.info.uri);
});
}
server.register({
register: require('hapi-node-postgres'),

Просмотреть файл

@ -16,7 +16,11 @@
"jshint-stylish": "^0.4.0",
"nodemon": "^1.2.1",
"code": "^1.2.1",
"lab": "^5.1.0"
"lab": "^5.1.0",
"run-sequence": "^1.0.2",
"gulp-shell": "^0.2.11",
"inquirer": "^0.8.0",
"yargs": "^1.3.3"
},
"engines": {
"node": ">= 0.10.x",
@ -31,9 +35,10 @@
"start": "node index.js",
"dev": "NODE_ENV=development GALAXY_API_SETTINGS=./settings_dev.js nodemon index.js",
"prod": "NODE_ENV=production GALAXY_API_SETTINGS=./settings_prod.js node index.js",
"test": "./node_modules/lab/bin/lab -c -L",
"lab": "./node_modules/lab/bin/lab",
"test-cover": "./node_modules/lab/bin/lab -c -r html -o ./test/artifacts/coverage.html && open ./test/artifacts/coverage.html"
"test": "GALAXY_API_SETTINGS=./settings_test.js gulp refreshdb --no-prompt && GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab",
"test-keepdb": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab",
"test-verbose": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab -c -L",
"test-cover": "GALAXY_API_SETTINGS=./settings_test.js ./node_modules/lab/bin/lab -c -r html -o ./test/artifacts/coverage.html && open ./test/artifacts/coverage.html"
},
"version": "0.0.5"
}

Просмотреть файл

@ -19,7 +19,7 @@ exports.HOST = '0.0.0.0';
exports.PORT = 4000;
// Usage: postgres://user:password@host/database
exports.POSTGRES_URL = process.env.DATABASE_URL || 'postgres://localhost/galaxy-api';
exports.POSTGRES_URL = 'postgres://localhost/galaxy-api';
exports.SECRET = 'a secret string';

Просмотреть файл

@ -1,2 +1 @@
module.exports = {
};
exports.POSTGRES_URL = 'postgres://localhost/galaxy-api_test';