Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for MySQL and finished the CLI script #7

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 100 additions & 68 deletions bin/cli
Original file line number Diff line number Diff line change
Expand Up @@ -15,92 +15,124 @@ var debug = require('debug')('loopback-component-migrate');
var program = require('commander');

program
.version(require(path.join(__dirname, '..', './package.json')).version)
.option('-d, --migrations-dir <path>', 'set migrations directory. defaults to ./migrations')
.option('-s, --server <path>', 'set server path. defaults to ./server/server.js');
.version(require(path.join(__dirname, '..', './package.json')).version)
.option('-d, --migrations-dir <path>', 'set migrations directory. defaults to ./server/migrations')
.option('-s, --server <path>', 'set server path. defaults to ./server/server.js');

program
.command('create [name]')
.description('create a new migration script')
.action(function(name, options){
.command('create [name]')
.description('create a new migration script')
.action(function(name, options){

function stringifyAndPadLeading(num) {
var str = num + '';
return (str.length === 1) ? '0' + str : str;
}
function stringifyAndPadLeading(num) {
var str = num + '';
return (str.length === 1) ? '0' + str : str;
}

function generateFileName(name) {
var d = new Date(),
year = d.getFullYear() + '',
month = stringifyAndPadLeading(d.getMonth()+1),
day = stringifyAndPadLeading(d.getDate()),
hours = stringifyAndPadLeading(d.getHours()),
minutes = stringifyAndPadLeading(d.getMinutes()),
seconds = stringifyAndPadLeading(d.getSeconds()),
dateString = year + month + day + hours + minutes + seconds,
fileName = dateString + (name ? '-' + name : '') + '.js';
return fileName;
}
function generateFileName(name) {
var d = new Date(),
year = d.getFullYear() + '',
month = stringifyAndPadLeading(d.getMonth()+1),
day = stringifyAndPadLeading(d.getDate()),
hours = stringifyAndPadLeading(d.getHours()),
minutes = stringifyAndPadLeading(d.getMinutes()),
seconds = stringifyAndPadLeading(d.getSeconds()),
dateString = year + month + day + hours + minutes + seconds,
fileName = dateString + (name ? '-' + name : '') + '.js';
return fileName;
}

function getMigrationsDir () {
var dir = path.join(process.cwd(), 'migrations');
debug('Using migrations directory: %s', dir);
return dir;
}
function getMigrationsDir () {
var dir = path.join(process.cwd(), 'server/migrations');
debug('Using migrations directory: %s', dir);
return dir;
}

function ensureDirectory (dir) {
debug('Preparing migrations directory: %s', dir);
mkdirp.sync(dir);
}
function ensureDirectory (dir) {
debug('Preparing migrations directory: %s', dir);
mkdirp.sync(dir);
}

function writeFile (fileName, contents) {
debug('Creating migration script: %s', fileName);
var migrationsDir = getMigrationsDir();
ensureDirectory(migrationsDir);
var filePath = path.join(migrationsDir, fileName);
fs.writeFileSync(filePath, contents);
}
function writeFile (fileName, contents) {
debug('Creating migration script: %s', fileName);
var migrationsDir = getMigrationsDir();
ensureDirectory(migrationsDir);
var filePath = path.join(migrationsDir, fileName);
fs.writeFileSync(filePath, contents);
}

// Create the migration file.
var fileName = generateFileName(name);
var migrationsDir = path.join(process.cwd(), 'migrations');
console.log('Creating migration script %s in %s', fileName, migrationsDir);
// Create the migration file.
var fileName = generateFileName(name);
var migrationsDir = getMigrationsDir();
console.log('Creating migration script %s in %s', fileName, migrationsDir);

var filePath = path.join(migrationsDir,fileName);
var fileContent = fs.readFileSync(path.join(__dirname, '..', 'migration-skeleton.js'));
writeFile(fileName, fileContent);
});
var fileContent = fs.readFileSync(path.resolve(path.join(__dirname, '../', 'migration-skeleton.js')));
writeFile(fileName, fileContent);
});

program
.command('migrate <to>')
.alias('up')
.description('Migrate to the given migration')
.action(function(to, options){
console.log('Migrating up to: "%s" [TODO]', to);
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(process.cwd(), server));
}).on('--help', function() {
.command('up [to]')
.description('Run all pending migrations')
.action(function(to, options){
console.log('Migrating up...');
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(server));
app.on('booted', function() {
app.models['Migration'].on('complete', function(err) {
if (err) {
console.error(err);
process.exit(1);
}
else {
console.log('Migrations done');
process.exit(0);
}
});
app.models['Migration'].migrate('up', to, function(err) {
if (err) {
console.error(err);
process.exit(1);
}
});
});
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ migrate 005');
console.log(' $ up 005');
console.log(' $ migrate up');
console.log(' $ migrate up 023');
console.log();
});
});

program
.command('rollback <to>')
.alias('down')
.description('Rollback to the given migration')
.action(function(to, options){
console.log('Rolling back to: "%s" [TODO]', to);
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(process.cwd(), server));
}).on('--help', function() {
.command('down <to>')
.description('Rollback to the given migration')
.action(function(to, options){
console.log('Rolling back to: "%s"', to);
var server = program.server || process.cwd() + '/server/server.js';
var app = require(path.resolve(server));
app.on('booted', function() {
app.models['Migration'].on('complete', function(err) {
if (err) {
console.error(err);
process.exit(1);
}
else {
console.log('Rollback done');
process.exit(0);
}
});
app.models['Migration'].migrate('down', to, function(err) {
if (err) {
console.error(err);
process.exit(1);
}
});
});
}).on('--help', function() {
console.log(' Examples:');
console.log();
console.log(' $ rollback 001');
console.log(' $ down 001');
console.log(' $ migrate down 001');
console.log();
});
});

program.parse(process.argv);
8 changes: 5 additions & 3 deletions lib/models/migration-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"comments": "Mapping type.",
"required": true
},
"from": {
"from_": {
"type": "String",
"comments": "Source Id.",
"required": true
Expand All @@ -28,8 +28,10 @@
"type_from_index": {
"keys": {
"type": 1,
"from": 1
}
"from_": 1
},
"columns": "`type`, `from_`",
"kind": "UNIQUE"
}
}
}
4 changes: 3 additions & 1 deletion lib/models/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
},
"indexes": {
"name_index": {
"name": 1
"name": 1,
"columns": "name",
"kind": "UNIQUE"
}
},
"methods": {
Expand Down
4 changes: 2 additions & 2 deletions migration-skeleton.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
up: function(dataSource, next) {
up: function(app, next) {
next();
},
down: function(dataSource, next) {
down: function(app, next) {
next();
}
};
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "loopback-component-migrate",
"description": "Migration framework for Loopback.",
"version": "0.1.2",
"version": "0.1.3",
"author": {
"name": "Tom Kirkpatrick @mrfelton"
},
Expand Down Expand Up @@ -51,9 +51,6 @@
"debug": "^2.2.0",
"mkdirp": "^0.5.1"
},
"peerDependencies": {
"loopback": ">=2.22.0"
},
"devDependencies": {
"bluebird": "latest",
"chai": "latest",
Expand Down