var internals = {};

internals.mod = internals.mod || {};
internals.mod.log = require('../log');
internals.mod.type = require('../data_type');
var Shadow = require('./shadow');
var log = internals.mod.log;
var tunnel = require('tunnel-ssh'),
    Promise = require('bluebird'),
    SeederInterface = require('../interface/seederInterface.js'),
    MigratorInterface = require('../interface/migratorInterface.js');

var ShadowProto = {

  createTable: function() { return Promise.resolve(); },
  addForeignKey: function() { return Promise.resolve(); },
  createCollection: function() { return Promise.resolve(); }
};

exports.connect = function (config, intern, callback) {
  var driver, req;

  var mod = internals.mod;
  internals = intern;
  internals.mod = mod;

  //add interface extensions to allow drivers to add new methods
  internals.interfaces = {

    SeederInterface: SeederInterface.extending,
    MigratorInterface: MigratorInterface.extending
  };

  if ( !config.user && config.username )
    config.user = config.username;

  if (config.driver === undefined) {
    throw new Error(
      'config must include a driver key specifing which driver to use');
  }

  if (config.driver && typeof (config.driver) === 'object') {

    log.verbose('require:', config.driver.require);
    driver = require(config.driver.require);

  }
  else {

    switch(config.driver) {
      case 'sqlite':
        config.driver = 'sqlite3';
      break;

      case 'postgres':
      case 'postgresql':
        config.driver = 'pg';
      break;
    }

    try {

      req = 'db-migrate-' + config.driver;
      log.verbose('require:', req);

      try {

        driver = require(req);
      }
      catch (Exception) {

        driver = require('../../../' + req);
      }
    }
    catch (Exception) {

      try {

        //Fallback to internal drivers, while moving drivers to new repos
        req = './' + config.driver;
        log.verbose('require:', req);
        driver = require(req);
      }
      catch (Exception) {

        return callback( { stack: 'No such driver found, please try to install it via ' +
          'npm install db-migrate-' + config.driver + ' or ' +
          'npm install -g db-migrate-' + config.driver  } );
      }
    }
  }

  log.verbose('connecting');

  var connect = function(config) {
    driver.connect(config, intern, function (err, db) {

      if (err) {

        callback(err);
        return;
      }
      log.verbose('connected');

      if (!global.immunity)
        db = Shadow.infect(db, internals, ShadowProto);

      callback(null, db);
    });
  };

  if (config.tunnel) {
    var tunnelConfig = JSON.parse(JSON.stringify(config.tunnel));
    tunnelConfig.dstHost = config.host;
    tunnelConfig.dstPort = config.port;

    if (tunnelConfig.privateKeyPath) {
      tunnelConfig.privateKey = require('fs').readFileSync(tunnelConfig.privateKeyPath);
    }

    // Reassign the db host/port to point to our local ssh tunnel
    config.host = '127.0.0.1';
    config.port = tunnelConfig.localPort;

    tunnel(tunnelConfig, function (err) {

      if (err) {
        callback(err);
        return;
      }
      log.verbose('SSH tunnel connected on port ', tunnelConfig.localPort);

      connect(config);
    });
  }
  else {
    connect(config);
  }
};

@@ -403,16 +403,52 @@
 face.js'
+),%0A    resolve = require( 'resolve' 
 );%0A%0Avar 
@@ -1366,16 +1366,32 @@
 uire);%0A%0A
+    _connect();%0A
   %7D%0A  el
@@ -1592,29 +1592,16 @@
 %0A    %7D%0A%0A
-    try %7B%0A%0A  
     req 
@@ -1633,26 +1633,24 @@
 driver;%0A    
-  
 log.verbose(
@@ -1668,33 +1668,163 @@
  req);%0A%0A    
-  try %7B%0A%0A
+resolve(req, %7B%0A%0A    basedir: process.cwd()%0A    %7D, function (error, localModule) %7B%0A%0A      try %7B%0A        if ( error ) %7B%0A          try %7B%0A%0A    
         driv
@@ -1844,32 +1844,36 @@
 req);%0A      
+    
 %7D%0A      
 catch (Excep
@@ -1852,32 +1852,36 @@
         %7D%0A      
+ 
+   
 catch (Exception
@@ -1877,32 +1877,36 @@
  (Exception) %7B%0A%0A
+    
         driver =
@@ -1930,32 +1930,36 @@
 ./' + req);%0A
+    
       %7D%0A
     %7D%0A    ca
@@ -1946,22 +1946,104 @@
       %7D%0A
+    
     %7D%0A
+        else %7B%0A          driver = require( localModule );%0A        %7D%0A      %7D%0A  
     catc
@@ -2061,24 +2061,26 @@
 n) %7B%0A%0A      
+  
 try %7B%0A%0A     
@@ -2082,16 +2082,18 @@
         
+ 
+ 
 //Fallba
@@ -2154,24 +2154,26 @@
 pos%0A        
+  
 req = './' +
@@ -2184,24 +2184,26 @@
 fig.driver;%0A
+  
         log.
@@ -2228,32 +2228,34 @@
 , req);%0A        
+  
 driver = require
@@ -2263,32 +2263,34 @@
 req);%0A      
+  
 %7D%0A      
 catch (Excep
@@ -2273,32 +2273,34 @@
     %7D%0A      
+ 
+ 
 catch (
-E
+e
 xception) %7B%0A
@@ -2292,32 +2292,34 @@
  (exception) %7B%0A%0A
+  
         return c
@@ -2328,16 +2328,28 @@
 lback( %7B
+%0A           
  stack: 
@@ -2406,32 +2406,34 @@
 a ' +%0A          
+  
 'npm install db-
@@ -2471,32 +2471,34 @@
 r ' +%0A          
+  
 'npm install -g 
@@ -2525,16 +2525,25 @@
 g.driver
+%0A        
   %7D );%0A 
@@ -2551,19 +2551,74 @@
     
+  
 %7D%0A    
+ 
+ 
 %7D%0A
-  %7D
+%0A      _connect();%0A    %7D);%0A  %7D%0A%0A%0Afunction _connect() %7B
 %0A%0A  
@@ -3658,11 +3658,13 @@
 g);%0A  %7D%0A
+%7D%0A
 %7D;%0A

Resolve driver from local projects