-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiccro-make:migration.js
70 lines (62 loc) · 1.75 KB
/
miccro-make:migration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
'use strict';
const program = require('commander'),
exec = require('child_process').exec,
fs = require('fs'),
chalk = require("chalk");
program
.option('-c, --create [table]', 'Add the table name.')
.parse(process.argv);
/*
* Get the model name an pluralize it
*/
let date = new Date();
let monthCount = date.getMonth() + 1;
let month = (monthCount.toString().length < 2) ? '0' + monthCount.toString() : monthCount.toString();
let fileName = date.getFullYear().toString()
+ month
+ date.getDate().toString()
+ date.getHours().toString()
+ date.getMinutes().toString()
+ date.getSeconds().toString()
+ '_' + program.args[0];
/*
* Stub the model
*/
let stub1 = `
exports.up = function(knex, Promise) {
// Do the migration
};
exports.down = function(knex, Promise) {
// Revert the migration
};
`.trim();
/*
* Stub the model
*/
let stub2 = `
exports.up = function(knex, Promise) {
return knex.schema.createTable('${program.create}', function(table) {
table.increments('id').primary();
table.timestamps();
});
};
exports.down = function(knex, Promise) {
return knex.schema.dropTable('${program.create}');
};
`.trim();
/*
* Decide which template should be used and
* write the template into a new file
*/
if(program.create) {
fs.writeFile('migrations/' + fileName + '.js', stub2, (err) => {
if (err) throw err;
console.log(chalk.green(`Created Migration: ${__dirname}/migrations/${fileName}.js`));
});
} else {
fs.writeFile('migrations/' + fileName + '.js', stub1, (err) => {
if (err) throw err;
console.log(chalk.green(`Created Migration: ${__dirname}/migrations/${fileName}.js`));
});
}