Skip to content

Commit

Permalink
overhaul seeds and migrate
Browse files Browse the repository at this point in the history
ref #10
  • Loading branch information
maxgrossman committed Aug 4, 2018
1 parent db4914f commit 00916cb
Show file tree
Hide file tree
Showing 25 changed files with 264 additions and 95 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
sudo: required

langauge: python

services:
- docker

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ RUN apt-get update \
libspatialite-dev \

# -- specific node version -- #
RUN curl -sL https://deb.nodesource.com/setup_8.x \
RUN curl -sL https://deb.nodesource.com/setup_10.x \
&& apt-get install nodejs
6 changes: 3 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module.exports = {
test: {
db: ':memory:'
db: './db/test-posm-paths.sqlite3'
},
development: {
db: './db/posm-paths.sql'
develop: {
db: './db/posm-paths.sqlite3'
}
}
2 changes: 1 addition & 1 deletion db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Database {
constructor() {
if (!Database.instance) {
this._spatialite = process.env.SPATIALITE_LOCATION;
this._dbLoc = config[process.env.ENVIRONMENT].db;
this._dbLoc = config[process.env.ENVIRONMENT || 'develop'].db;
}
return Database.instance
};
Expand Down
17 changes: 17 additions & 0 deletions db/migrations/.migrate
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"lastRun": "1533377102405-add-images.js",
"migrations": [
{
"title": "1533377087768-add-users.js",
"timestamp": 1533418332304
},
{
"title": "1533377095426-add-sequences.js",
"timestamp": 1533418332325
},
{
"title": "1533377102405-add-images.js",
"timestamp": 1533418332609
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

const fs = require('fs');
const path = require('path');
const upSQL = fs.readFileSync(path.join(__dirname, '1533377087768-add-users-up.sql')).toString();
const downSQL = fs.readFileSync(path.join(__dirname, '1533377087768-add-users-down.sql')).toString();
const dbLoc = require('../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../db');
const upSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377087768-add-users-up.sql')).toString();
const downSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377087768-add-users-down.sql')).toString();

const dbLoc = require('../../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../');
Database.connect(dbLoc);

module.exports.up = function (next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const fs = require('fs');
const path = require('path');
const upSQL = fs.readFileSync(path.join(__dirname, '1533377095426-add-sequences-up.sql')).toString();
const downSQL = fs.readFileSync(path.join(__dirname, '1533377095426-add-sequences-down.sql')).toString();
const upSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377095426-add-sequences-up.sql')).toString();
const downSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377095426-add-sequences-down.sql')).toString();

const dbLoc = require('../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../db');
const dbLoc = require('../../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../');
Database.connect(dbLoc);

module.exports.up = function (next) {
Expand Down
22 changes: 22 additions & 0 deletions db/migrations/1533377102405-add-images.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const fs = require('fs');
const path = require('path');
const upSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377102405-add-images-up.sql')).toString();
const downSQL = fs.readFileSync(path.join(__dirname, '..', 'sql', '1533377102405-add-images-down.sql')).toString();

const dbLoc = require('../../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../');
Database.connect(dbLoc);

module.exports.up = function (next) {
Database.executeSpatial(upSQL)
.then(() => next())
.catch(error => { console.error(error); next(); })
}

module.exports.down = function (next) {
Database.executeSpatial(downSQL)
.then(() => next())
.catch(error => { console.error(error); next(); });
}
26 changes: 26 additions & 0 deletions db/seeds/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

const users = require('../../testData/seeds').users;
const uuidv4 = require('uuid/v4');
const dbLoc = require('../../config')[process.env.ENVIRONMENT || 'develop'].db;
const Database = require('../');

Database.connect(dbLoc);

let seeds = [
{
users: () => {
const values = users.map(user => `('${uuidv4()}', '${user.name}')`).join(',');
const sql = `INSERT INTO Users VALUES ${values};`;
return Database.execute(sql)
.catch((err) => { throw err; });
}
}
];

if (process.argv.slice(2).length > 0) {
seeds = seeds.filter(s => process.argv.includes(Object.keys(s)[0]));
}

seeds.forEach(s => Object.values(s)[0]());

1 change: 1 addition & 0 deletions db/sql/1533377087768-add-users-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS Users;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE Users (
CREATE TABLE IF NOT EXISTS Users(
id UUID PRIMARY KEY,
name TEXT NOT NULL
);
1 change: 1 addition & 0 deletions db/sql/1533377095426-add-sequences-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS Sequences;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE Sequences(
CREATE TABLE IF NOT EXISTS Sequences(
id UUID NOT NULL PRIMARY KEY,
userId UUID NOT NULL,
images JSON1 NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions db/sql/1533377102405-add-images-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS Images;
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CREATE TABLE Images(
SELECT InitSpatialMetaData(1);

CREATE TABLE IF NOT EXISTS Images(
id UUID NOT NULL PRIMARY KEY,
path TEXT NOT NULL,
time TEXT NOT NULL,
Expand All @@ -9,7 +11,5 @@ CREATE TABLE Images(
FOREIGN KEY(seqId) REFERENCES Sequences(id)
);

--- SpatialLite for image locations ---
SELECT AddGeometryColumn(
'Images', 'loc', 4326, 'POINT', 'XY'
);
-- SpatialLite for image locations ---
SELECT AddGeometryColumn('Images', 'loc', 4326, 'POINT', 'XY');
Binary file added db/test-posm-paths.sqlite3
Binary file not shown.
Empty file.
Empty file.
Empty file.
21 changes: 0 additions & 21 deletions migrations/1533377102405-add-images.js

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"license": "MIT",
"scripts": {
"test": "./node_modules/.bin/mocha test/**/* --exit",
"seed": "knex seed:run"
"migrate:up": "migrate up --state-file db/migrations/.migrate --migrations-dir db/migrations",
"migrate:down": "migrate down --state-file db/migrations/.migrate --migrations-dir db/migrations",
"seed": "node db/seeds/index.js",
"migrate:up:seed": "npm run migrate:up && npm run seed"
},
"dependencies": {
"bluebird": "^3.5.1",
Expand Down
16 changes: 0 additions & 16 deletions seeds/users.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ describe('db', () => {
Database
.executeSpatial(badSql)
.catch((err) => expect(err).to.be.instanceof(Error));
})
});
});
});
26 changes: 13 additions & 13 deletions test/migrations.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict';

const expect = require('chai').expect;
const migrate = require('migrate');
Promise = require('bluebird');

const expect = require('chai').expect;
const migrate = Promise.promisifyAll(require('migrate'));
const path = require('path');
const migrationsPath = path.join(process.cwd(), 'migrations');
const migrationsPath = path.join(__dirname, '..', 'db', 'migrations');
const migrationStore = path.join(migrationsPath, '.migrate');
const migrationOpts = { migrationsDirectory: migrationsPath, stateStore: migrationStore };
const migrationOpts = { migrationsDirectory: migrationsPath, stateStore: migrationStore };

describe('migrations', () => {
it('migrates db up and back down', () => {
migrate.load(migrationOpts, (err, set) => {
if (err) throw err
console.log(set);
set.up((err, res) => {
if (err) throw err;
set.down((err, res) => { if (err) throw err; })
});
});
migrate.loadAsync(migrationOpts)
.then((set) => {
set = Promise.promisifyAll(set);
set.upAsync().then(() => set.downAsync())
})
.catch((err) => console.error(err));
});
});
});

Loading

0 comments on commit 00916cb

Please sign in to comment.