Skip to content

Commit

Permalink
Merge pull request #164 from SockDrawer/knex_migrations
Browse files Browse the repository at this point in the history
Add Knex Migrations
  • Loading branch information
AccaliaDeElementia authored Dec 7, 2018
2 parents 61d354c + 6a16d3f commit cfbd277
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 112 deletions.
1 change: 1 addition & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exclude_patterns:
- "static/js/ckeditor/"
- "db/"
- "**/test/"
- "migrations/**"
checks:
similar-code:
config:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ errorShots
.nyc_output

#Dev Mode Database File.
database.sqlite
*.sqlite
*.sqlite3

#vscode workspace files
.vscode
43 changes: 43 additions & 0 deletions knexfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict';
module.exports = {

development: {
client: 'sqlite3',
connection: {
filename: './dev.sqlite3'
},
useNullAsDefault: true
},

staging: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
},

production: {
client: 'postgresql',
connection: {
database: 'my_db',
user: 'username',
password: 'password'
},
pool: {
min: 2,
max: 10
},
migrations: {
tableName: 'knex_migrations'
}
}
};
48 changes: 48 additions & 0 deletions migrations/20181202214158_initial-database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';

exports.up = (knex, Promise) => {
return Promise.all([
knex.schema.createTable('Games', (table) => {
table.increments('ID').primary();
table.string('gameDescription');
}),
knex.schema.createTable('Users', (table) => {
table.increments('ID').primary();
table.string('Username').notNullable().unique();
table.boolean('Admin').notNullable().defaultTo(false);
table.string('AuthSecret').notNullable();
}),
knex.schema.createTable('Boards', (table) => {
table.increments('ID').primary();
//This shouldn't be nullable, but we don't have users working yet
table.integer('Owner').references('Users.ID'); //.notNullable();
table.integer('ParentID').references('Boards.ID').nullable();
table.integer('GameID').references('Games.ID').nullable();
table.string('Name').notNullable();
table.boolean('Adult').defaultTo(false);
table.string('Description').notNullable().defaultTo('');
}),
knex.schema.createTable('Threads', (table) => {
table.increments('ID').primary();
table.string('Title').notNullable();
table.integer('Board').references('Boards.ID').notNullable();
}),
knex.schema.createTable('Posts', (table) => {
table.increments('ID').primary();
table.integer('Thread').references('Threads.ID').notNullable();
table.string('Body').notNullable();
table.integer('Poster').references('Users.ID');
table.timestamps(false, true);
})
]);
};

exports.down = (knex, Promise) => {
return Promise.all([
knex.schema.dropTable('Games'),
knex.schema.dropTable('Users'),
knex.schema.dropTable('Boards'),
knex.schema.dropTable('Threads'),
knex.schema.dropTable('Posts')
]);
};
15 changes: 15 additions & 0 deletions migrations/20181204124807_user-profiles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

exports.up = (knex) => {
return knex.schema.alterTable('Users', (table) => {
table.string('DisplayName');
table.string('Avatar').defaultTo('');
});
};

exports.down = (knex) => {
return knex.schema.alterTable('Users', (table) => {
table.dropColumn('DisplayName');
table.dropColumn('Avatar');
});
};
31 changes: 18 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"express-session": "^1.15.1",
"express-validator": "^5.2.0",
"knex": "^0.15.1",
"lodash.merge": "^4.6.1",
"moment": "^2.22.2",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
Expand Down Expand Up @@ -71,7 +72,8 @@
"prepush": "npm test",
"postmerge": "npm install",
"postrewrite": "npm install",
"postcheckout": "npm install"
"postcheckout": "npm install",
"knex": "knex"
},
"config": {
"commitizen": {
Expand All @@ -85,7 +87,8 @@
"**/node_modules/**",
"static/**",
"mockups/**",
"*.js"
"*.js",
"migrations/**"
],
"per-file": true,
"watermarks": {
Expand Down
76 changes: 12 additions & 64 deletions src/model/db.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,7 @@
'use strict';

let knex = 'banana';

const ensureKnexTables = async(knexdb, tables) => {
for (let i = 0; i < tables.length; i++) {
const [name, creator] = tables[i];

const exists = await knexdb.schema.hasTable(name);
if (!exists) {
await knexdb.schema.createTable(name, creator);
}
}
};
const merge = require('lodash.merge');
let knex = null;

const db = {
initialized: false,
Expand All @@ -20,58 +10,16 @@ const db = {
if (db.initialised) {
return Promise.resolve();
}

knex = require('knex')({
client: 'sqlite3',
connection: {
filename: config.database.filename
},
useNullAsDefault: true
});


const tables = [
['Games', (table) => {
table.increments('ID').primary();
table.string('gameDescription');
}],
['Users', (table) => {
table.increments('ID').primary();
table.string('Username').notNullable().unique();
table.string('DisplayName');
table.string('Avatar').defaultTo('');
table.boolean('Admin').notNullable().defaultTo(false);
table.string('AuthSecret').notNullable();
}],
['Boards', (table) => {
table.increments('ID').primary();
//This shouldn't be nullable, but we don't have users working yet
table.integer('Owner').references('Users.ID'); //.notNullable();
table.integer('ParentID').references('Boards.ID').nullable();
table.integer('GameID').references('Games.ID').nullable();
table.string('Name').notNullable();
table.boolean('Adult').defaultTo(false);
table.string('Description').notNullable().defaultTo('');
}],
['Threads', (table) => {
table.increments('ID').primary();
table.string('Title').notNullable();
table.integer('Board').references('Boards.ID').notNullable();
}],
['Posts', (table) => {
table.increments('ID').primary();
table.integer('Thread').references('Threads.ID').notNullable();
table.string('Body').notNullable();
table.integer('Poster').references('Users.ID');
table.timestamps(false, true);
}]
];

return ensureKnexTables(knex, tables)
.then(() => {
db.initialised = true;
return Promise.resolve(db.initialised);
});

const environment = process.env.DB_ENVIRONMENT || 'development';
let connection = require('../../knexfile.js')[environment];
if (config && config.database) {
connection = merge(connection, config.database);
}
knex = require('knex')(connection);
await knex.migrate.latest();
db.initialised = true;
return db.initialised;
},

/**
Expand Down
8 changes: 4 additions & 4 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,10 @@ if (require.main === module) {

//TODO: Make this read from a file
setup({
database: {
engine: 'sqlite3',
filename: './db/database.sqlite'
},
// This will read from knexfile.js and select configuration based on the
// value of the environment variable `DB_ENVIRONMENT`
// If set it should be set to a valid knexfile configuration stanza
database: null,
http: {
// eslint-disable-next-line no-process-env
port: process.env.PORT || 9000
Expand Down
6 changes: 5 additions & 1 deletion test/integration/APITests.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ context('API server', function() {
return Promise.resolve()
.then(() => server.setup({
database: {
filename: ':memory:'
client: 'sqlite3',
connection: {
filename: ':memory:'
},
useNullAsDefault: true
},
http: {
port: 9000
Expand Down
6 changes: 5 additions & 1 deletion test/unit/models/boardTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('Board model', function() {
})
.then(() => DB.initialise({
database: {
filename: ':memory:'
client: 'sqlite3',
connection: {
filename: ':memory:'
},
useNullAsDefault: true
}
}));
});
Expand Down
Loading

0 comments on commit cfbd277

Please sign in to comment.