<commit_before>module.exports = function(self,deps){
    var preprocessProperties = self.require('lib/properties');
    var getAppScheme = self.require('lib/getAppScheme');
    var compareDescriptors = self.require('lib/difference');
    var g = require('grunt');
    var Sync = require('sql-ddl-sync').Sync;
    var Q = require('q');
    var _ = require('underscore');
    var currentState;
    var appState;
    var dbg = true;
    var MState;
    var flagFirst = true;
    var diff;
    var mjcDir;
    var magic = {id: null, run: null};
    
    return {
        init : function(mDir, dbPath){
           mjcDir = mDir;
           var prms = self.require('lib/migrationState').init(dbPath).then(function(State){
               MState = State;
               return State.lastState();
           }).then(function(last){
               if(last.getId()) flagFirst = false;
               currentState = last;
               if(currentState.err){
                   if(dbg) console.log(currentState.err);
                   return Q.reject(new Error("Migration module: Errors in last migration state."));
               }
               return Q();
           }).then(function(){
               return MState.createState().then(function(stt){
                   appState = stt;
                   appState.setScheme(getAppScheme());
               });
           }).then(function(){
               diff = compareDescriptors(currentState.scheme, appState.scheme);
               m.diff = diff;
               if(diff.need){
                   if(flagFirst) {
                       if(dbg) console.log("First migration - magic function not need.");
                       return Q();
                   }
                   if(!currentState.isSynced()){
                       magic.id = currentState.magicId;
                   } else {
                       magic.id = currentState.getId();
                   }
                   
                   var mjcPath = mjcDir+magic.id+".js";
                   var txt = "module.exports = {\n"+
                   "function(models) {\n"+
                   "}\n"+
                   "};\n";
                   if (!g.file.exists(mjcPath)){
                       g.file.write(mjcPath, txt);
                       if(dbg) console.log("Magic file created: "+ mjcPath);
                   } else {
                       if(dbg) console.log("Magic file already exists: " + mjcPath);
                   }
                   magic.run = require(mjcPath);
                        
               } else {
                   if(dbg) console.log("Nothing to sync.");
                   return Q(new Error("Nothing to sync."));
               }
               return Q();
           });
           return prms;
        }, 
        run: function(){
            if(!diff.need){
                if(dbg) console.log("Nothing to sync");
                return Q();
            }
            if(!flagFirst && !magic.run ){
                if(dbg) console.log("no magic function");
                return Q().reject(new Error("no magic function"));
            }
            var prms = Q();
            var syncingState;
            if(currentState.isSynced()){
                prms = MState.createState().then(function(stt){
                    syncingState = stt;
                    syncingState.setScheme(currentState.getScheme());
                    syncingState.downStateId = currentState.getId();
                });
            } else {
                syncingState = currentState;
            }
            
            prms = prms.then(function(){ // ddl sync app models;
                if(dbg) console.log("ddl sync added app models ... ");
                if(_.keys(diff.ddlSyncAdd).length == 0 ){
                    if(dbg) console.log("no models to ddl-sync.");
                    return Q();
                } 
                return syncingState.ddlSync(diff.ddlSyncAdd).then(function(){
                    return deps.config.reloadScope();
                });
            }).then(function(){ // orm sync app models;
                if(dbg) console.log("orm sync app models ... ");
                if(_.keys(diff.modelSync).length == 0 ) {
                    if(dbg) console.log("no models to sync.");
                    return Q();
                }
                function one(name){
                    var df = Q.defer();
                    m.app.models[mdl].sync(function(err){ //TODO
                        if(err) df.reject(err);
                        if(dbg) console.log("Model "+mdl+" synced.");<commit_msg>reject in orm sync one model<commit_after>module.exports = function(self,deps){
    var preprocessProperties = self.require('lib/properties');
    var getAppScheme = self.require('lib/getAppScheme');
    var compareDescriptors = self.require('lib/difference');
    var g = require('grunt');
    var Sync = require('sql-ddl-sync').Sync;
    var Q = require('q');
    var _ = require('underscore');
    var currentState;
    var appState;
    var dbg = true;
    var MState;
    var flagFirst = true;
    var diff;
    var mjcDir;
    var magic = {id: null, run: null};
    
    return {
        init : function(mDir, dbPath){
           mjcDir = mDir;
           var prms = self.require('lib/migrationState').init(dbPath).then(function(State){
               MState = State;
               return State.lastState();
           }).then(function(last){
               if(last.getId()) flagFirst = false;
               currentState = last;
               if(currentState.err){
                   if(dbg) console.log(currentState.err);
                   return Q.reject(new Error("Migration module: Errors in last migration state."));
               }
               return Q();
           }).then(function(){
               return MState.createState().then(function(stt){
                   appState = stt;
                   appState.setScheme(getAppScheme());
               });
           }).then(function(){
               diff = compareDescriptors(currentState.scheme, appState.scheme);
               m.diff = diff;
               if(diff.need){
                   if(flagFirst) {
                       if(dbg) console.log("First migration - magic function not need.");
                       return Q();
                   }
                   if(!currentState.isSynced()){
                       magic.id = currentState.magicId;
                   } else {
                       magic.id = currentState.getId();
                   }
                   
                   var mjcPath = mjcDir+magic.id+".js";
                   var txt = "module.exports = {\n"+
                   "function(models) {\n"+
                   "}\n"+
                   "};\n";
                   if (!g.file.exists(mjcPath)){
                       g.file.write(mjcPath, txt);
                       if(dbg) console.log("Magic file created: "+ mjcPath);
                   } else {
                       if(dbg) console.log("Magic file already exists: " + mjcPath);
                   }
                   magic.run = require(mjcPath);
                        
               } else {
                   if(dbg) console.log("Nothing to sync.");
                   return Q(new Error("Nothing to sync."));
               }
               return Q();
           });
           return prms;
        }, 
        run: function(){
            if(!diff.need){
                if(dbg) console.log("Nothing to sync");
                return Q();
            }
            if(!flagFirst && !magic.run ){
                if(dbg) console.log("no magic function");
                return Q().reject(new Error("no magic function"));
            }
            var prms = Q();
            var syncingState;
            if(currentState.isSynced()){
                prms = MState.createState().then(function(stt){
                    syncingState = stt;
                    syncingState.setScheme(currentState.getScheme());
                    syncingState.downStateId = currentState.getId();
                });
            } else {
                syncingState = currentState;
            }
            
            prms = prms.then(function(){ // ddl sync app models;
                if(dbg) console.log("ddl sync added app models ... ");
                if(_.keys(diff.ddlSyncAdd).length == 0 ){
                    if(dbg) console.log("no models to ddl-sync.");
                    return Q();
                } 
                return syncingState.ddlSync(diff.ddlSyncAdd).then(function(){
                    return deps.config.reloadScope();
                });
            }).then(function(){ // orm sync app models;
                if(dbg) console.log("orm sync app models ... ");
                if(_.keys(diff.modelSync).length == 0 ) {
                    if(dbg) console.log("no models to sync.");
                    return Q();
                }
                function one(name){
                    var df = Q.defer();
                    m.app.models[mdl].sync(function(err){ //TODO
                        if(dbg) console.log("Syncing: "+mdl);
                        if(err) df.reject(err);
                        if(dbg) console.log("Model "+mdl+" synced.");
{'fraction_non_alphanumeric': 0.12436355757772723, 'fraction_numerical': 0.00043332250027082654, 'mean_word_length': 1.2267245537867824, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '520365', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2279, 'n_words': 491}