Librenms downgrade procedure with database migration

Hello,

there is a manual procedure to upgrade to a new major version.

From https://docs.librenms.org/General/Updating/ -> avanced user, I do after

git pull
git reset --hard <major-version>
composer install --no-dev
./build-base.php
./validate.php

major-version : for example 1.58

If I want to downgrade to a previous version. I can do :

git reset --hard <previous-major-version>
composer install --no-dev
./build-base.php
./validate.php

previous-major-version : for example 1.57

If there is no database migration between 1.57 and 1.58, it will work. But if not, it won’t.

Code contains routines to migrate database schema (function up) and to revert (function down). But how to do the migration revert easily ?

$ cat database/migrations/2019_10_03_211702_serialize_config.php 
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class SerializeConfig extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        (...)

            DB::table('config')
                ->where('config_id', $config->config_id)
                ->update(['config_value' => json_encode($value)]);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::table('config')->get()->each(function ($config) {
            $value = json_decode($config->config_value);
            $value = is_bool($value) ? var_export($value, true) : (string)$value;

            DB::table('config')
                ->where('config_id', $config->config_id)
                ->update(['config_value' => $value]);
        });
    }
}

The easiest way, in that case, is to wait for next major version. You can revert migrations but :

  • You have to do it before doing the GIT reset (cause you cannot rollback a migration file that has already been removed by git, obviously)
  • You should probably stop all you crons before doing it
  • You should do a snapshot (if running on a VM) of the machine before touching anything
  • You should read about the ‘migrate:rollback’ command in Laravel.

Merci. Following works to revert one step is database schema:

php artisan migrate:rollback --step=1