question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reduce boilerplate code when running migrations from SQL files

See original GitHub issue

When 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.

<bountysource-plugin> --- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/40399864-reduce-boilerplate-code-when-running-migrations-from-sql-files?utm_campaign=plugin&utm_content=tracker%2F73887&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F73887&utm_medium=issues&utm_source=github). </bountysource-plugin>

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
troygoodecommented, Jan 2, 2017

@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:

'use strict'
 
const path = require('path')
const boilerplate = require('db-migrate-boilerplate')
 
module.exports = boilerplate({
  upPath: path.join(__dirname, 'sqls', '20161226111110-test-up.sql'),
  downPath: path.join(__dirname, 'sqls', '20161226111110-test-down.sql')
})
0reactions
stale[bot]commented, Nov 23, 2017

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found