Database

Warning

A plugin should never change core’s database! It just add its own tables to manage its own data.

Of course, plugins rely on GLPI database model and must therefore respect database naming conventions.

Creating, updating or removing tables is done by the plugin, at installation, update or uninstallation; functions added in the hook.php file will be used for that; and you will rely on the Migration class provided from GLPI core. Please refer to this documentation do know more about various Migration possibilities.

Creating and updating tables

Creating and updating tables must be done in the plugin installation process. You will add the required code to the plugin_{myplugin}_install. As the same fucntion is used for both installation and update, you’ll have to make tests to know what to do.

For example, we will create a basic table to store some configuration for our plugin:

<?php

/**
 * Install hook
 *
 * @return boolean
 */
function plugin_myexample_install() {
   global $DB;

   //instanciate migration with version
   $migration = new Migration(100);

   //Create table only if it does not exists yet!
   if (!TableExists('glpi_plugin_myexample_configs')) {
      //table creation query
      $query = "CREATE TABLE `glpi_plugin_myexample_config` (
                  `id` INT(11) NOT NULL autoincrement,
                  `name` VARCHAR(255) NOT NULL,
                  PRIMARY KEY  (`id`)
               ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
      $DB->queryOrDie($query, $DB->error());
   }

   //execute the whole migration
   $migration->executeMigration();

   return true;
}

The update part is quite the same. Considering our previous example, we missed to add a field in the configuration table to store the config value; and we should add an index on the name column. The code will become:

<?php
/**
 * Install hook
 *
 * @return boolean
 */
function plugin_myexample_install() {
   global $DB;

   //instanciate migration with version
   $migration = new Migration(100);

   //Create table only if it does not exists yet!
   if (!TableExists('glpi_plugin_myexample_configs')) {
      //table creation query
      $query = "CREATE TABLE `glpi_plugin_myexample_configs` (
                  `id` INT(11) NOT NULL autoincrement,
                  `name` VARCHAR(255) NOT NULL,
                  PRIMARY KEY  (`id`)
               ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
      $DB->queryOrDie($query, $DB->error());
   }

   if (TableExists('glpi_plugin_myexample_configs')) {
      //missed value for configuration
      $migration->addField(
         'glpi_plugin_myexample_configs',
         'value',
         'string'
      );

      $migration->addKey(
         'glpi_plugin_myexample_configs',
         'name'
      );
   }

   //execute the whole migration
   $migration->executeMigration();

   return true;
}

Of course, we can also add or remove tables in our upgrade process, drop fields, keys, ... Well, do just what you need to do :-)

Deleting tables

You will have to drop all plugins tables when it will be uninstalled. Just put your code into the plugin_{myplugin]_uninstall function:

<?php
/**
 * Uninstall hook
 *
 * @return boolean
 */
function plugin_myexample_uninstall() {
   $tables = [
      'configs'
   ];

   foreach ($tables as $table) {
      $tablename = 'glpi_plugin_myexample_' . $table;
      //Create table only if it does not exists yet!
      if (TableExists($tablename)) {
         $DB->queryOrDie(
            "DROP TABLE `$tablename`",
            $DB->error()
         );
      }
   }

   return true;
}

Creative Commons License