This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
ProgBlog/migrations/20160224105517_initial.js

75 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-02-24 14:01:10 -05:00
'use strict';
const helpers = require('../app/migration_helpers');
exports.up = function(knex, Promise) {
const dbType = helpers.get_db_type(knex);
// Defer has the promise resulting from creating the initial tables
let defer = knex.schema.createTableIfNotExists('users', (table) => {
table.comment('User authentication table');
table.increments()
.unsigned();
table.string('user');
table.binary('password_hash');
table.timestamp('created_at')
.defaultTo(knex.fn.now());
table.timestamp('updated_at')
.defaultTo(knex.fn.now());
}).then(() => {
return knex.schema.createTableIfNotExists('posts', (table) => {
table.comment('Blog post table');
table.increments()
.unsigned();
table.integer('created_by')
.unsigned()
.references('users.id');
table.string('uri')
.index()
.comment('The uri segment of the blog post');
table.string('title')
.comment('The title of the blog post');
table.text('content')
.comment('The content of the blog post');
table.timestamp('created_at')
.defaultTo(knex.fn.now());
table.timestamp('updated_at')
.defaultTo(knex.fn.now());
});
});
if ('pg' === dbType) {
// Create the function to update
return knex.schema.raw(helpers.pg_timestamp_update_function())
// Create the tables
.then(() => defer)
// Add trigger for tables
.then(() => knex.schema.raw(helpers.pg_create_timestamp_update_trigger('users')))
.then(() => knex.schema.raw(helpers.pg_create_timestamp_update_trigger('posts')));
} else if ('sqlite3' === dbType) {
return defer.then(() => knex.schema.raw(helpers.sqlite3_create_timestamp_update_trigger('users')))
.then(() => knex.schema.raw(helpers.sqlite3_create_timestamp_update_trigger('posts')));
} else {
return defer;
}
};
exports.down = function(knex, Promise) {
const dbType = helpers.get_db_type(knex);
let defer = knex.schema.dropTableIfExists('posts')
.then(() => knex.schema.dropTableIfExists('users'));
if ('pg' === dbType) {
return knex.schema.raw(helpers.pg_delete_timestamp_update_trigger('users'))
.then(() => knex.schema.raw(helpers.pg_delete_timestamp_update_trigger('posts')))
.then(() => defer);
} else if ('sqlite3' === dbType) {
return knex.schema.raw(helpers.sqlite3_delete_timestamp_update_trigger('users'))
.then(() => knex.schema.raw(helpers.sqlite3_delete_timestamp_update_trigger('posts')))
.then(() => defer);
} else {
return defer;
}
};