noppoMan / npdynamodb

A Node.js Simple Query Builder and ORM for AWS DynamoDB
112 stars 18 forks source link

Improved Migrate on OSX as it was trying to process DS_Store system files and failing #41

Closed brett--anderson closed 8 years ago

brett--anderson commented 8 years ago

Improved Migrate on OSX as it was trying to process DS_Store system files and failing. By checking that the 'version' component of the dir is actually a number, DS_Store files are ignored and migrate continues successfully. You might also consider checking that files have a '.js' suffix too before trying to process them.

noppoMan commented 8 years ago

Hi, Brett Thanks for using npdynamodb!

Ohh.. That's a good point. if I have to bring up one, It's better to make sure the file extension is .js or not as you mentioned above. Cause migration file should be a .js file.

And then I'll merge your PR. Anyways thanks for your contribution. That's a great point to improve.

Yuki

brett--anderson commented 8 years ago

Hi Yuki,

Thanks for writing it ;) I've just started trying it out and it's great to have migrations for dynamodb!

Please review the most recent commit, I'm now checking the version is an int and that the file has a '.js' extension.

Thanks, Brett

noppoMan commented 8 years ago

Thanks for your picking up. I did the test and worked fine. but I have to bring up one more... Could you unify these commits to a commit ?

After that I'll merge soon. Thanks

Yuki

brett--anderson commented 8 years ago

No problem, that should be squashed into a single commit and good to go now.

noppoMan commented 8 years ago

@brett--anderson

I merged. Thanks for your contribution! And feel free to make other improvement

brett--anderson commented 8 years ago

@noppoMan

My pleasure. A quick question; I'd like to trigger the migration whenever the server is started. Is there a simple way to programatically access the Migrator?

brett--anderson commented 8 years ago

@noppoMan

Also, any chance you could push the updated module to npm?

noppoMan commented 8 years ago

@brett--anderson

Thanks again. Yes, you can access Migrator with

const npdynamodb = require('npdynamodb');
const NpdMigrator = npdynamodb.Migrator;
const migrator = NpdMigrator.Migrator;
const migrateRunner = NpdMigrator.Runner;  // this is migrate runner you should use.

And you can see the usage of these modules at the migration section of /lib/bin/npd and test/migrator_spec.js.

any chance you could push the updated module to npm?

Yep, sure. I'll release current master as npm@0.2.10

noppoMan commented 8 years ago

@brett--anderson I released npdynamodb@0.2.10 with your fixing https://www.npmjs.com/package/npdynamodb Plz check it out.

brett--anderson commented 8 years ago

@noppoMan

Awesome! Thanks so much for the info and the npm deploy. I'm working with the latest now with no problems.

Thanks! Brett.

brett--anderson commented 8 years ago

For anyone reading this thread to work out how to programatically trigger a migration, I created a helper method with the following code (I've hacked all the require code into the function for a shorter post here):

Helper.prototype.migrate = function() {
    var program = require('commander');
    var npdynamodb = require('npdynamodb');

    var NpdMigrator = npdynamodb.Migrator;
    var MigrateRunner = NpdMigrator.Runner;
    var npdFile = require('../npdfile.js');
    var env = program.env || 'development';
    var config = npdFile[env];

    var migrationFilePath = (npdFile.migration && npdFile.migration.migrationFilePath)
        ? npdFile.migration.migrationFilePath : 'migrations';
    migrationFilePath =  process.cwd() + '/' + migrationFilePath;

    config.cwd = migrationFilePath;
    config.env = env;
    var migrateRunner = new MigrateRunner(config);

    return migrateRunner.run();
}

You can then call migration:

helper.migrate().then(function(data){
  if(data.length === 0) {
    console.log("Migrations already up to date");
  }else{
    console.log(data.map(function(path){ return "Migrated" + path; }).join("\n"));
  }
  ... start server, or unit tests, or whatever ...
});

Replace console.log with your log framework of choice.

@noppoMan : I'm not sure if you want to include something like this in the npdynamodb module for ease of access, and document it in the readme?