Welcome Guest, Not a member yet? Register   Sign In
Migrate to a certain file
#1

Let's say I have 10 migrations files in the folder "App/Database/Migrations and everyone is migrated. Now I like to rollback to the migration nr 7. How can I do that from a controller in CI4?
In CI3 I ran
Code:
$this->migration->version($timestamp);

I have problem finding any function that rollbacks migrations the way I want:
Method regress() seems to work with batches and groups. I have group 'default' and batch '1' in all my files, so that's not for me.
Method force() might work, but how do I pass in the namespace as the second argument? force('App\Database\Migrations\Add_something,)
The following ugly script throws the error "Migration file not found":
PHP Code:
<?php namespace App\Controllers;

class 
Migrate extends \CodeIgniter\Controller
{
    public function index()
    {
        // Find latest migrated file
        $db = \Config\Database::connect();
        $query $db->query('SELECT * FROM migrations ORDER BY id DESC LIMIT 1');
        foreach ($query->getResult() as $row)
        {
            $latest =  $row->version;
        }

        // Display all migrations files with links to method "force"
        $migrate = \Config\Services::migrations();
        $files =  $migrate->findMigrations();
        echo "Migrations (to keep order, only migrate one step up/down at a time. I will restrict links someday):<br>";
        foreach ($files as $file) {
            echo $file->path;
            echo '<a href="/migrate/force/'.$file->path.'">Migrate this file</a>';
            if($file->version == $latest)
                echo ' LATEST';
            //dx($file);
            echo '<br>';
        }
    }

    public function force($path){
        $migrate = \Config\Services::migrations();
        $migrate->force($path,'App\Database\Migrations');
        return redirect()->to('/migrate');
    }



FYI The above controller results in a view like this:

"Migrations (to keep order, only migrate one step up/down at a time. I will restrict links someday):
C:\xampp\htdocs\ci4\app\Database\Migrations\20100101000000_add_dummy.phpMigrate this file
C:\xampp\htdocs\ci4\app\Database\Migrations\20100101000002_add_hobby.phpMigrate this file
C:\xampp\htdocs\ci4\app\Database\Migrations\20100101000003_add_human.phpMigrate this file LATEST
C:\xampp\htdocs\ci4\app\Database\Migrations\20100101000004_add_another_human.phpMigrate this file"
Reply


Messages In This Thread
Migrate to a certain file - by muuucho - 10-05-2020, 04:34 AM
RE: Migrate to a certain file - by T.O.M. - 10-09-2020, 12:52 AM



Theme © iAndrew 2016 - Forum software by © MyBB