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

25 custom template #35

Open
wants to merge 9 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
27 changes: 27 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ module.exports = {
}
```

### Custom templates
You may specify a custom template file as an option.

```js
migration.init(
connection,
__dirname + '/migrations',
function() {},
[
"--template migration_template.js"
]
);
```

Use the `${{ args.up }}` tag to indicate where the `up` content passed on the command line should be placed.

```js
module.exports = {
'up' : function (conn, cb) {
conn.query ("${{ args.up }}", function (err, res) {
cb();
});
},
'down' : ""
}
```

## Executing Migrations
There are few ways to run migrations.

Expand Down
5 changes: 3 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
'table' : 'mysql_migrations_347ertt3e',
'migrations_types' : ['up', 'down']
'table': 'mysql_migrations_347ertt3e',
'migrations_types': ['up', 'down'],
'template': undefined
};
28 changes: 19 additions & 9 deletions core_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,34 @@ var fileFunctions = require('./file');
var queryFunctions = require('./query');
var colors = require('colors');
var exec = require('child_process').exec;
var table = require('./config')['table'];
var config = require('./config');
var table = config['table'];

function add_migration(argv, path, cb) {
fileFunctions.validate_file_name(argv[4]);
fileFunctions.readFolder(path, function (files) {
var file_name = Date.now() + "_" + argv[4];
var file_path = path + '/' + file_name + '.js';
var content;

if (config.template) {
content = fs.readFileSync(config.template, { encoding: 'utf-8' });
let up = '';
if (argv.length > 5) up = argv[5];
content = content.replace(/\$\{\{ args\.up \}\}/i, up);
} else {
var sql_json = {
up: '',
down: ''
};

if (argv.length > 5) {
sql_json['up'] = argv[5];
}

var sql_json = {
up : '',
down : ''
};

if (argv.length > 5) {
sql_json['up'] = argv[5];
content = 'module.exports = ' + JSON.stringify(sql_json, null, 4);
}

var content = 'module.exports = ' + JSON.stringify(sql_json, null, 4);
fs.writeFile(file_path, content, 'utf-8', function (err) {
if (err) {
throw err;
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function migration(conn, path, cb, options) {
if (options.indexOf("--update-schema") > -1) {
updateSchema = true;
}

options.filter(option => option.startsWith('--template ')).forEach(option => {
config.template = option.split(' ', 2)[1];
fs.accessSync(config.template, fs.constants.F_OK);
});
}

queryFunctions.run_query(conn, "CREATE TABLE IF NOT EXISTS `" + table + "` (`timestamp` varchar(254) NOT NULL UNIQUE)", function (res) {
Expand Down Expand Up @@ -94,6 +99,8 @@ function handle(argv, conn, path, cb) {
else {
throw new Error('command not found : ' + argv.join(" "));
}
} else {
cb();
}
}

Expand Down
29 changes: 28 additions & 1 deletion test/core_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,42 @@ var coreFunctions = require('../core_functions');
var testCommons = require('./test_commons');
var mysql = require('./mysql');
var assert = require('assert');
var config = require('../config');

var should = chai.should();

describe('core_functions.js', function() {
before(function (done) {
beforeEach(function (done) {
testCommons(done);
});

context('add_migration', function () {
it('uses custom template with up SQL', function (done) {
config.template = __dirname + '/migrations/test-template.js';
fs.writeFileSync(config.template, 'Test: ${{ args.up }}', { encoding: 'utf-8' });
var sql = `SELECT 'MySqlHere'`;
var commands = ['node', 'migration', 'add', 'migration', 'test_custom_template_with_up', sql];
var path = __dirname + '/migrations';
coreFunctions.add_migration(commands, path, function () {
var fileName = fs.readdirSync(path)[0];
var contents = fs.readFileSync(path + '/' + fileName, { encoding: 'utf-8' });
assert.equal(contents, `Test: ${sql}`, "SQL tag replaced with SQL");
done();
});
});
it('uses custom template without up SQL', function (done) {
config.template = __dirname + '/migrations/test-template.js';
fs.writeFileSync(config.template, 'Test: ${{ args.up }}', { encoding: 'utf-8' });
var commands = ['node', 'migration', 'add', 'migration', 'test_custom_template_no_sql'];
var path = __dirname + '/migrations';
coreFunctions.add_migration(commands, path, function () {
var fileName = fs.readdirSync(path)[0];
var contents = fs.readFileSync(path + '/' + fileName, { encoding: 'utf-8' });
assert.equal(contents, `Test: `, "SQL tag replaced with blank");
done();
});
});

it('should add migration', function (done) {
var commands = ['node', 'migration', 'add', 'migration', 'create_user2'];
var path = __dirname + '/migrations';
Expand Down
37 changes: 37 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var migrations = require('../index');
var testCommons = require('./test_commons');
var mysql = require('./mysql');
var assert = require('assert');
var fs = require('fs');
var config = require('../config');

var path = __dirname + '/migrations';

describe('index.js', function () {
before(function (done) {
testCommons(done);
});

context('init', function () {
it('reads template option', function (done) {
var name = __dirname + '/migrations/template_17c2387b-38af-4ef6-bf6f-bb72f68255ff.js';
fs.writeFileSync(name, 'this is a template', { encoding: 'utf-8' });
mysql.getConnection(function (err, connection) {
if (err) throw err;
migrations.init(
mysql,
path,
function () {
assert.equal(name, config.template);
done();
},
[
`--template ${name}`
]
)
});

})
});

});
4 changes: 3 additions & 1 deletion test/test_commons.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var mysql = require('./mysql');
var fs = require('fs');
var config = require('../config');

function deleteFolderRecursive(path) {
if (fs.existsSync(path)) {
Expand All @@ -15,7 +16,8 @@ function deleteFolderRecursive(path) {
}

module.exports = function(cb) {
mysql.getConnection(function(err, connection) {
config.template = undefined;
mysql.getConnection(function (err, connection) {
if (err) {
throw err;
}
Expand Down