Skip to content
Browse files

[new feature] Add database migration tool

  • Loading branch information...
1 parent c4ae57c commit 3d63383352e53847be65743cfeba9f9e3c9d363a @ben-lin ben-lin committed Sep 6, 2012
Showing with 229 additions and 62 deletions.
  1. +7 −0 bin/coke
  2. +1 −0 lib/cli/index.js
  3. +0 −62 lib/cli/migrate.js
  4. +32 −0 lib/cli/migrate/index.js
  5. +189 −0 lib/cli/migrate/lib.js
View
7 bin/coke
@@ -55,6 +55,13 @@ switch( command ){
break;
+ case 'm':
+ case 'migrate':
+ cli.migrate( args );
+ exit = false;
+
+ break;
+
case 'h':
case 'help':
case '-h':
View
1 lib/cli/index.js
@@ -15,6 +15,7 @@ files.forEach( function ( file ){
});
cli.generators = require( './generators' );
+cli.migrate = require( './migrate' );
/**
* Exports module.
View
62 lib/cli/migrate.js
@@ -1,62 +0,0 @@
-var Class = require( 'node.class' );
-var Flow = require( 'node.flow' );
-
-module.exports = function ( file_name ){
-/**
- * Module dependencies.
- * @private
- */
- var utils = require( '../utils' );
- var set_global = require( '../global' );
-
- utils.is_project_root( function ( current ){
- set_global( current, function (){
- var mongoose = require( BASE_DIR + 'node_modules/mongoose' );
- var migration = require( BASE_DIR + 'db/migrate/' + file_name );
-
- require( CORE_DIR + 'model' )({
- mongoose : mongoose,
- schema_name : migration.ori_schema
- }, function ( model_names ){
- var export_actions = migration.export( Model );
- var Export = Class.extend( export_actions );
- var flow = new Flow();
-
- var series = function ( action ){
- if( utils.typeof( action ) !== 'function' ) return;
-
- flow.series( action );
- };
-
- var parallel = function ( action ){
- if( utils.typeof( action ) !== 'function' ) return;
-
- flow.parallel( action );
- };
-
- var join = flow.join;
-
- var export_instance = new Export( series, parallel, join,
- // end
- function ( action ){
- if( utils.typeof( action ) !== 'function' ) return;
-
- flow.series( function ( args, next ){
- require( CORE_DIR + 'model' )({
- mongoose : mongoose
- }, function ( model_names ){
- var import_actions = migration.import( Model );
- var Import = Class.extend( import_actions );
- var import_instance = new Export( series, parallel, join,
- function ( action ){
- if( utils.typeof( action ) !== 'function' ) return flow.end();;
-
- flow.end( action );
- });
- });
- });
- });
- });
- });
- });
-};
View
32 lib/cli/migrate/index.js
@@ -0,0 +1,32 @@
+module.exports = function ( file_name ){
+ var utils = require( '../../utils' );
+ var set_global = require( '../../global' );
+
+// Model is a global builder fn
+ utils.is_project_root( function ( current ){
+ set_global( current, function (){
+ var migration = require( BASE_DIR + 'db/migrate/' + file_name );
+ var cp = require( 'child_process' );
+
+ var create_child = function ( method ){
+ var child = cp.fork( __dirname + '/lib' );
+
+ child.on( 'message', function( msg ){
+ if( msg.exit === true ) child.kill();
+ if( msg.next ) create_child( msg.next );
+ });
+
+ child.send({
+ current : current,
+ file_name : file_name,
+ method : method
+ });
+ };
+
+ if( migration.modify ) return create_child( 'modify' );
+ if( migration.export ) return create_child( 'export' );
+
+ migration.import && create_child( 'import' );
+ });
+ });
+};
View
189 lib/cli/migrate/lib.js
@@ -0,0 +1,189 @@
+process.on( 'message', function ( msg ){
+ var Class = require( 'node.class' );
+ var slice = [].slice;
+ var utils = require( '../../utils' );
+ var set_global = require( '../../global' );
+
+ set_global( msg.current, function (){
+ require( CORE_DIR + 'logger' );
+ LOG.sys( 'loading core module: logger' );
+
+ var Migrate = Class.extend({
+
+ init : function ( migration, mongoose, utils ){
+ var Flow = require( 'node.flow' );
+
+ this.migration = migration;
+ this.mongoose = mongoose;
+ this.utils = utils;
+ this.model = require( CORE_DIR + 'model' );
+ this.flow = new Flow();
+ },
+
+ exit : function (){
+ process.send({ exit : true });
+ this.exit();
+ },
+
+ reset : function ( db, end ){
+ db && db.close();
+ end ? end() : this.exit();
+ },
+
+ series : function ( action ){
+ if( this.utils.typeof( action ) !== 'function' ){
+ return this.exit();
+ }
+
+ this.flow.series( action );
+ },
+
+ parallel : function ( action ){
+ if( this.utils.typeof( action ) !== 'function' ){
+ return this.exit();
+ }
+
+ this.flow.parallel( action );
+ },
+
+ modify : function (){
+ var flow = this.flow;
+ var self = this;
+ var callback = function ( model_names ){
+ console.log( self.migration );
+ var modify_actions = self.migration.modify( Model );
+ var Modify = Class.extend( modify_actions );
+
+ var series = function ( action ){
+ self.series( action );
+ };
+
+ var parallel = function ( action ){
+ self.parallel( action );
+ };
+
+ var join = function (){
+ flow.join.call( flow, slice.call( arguments ));
+ };
+
+ var end = function ( action ){
+ self.flow.end( function ( args ){
+ var db = self.utils.db();
+
+ if( self.utils.typeof( action ) !== 'function' ){
+ return self.reset( db );
+ }
+
+ action( function (){
+ self.reset( db );
+ });
+ });
+ };
+
+ new Modify( series, parallel, join, end );
+ };
+
+ this.model({ mongoose : this.mongoose }, callback );
+ },
+
+ import : function (){
+ var flow = this.flow;
+ var self = this;
+ var callback = function ( model_names ){
+ var import_actions = self.migration.import( Model );
+ var Import = Class.extend( import_actions );
+
+ var series = function ( action ){
+ self.series( action );
+ };
+
+ var parallel = function ( action ){
+ self.parallel( action );
+ };
+
+ var join = function (){
+ flow.join.call( flow, slice.call( arguments ));
+ };
+
+ var end = function ( action ){
+ self.flow.end( function ( args ){
+ var db = self.utils.db();
+
+ if( self.utils.typeof( action ) !== 'function' ){
+ return self.reset( db );
+ }
+
+ action( function (){
+ self.reset( db );
+ });
+ });
+ };
+
+ new Import( series, parallel, join, end );
+ };
+
+ this.model({ mongoose : this.mongoose }, callback );
+ },
+
+ export : function (){
+ var flow = this.flow;
+ var self = this;
+ var callback = function ( model_names ){
+ var export_actions = self.migration.export( Model );
+ var Export = Class.extend( export_actions );
+
+ var series = function ( action ){
+ self.series( action );
+ };
+
+ var parallel = function ( action ){
+ self.parallel( action );
+ };
+
+ var join = function (){
+ flow.join.call( flow, slice.call( arguments ));
+ };
+
+ var end = function ( action ){
+ var db = self.utils.db();
+
+ if( self.utils.typeof( action ) !== 'function' ){
+ return self.reset( db, function (){
+ flow.end();
+ });
+ }
+
+ if( !self.migration.import ){
+ return self.reset( db, function (){
+ flow.end( action );
+ });
+ }
+
+ self.flow.end( function ( args ){
+ action( function (){
+ self.reset( db, function (){
+ process.send({ next : 'import' });
+ self.exit();
+ });
+ });
+ });
+ };
+
+ new Export( series, parallel, join, end );
+ };
+
+ this.model({
+ mongoose : this.mongoose,
+ schema_name : this.migration.ori_schema
+ }, callback );
+ }
+ });
+
+ var mongoose = require( BASE_DIR + 'node_modules/mongoose' );
+ var migration = require( BASE_DIR + 'db/migrate/' + msg.file_name );
+ var m = new Migrate( migration, mongoose, utils );
+
+ m[ msg.method ]();
+ });
+});
+

0 comments on commit 3d63383

Please sign in to comment.
Something went wrong with that request. Please try again.