Reduce boilerplate code when running migrations from SQL files
See original GitHub issueWhen sql-file
option is used it creates the following migration file:
'use strict';
var dbm;
var type;
var seed;
var fs = require('fs');
var path = require('path');
var Promise;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};
exports.up = function(db) {
var filePath = path.join(__dirname, 'sqls', '20161226111110-test-up.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports.down = function(db) {
var filePath = path.join(__dirname, 'sqls', '20161226111110-test-down.sql');
return new Promise( function( resolve, reject ) {
fs.readFile(filePath, {encoding: 'utf-8'}, function(err,data){
if (err) return reject(err);
console.log('received data: ' + data);
resolve(data);
});
})
.then(function(data) {
return db.runSql(data);
});
};
exports._meta = {
"version": 1
};
This is a huge amount of non-optimized boilerplate code.
It’s pretty common to have hundreds of migrations in actively developed project. Now, consider this boilerplate multiplied by one hundred. This will increase code base size significantly and will take additional time for IDE to analyze the code and build it’s indices.
Please, consider to reduce the size of such migration files. I think the only relevant part of it is: __dirname, 'sqls', '20161226111110-test-down.sql'
, so everything else could be moved to the library code.
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Commands - db-migrate - Read the Docs
The create command creates templates for you, there are several options for this. This is useful if you start a new migration, thus...
Read more >How to write & run database migration in Golang
The first one is create , which we can use to create new migration files. The 2nd one is goto , which will...
Read more >Using no frameworks at all, only https://github.com ...
I use Rails migrations, via the standalone_migrations gem, and code-generate the basic CRUD Go stuff from the schema. I've tried a bunch of...
Read more >Migration Style Guide - GitLab Docs
This generates the migration file in db/migrate . Regular schema migrations to add new models. To create a new model you can use...
Read more >How to force a new empty EF Migration? - Stack Overflow
For this purpose I place all the sql code that i have to execute in scripts files and the execute then in the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@maxsummers this was driving me nuts so I pulled the boilerplate out into a separate package that you can use:
https://www.npmjs.com/package/db-migrate-boilerplate
This simplifies what you’re committing into your repo to:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.