CodeIgniter Forums
Cannot auto-migrate after creating DB in new Command (CI4.2) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Cannot auto-migrate after creating DB in new Command (CI4.2) (/showthread.php?tid=85896)



Cannot auto-migrate after creating DB in new Command (CI4.2) - DocHojo - 12-18-2022

Hi there,

I'm trying to do a spark command doing all the following instruction at once:
- DROP actual DB
- CREATE DB with same name
- Migrate
- Seed
The new Command has the run method as below:

PHP Code:
    public function run(array $params) {
        // Drop table of db
        command('db:drop');
        // Migrate
        command('migrate', ['all']);
        // Seed
        command('db:seed ImportAll');
    

The db:drop command has the code below:

PHP Code:
      $name_db getenv('database.default.database');
      $this->db->query('DROP DATABASE IF EXISTS ' $name_db);
      $this->db->query('CREATE DATABASE ' $name_db); 

Problem is that the migrate command is generating an error, stating that no DB is selected.

I tried to reconnect the DB after the drop doing $this->db->connect() but no success.

The trick that works for now is to remove only the tables and not the entire DB but it is not the way I want to do it.

Any hint on how to make this working?