1 |
|
2 | const _ = require('lodash');
|
3 | const async = require('async');
|
4 |
|
5 | const fs = require('fs');
|
6 |
|
7 | const config = require('../src/config');
|
8 | const db = require('../src/db/db');
|
9 | const plugins = require('../src/plugins');
|
10 |
|
11 |
|
12 | const verbs = {
|
13 |
|
14 |
|
15 | migrate: function(args, callback) {
|
16 | config.mysql.debugsql = false;
|
17 | db.migrate(callback);
|
18 | },
|
19 |
|
20 |
|
21 | migrations: function(args, callback) {
|
22 | config.mysql.debugsql = false;
|
23 | db.migrations(function(err, migrations) {
|
24 | migrations = _.reverse(migrations);
|
25 | _.each(migrations, function(migration) {
|
26 | console.log([
|
27 | migration.id,
|
28 | (migration.success ? 'OK' : 'KO'),
|
29 | migration.file
|
30 | ].join(' '));
|
31 | });
|
32 | callback(err);
|
33 | });
|
34 | },
|
35 |
|
36 |
|
37 | reset: function(args, callback) {
|
38 | const stdin = process.openStdin();
|
39 | const database = config.mysql.database;
|
40 |
|
41 | console.log('WARNING: Database will be reset, data will be lost!');
|
42 | console.log('Confirm the database name (' + config.mysql.database + '):');
|
43 | stdin.addListener('data', function(d) {
|
44 | d = d.toString().trim();
|
45 | if (d !== database) {
|
46 | return callback('Cancelled.');
|
47 | }
|
48 |
|
49 | const DROP_DATABASE = 'DROP DATABASE IF EXISTS `' + database + '`;';
|
50 | const CREATE_DATABASE = 'CREATE DATABASE `' + database + '`;';
|
51 |
|
52 | config.mysql.debugsql = false;
|
53 | config.mysql.database = null;
|
54 | db.init();
|
55 | db.query(DROP_DATABASE, function() {
|
56 | db.query(CREATE_DATABASE, function() {
|
57 | config.mysql.database = database;
|
58 | db.init();
|
59 | db.migrate(function() {
|
60 | callback();
|
61 | });
|
62 | });
|
63 | });
|
64 | });
|
65 | },
|
66 |
|
67 | reverse: function(args, callback) {
|
68 | db.query('show tables', function(err, tables) {
|
69 | async.eachSeries(tables, function(table, callback) {
|
70 | table = _.values(table)[0];
|
71 | if (table === '__db_migrations') {
|
72 | return callback();
|
73 | }
|
74 | const object = _.capitalize(table.substring(0, table.length - 1));
|
75 | db.query(`explain ${table}`, function(err, fields) {
|
76 | const primary = _.chain(fields).filter({ Key: 'PRI' }).map('Field').join('\', \'');
|
77 | let lines = [
|
78 | '',
|
79 | 'const Model = require(\'igo\').Model;',
|
80 | '',
|
81 | 'const schema = {',
|
82 | ' table: \'' + table + '\',',
|
83 | ' primary: [ \'' + primary + '\' ],',
|
84 | ' columns: ['
|
85 | ];
|
86 | fields.forEach(function(field) {
|
87 | lines.push(` '${field.Field}',`);
|
88 | });
|
89 | lines = lines.concat([
|
90 | ' ],',
|
91 | ' associations: () => [',
|
92 | ' ], ',
|
93 | ' scopes: {',
|
94 | ' }',
|
95 | '};',
|
96 | '', '',
|
97 | `class ${object} extends Model(schema) {`,
|
98 | `}`, '', '',
|
99 | `module.exports = ${object};`
|
100 | ]);
|
101 |
|
102 | const file = `./app/models/${object}.js`;
|
103 | console.log('wrote ' + file);
|
104 | fs.writeFile(file, lines.join('\n'), callback);
|
105 | });
|
106 | }, callback);
|
107 | });
|
108 | }
|
109 |
|
110 | };
|
111 |
|
112 |
|
113 | module.exports = function(argv) {
|
114 | var args = argv._;
|
115 | config.init();
|
116 | db.init();
|
117 | plugins.init();
|
118 |
|
119 | if (args.length > 1 && verbs[args[1]]) {
|
120 | verbs[args[1]](args, function(err) {
|
121 | console.log(err || 'Done.');
|
122 | process.exit(0);
|
123 | });
|
124 | } else {
|
125 | console.error('ERROR: Wrong options');
|
126 | console.error('Usage: igo db [migrate|migrations|reverse|reset]')
|
127 | }
|
128 |
|
129 |
|
130 | };
|