Skip to content

Reduce boilerplate code when running migrations from SQL files #452

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

Closed
maxsummers opened this issue Dec 26, 2016 · 5 comments
Closed

Reduce boilerplate code when running migrations from SQL files #452

maxsummers opened this issue Dec 26, 2016 · 5 comments
Labels

Comments

@maxsummers
Copy link

maxsummers commented Dec 26, 2016

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.

--- 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).
@wzrdtales
Copy link
Member

Duplicate of #401

@wzrdtales
Copy link
Member

@maxsummers Please have a look in the other issue I linked above and read my comments over there, I have already stated about some things that need to be in place to actually do what you ask for.

Fact is: SQL migrations currently do not exist, there is just a template and you basically still use javascript migrations. So yes this is true, but this is a migration framework that started with just javascript, so this is nothing that would be unexpected. So far there are plans to support raw SQL in a more native way, just like this is true now for es6 or coffeescript, for SQL as it does not use the abstractions and is not getting transpiled, there needs to be a few more things to be done and thought about though. As said above take a look over there in the other issue.

@troygoode
Copy link

@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')
})

@wzrdtales
Copy link
Member

wzrdtales commented Jan 5, 2017

@troygoode :) +1

I've made some thoughts about that again, and it might be quite a good option to handle the word sqls as a keyword that can not be used to name a scope, just like all. That way the problem described in the other issue wouldn't exist anymore and a name schema like the already existing ones would work to directly execute sqls.

@stale
Copy link

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.

@stale stale bot added the stale label Nov 23, 2017
@stale stale bot closed this as completed Nov 30, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants