Running migrations for Codeigniter 4 via HTTP

I did not have command line access on a few of the servers that hosted the projects I was working on. And currently Codeigniter 4 only has documentation on running database migrations / rollbacks / seeds in the command line.

Therefore I asked around and made a special controller for the migrations / rollbacks / seeding on those servers. Now I can run these database operations in the browser, by accessing the methods of that controller like this:

https://example.com/admintools/migrate

<?php

namespace App\Controllers;

use Exception;

class Admintools extends BaseController
{

    public function migrate()
    {
        // uncomment this after running migrations
        // exit;

        if (! service('ionAuth')->isAdmin()) {
            return;
        }
        // Load the migrations library
        $migrate = \Config\Services::migrations();

        try {
            // Run all available migrations
            $migrate->latest();

            // Alternatively, you can specify a specific version:
            // $migrate->version(3);

            echo 'Migrations were executed successfully.<br>';
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }


    public function seed()
    {
        // uncomment this after running seeds
        // exit;

        if (! service('ionAuth')->isAdmin()) {
            return;
        }

        $seeder = \Config\Database::seeder();

        try {
            echo 'Seeding Certificates...<br>';

            $seeder->call('CertificatesSeeder');
            echo 'Seeding executed successfully.<br>';
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }

        // add more seeds here if needed
    }

    public function rollback()
    {
        // uncomment this after running rollback
        // exit;

        if (! service('ionAuth')->isAdmin()) {
            return;
        }
        // Load the migrations library
        $migrate = \Config\Services::migrations();

        try {
            // Run rollback; see the last batch that is to be kept
            // (find it in table „migrations")
            // and reference it in $target variable
            $target = 3; // if set "3", both migrations with that no and and those below will be unaffected
            $migrate->regress($target);

            echo 'Migration rollback executed successfully.<br>';
        } catch (Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }
}

As you can see, there are multiple precautions here:

  • I have an „exit;“ statement on all methods by default, so I have to comment those out if I want the methods to do stuff (and comment them out again after stuff is done). This prevents from re-running commands accidentally by accidentally opening a migration-related address in the browser (which is extremely easy to do).
  • I also do not pass any data to controller methods via url segments or via get variables; and
  • I check for the user to be admin.

And it is ever advisable to do db backup before executing migrations, whichever way you do it.


Paskelbta

sukūrė

Komentarai

Įrašo “Running migrations for Codeigniter 4 via HTTP” komentarų : 1

  1. Mike avataras
    Mike

    Thank you so much ! I’d been searching for hours for exactly this !

    Well done 😀

Parašykite komentarą

El. pašto adresas nebus skelbiamas. Būtini laukeliai pažymėti *

Brukalų kiekiui sumažinti šis tinklalapis naudoja Akismet. Sužinokite, kaip apdorojami Jūsų komentarų duomenys.