Files
Cactidocumentation/Plugin-Schema-Upgrades.md
T

3.9 KiB

Upgrade a Plugin with Existing Data

Version 1 of the example device list stores an ID and name. Version 2 adds a note to each device. This walkthrough tests three installation paths: upgrade an existing database, retry after a migration, and install the new version into an empty database.

Record the starting state

On a disposable Cacti system, install version 1 and insert the four example records. Inspect both the registered plugin version and the actual schema:

SELECT version FROM plugin_config WHERE directory = 'workflowdemo';
SHOW COLUMNS FROM plugin_workflowdemo_devices;
SELECT id, name FROM plugin_workflowdemo_devices ORDER BY id;

Expect version 1.0, columns id and name, and four rows. For a real upgrade, back up existing data and test restoring it. Rehearse the upgrade against a copy from the oldest version your plugin supports. Run one migration at a time while plugin writes are quiescent.

Deploy and migrate

Extract workflowdemo version 2 over plugins/workflowdemo/. Keep the plugin installed. From the Cacti root run:

php plugins/workflowdemo/upgrade.php

This is a CLI entry point supplied by the example. Replacing INFO changes the version on disk. The migration separately updates the version in plugin_config after verifying the database change. It accepts an installed version of 1.0 or 2.0 and requires version 2.0 on disk.

The same column definition is used on upgrade and fresh installation:

function workflowdemo_note_column() {
    return ['name' => 'note', 'type' => 'varchar(80)', 'NULL' => false, 'default' => ''];
}

The migration calls api_plugin_db_add_column() only when the column is absent. The helper records its change for the plugin. The example then reads information_schema.COLUMNS to verify the name, type, length, and nullability. A second call to db_column_exists() in the same request can return its cached pre-migration result. Only after verification does the script change the registered version and print Schema 2.0 ready.

Verify a retry preserves user data

After the first run, check all four IDs and names. The new column starts with an empty string on every row. Write a note and rerun the command:

SELECT id, name, note FROM plugin_workflowdemo_devices ORDER BY id;
UPDATE plugin_workflowdemo_devices SET note = 'keep this note' WHERE id = 2;
php plugins/workflowdemo/upgrade.php
SELECT name, note FROM plugin_workflowdemo_devices WHERE id = 2;
SELECT version FROM plugin_config WHERE directory = 'workflowdemo';

Expect edge-b, keep this note, and version 2.0. A failure between the schema change and version update leaves the column available for the next run to verify. If the definition differs, inspect the database before continuing. For migrations that transform row data, verify the transformation as well as the schema before recording success.

Verify uninstall and a fresh installation

On the disposable fixture, uninstall the plugin. This removes its owned tables and their rows. Keep a backup if you need the example data later.

php cli/plugin_manage.php --plugin=workflowdemo --uninstall
SHOW TABLES LIKE 'plugin_workflowdemo_%';

Expect no matching tables. Leave the version 2 files in place and install again. A fresh installation should create the final schema immediately.

php cli/plugin_manage.php --plugin=workflowdemo --install --enable
SHOW COLUMNS FROM plugin_workflowdemo_devices LIKE 'note';
SELECT COUNT(*) FROM plugin_workflowdemo_devices;

Expect the note column and zero records. This distinguishes a working fresh install from an upgrade that only succeeds when historical data or an earlier installation step is present.


Copyright (c) 2004-2026 The Cacti Group