Welcome Guest, Not a member yet? Register   Sign In
Database Migrations from the Terminal
#1
Wink 

Hello Guys,

I'm managing Migrations in my Codeigniter project.

Here is my migration class:
PHP Code:
class Migrate extends CI_Controller
{

 
   public function __construct ()
 
   {
 
       parent::__construct();

 
       $this->load->library('migration');
 
       $this->load->dbforge();
 
   }

 
   public function latest ()
 
   {
 
       $this->migration->latest();
 
       echo $this->migration->error_string() . PHP_EOL;
 
   }

 
   public function reset ()
 
   {
 
       $this->migration->version(0);
 
       echo $this->migration->error_string() . PHP_EOL;
 
   }

 
   public function version ($version 0)
 
   {
 
       $version = (int) $version;
 
       if ($version == 0) {
 
           die('You need to paas a version greater than zero') . PHP_EOL;
 
       }

 
       $this->migration->version($version);
 
       echo $this->migration->error_string() . PHP_EOL;
 
   }



Now, what I would like to do is make sure my controller can be ran by the terminal.

Now all of these functions work, I can set the version, I can reset the migrations or automatically set it to the latest available.

Works Great!

However If i try to execute it from the terminal I.E:

Code:
php index.php migrate latest

I get a horrible error:

mothership:public_html Sherlock$ php index.php migrate latest

Code:
A PHP Error was encountered

Severity: WarningMessage:  mysqli::real_connect(): (HY000/2002): No such file or directoryFilename: /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.phpLine Number: 135

Backtrace:
        
    File: /Users/Matt/PhpstormProjects/barebones/application/third_party/MX/Loader.php    Line: 109    Function: DB
        
    File: /Users/Matt/PhpstormProjects/barebones/application/third_party/MX/Loader.php    Line: 173    Function: _ci_load_library
                
    File: /Users/Matt/PhpstormProjects/barebones/application/controllers/Migrate.php    Line: 14    Function: library
            
    File: /Users/Matt/PhpstormProjects/barebones/public_html/index.php    Line: 313    Function: require_once
        
    
A PHP Error was encountered

Severity: WarningMessage:  mysqli::real_connect(): (HY000/2002): No such file or directoryFilename: /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.phpLine Number: 135

Backtrace:
    
    File: /Users/Matt/PhpstormProjects/barebones/application/third_party/MX/Loader.php    Line: 173    Function: _ci_load_library
            
    File: /Users/Matt/PhpstormProjects/barebones/application/controllers/Migrate.php    Line: 14    Function: library
            
    File: /Users/Matt/PhpstormProjects/barebones/public_html/index.php    Line: 313    Function: require_once
        
    PHP Fatal error:  Call to a member function query() on a non-object in /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.php on line 221

Fatal error: Call to a member function query() on a non-object in /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.php on line 221

A PHP Error was encountered

Severity: ErrorMessage:  Call to a member function query() on a non-objectFilename: /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.phpLine Number: 221

Backtrace:

Can anybody help me with this? So that I can run this through the terminal.

Thanks
Reply
#2

It looks problem is in your hmvc implementation. Try to rename folder third_party/MX and run it via CLI again.
Reply
#3

The spelling and capitalisation is correct.

But I still have this problem.
Reply
#4

Are you relying on any $_SERVER variables in any of your code? Like to detect and set the environment? If so, those won't be set in a CLI request since it doesn't go through the web server.
Reply
#5

That looks like it'll be the likely cause but in my code I'm saying that if the server variable is not set then it will automatically be development so surely that would be fine?
Reply
#6

The error referenced is where it's instantiating the db class using the supplied db credentials in line 135. I know I've seen people using $_SERVER variables to define environments, so thought I'd ask as they won't work with CLI.
Quote:Severity: WarningMessage: mysqli::real_connect(): (HY000/2002): No such file or directoryFilename: /Users/Matt/PhpstormProjects/barebones/system/database/drivers/mysqli/mysqli_driver.phpLine Number: 135

Are you using 127.0.0.1 as the db host, or "localhost"? If localhost, try switching to the local IP.
Reply
#7

Away back on Thursday will let you know
Reply
#8

When using the MX Loader you usually need to extend MX_Controller (or some other controller which extends MX_Controller) instead of CI_Controller. Otherwise, libraries which are expected to be loaded onto $this may not be found.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB