forked from keswanikaran/cassanova-migrate
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapp.js
147 lines (134 loc) · 4.83 KB
/
app.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env node
/*jslint node: true */
"use strict";
var program = require('commander');
var Common = require('./util/common');
var fs = require('fs');
var DB = require('./util/database');
const Helpers = require ('./util/helpers');
/**
* Usage information.
*/
var usage = [
'',
' example : ',
'',
' cassandra-migrate <command> [options]',
'',
' cassandra-migrate up -k <keyspace> (Runs All pending cassandra migrations)',
'',
' cassandra-migrate down -k <keyspace> (Rolls back a single cassandra migration)',
'',
' cassandra-migrate <up/down> -n <migration_number>. (Runs cassandra migrations UP or DOWN to a particular migration number).',
'',
' cassandra-migrate <up/down> -k <keyspace> -s <migration_number> (skips a migration, either adds or removes the migration from the migration table)',
'',
' cassandra-migrate create <migration_name>. (Creates a new cassandra migration)',
'',
' cassandra-migrate create <migration_name> -t <template> (Creates a new cassandra migrate but uses a specified template instead of default).',
'',
].join('\n');
program.on('--help', function () {
console.log(usage);
});
program
.version(JSON.parse(fs.readFileSync(__dirname + '/package.json', 'utf8')).version)
.option('-k, --keyspace "<keyspace>"', "The name of the keyspace to use.")
.option('-H, --hosts "<host,host>"', "Comma seperated host addresses. Default is [\"localhost\"].")
.option('-u, --username "<username>"', "database username")
.option('-p, --password "<password>"', "database password")
.option('-o, --optionFile "<pathToFile>"', "pass in a javascript option file for the cassandra driver, note that certain option file values can be overridden by provided flags")
;
program.name = 'cassandra-migrate';
program
.command('create <title>')
.description('initialize a new migration file with title.')
.option('-t, --template "<template>"', "sets the template for create")
.action((title, options) => {
let Create = require('./commands/create');
let create = new Create(fs, options.template, Helpers.getDir(options));
create.newMigration(title);
process.exit(0);
});
program
.command('up')
.description('run pending migrations')
.option('-n, --num "<number>"', 'run migrations up to a specified migration number')
.option('-s, --skip "<number>"', "adds the specified migration to the migration table without actually running it", false)
.action((options) => {
let db = new DB(program);
let common = new Common(fs, db);
const dir = Helpers.getDir(options);
common.createMigrationTable()
.then(common.getMigrationFiles(dir))
.then(() => common.getMigrations())
.then(() => common.getMigrationSet('up', options.num))
.then((migrationLists) => {
let Up = require('./commands/up');
let up = new Up(db, migrationLists, dir);
if (!options.skip) {
console.log('processing migration lists');
console.log(migrationLists);
}
up.runPending(options.skip)
.then(result => {
console.log(result);
process.exit(0);
}, error => {
console.log(error);
process.exit(1);
});
})
.catch(error => {
console.log(error);
process.exit(1);
});
});
program
.command('down')
.description('roll back already run migrations')
.option('-n, --num "<number>"', 'rollback migrations down to a specified migration number')
.option('-s, --skip "<number>"', "removes the specified migration from the migration table without actually running it", false)
.action((options) => {
let db = new DB(program);
let common = new Common(fs, db);
const dir = getDir(program);
common.createMigrationTable()
.then(common.getMigrationFiles(dir))
.then(() => common.getMigrations())
.then(() => common.getMigrationSet('down', options.num))
.then((migrationLists) => {
console.log('processing migration lists');
let Down = require('./commands/down');
let down = new Down(db, migrationLists);
if (!options.skip) {
console.log('processing migration lists');
console.log(migrationLists);
}
down.runPending(options.skip)
.then(result => {
console.log(result);
process.exit(0);
}, error => {
console.log(error);
process.exit(1);
});
})
.catch(error => {
console.log(error);
process.exit(1);
});
});
/*
//@TODO: add this functionality so that a cql client isn't directly required
program
.command('run')
.description('run cql directly')
.option('-f, --files', 'run cql commands from file', true)
.action(function(options){
var Run = new require('../commands/run')(options);
Run.cql();
process.exit(0);
});
*/
program.parse(process.argv);