Versionen im Vergleich

Schlüssel

  • Diese Zeile wurde hinzugefügt.
  • Diese Zeile wurde entfernt.
  • Formatierung wurde geändert.

...

To implement a schema level update, two steps must be taken:

  1. The new numeric schema level must be specified in the plugin’s descriptor file.

  2. The actual schema update must be implemented.

Define new schema level in plugin descriptor

...

Codeblock
languagejson
{
    "$schema": "http://git.intra.pinuts.de/cmsbs/cmsbs/raw/master/doc/json-schemas/plugin.desc.json",
    "__encoding": "Pinuts File Encoding: UTF-8",
    "name": "de.acme.tutorial",
    ...,
    "dbSchema": 1
}

Implement schema update

Create a new ES6 file in your plugin’s CSE directory. If your plugin’s name is de.acme.projectxtutorial then cmsbs-conf/cse/plugins/de.acme.projectxtutorial/callback/DbSchemaUpdate.es6mjs would be a good choice:

Codeblock
languagejs
/// <reference path="../../../.vscode.js"/>

const SQL = UM.config.sql;

export default class DbSchemaMigration extends PluginSchemaMigration {
    schemaUpdate_0_1(scr) {
        // Create column for primary attribute "new_attribute":
        this.primaryAddColumn(scr, UM.config.getAttribute('new_attribute'));

        // Alter primary attribute's column according to the current settings from *.attributes file:
        this.primaryModifyColumn(scr, UM.config.getAttribute('altered_attribute'));

        // Create column for new table attribute's column:
        this.tableAddColumn(scr, UM.config.getAttribute('table_attribute->new_column'));

        // Alter table attribute's column according to the current settings from *.attributes file:
        this.tableModifyColumn(scr, UM.config.getAttribute('table_attribute->altered_column'));
        
        // Issue SQL statement to create an INDEX:
        this.primaryAddIndex(scr, UM.config.getAttribute('new_attribute'));

        // Issue SQL statement to mass-update some user entries:
        const createIndex1update1 = new SqlQuery(`CREATE INDEX idx_users_new_attr ON`UPDATE ${SQL.qualifyTable('users')} (SET p_new_attribute)`)entrytype='user' WHERE p_entrytype IS NULL`);
        scr.add(createIndex1update1);
        
        //  this.primaryAddColumn(scr, UM.config.getAttribute("active"));
Issue SQL statement to mass-update some user entries:
        const update1update2 = new SqlQuery(`UPDATE ${SQL.qualifyTable('users')} SET p_entrytype='usercustomer' WHERE p_entrytype IS NULL`='user'`);
        scr.add(update1update2);
    }
}

// Don't forget to export the class to the respective namespace:
de.acme.projectxtutorial.DbSchemaMigration = DbSchemaMigration;

The method schemaUpdate_0_1 will be executed upon UM startup whenever the recorded schema level for the plugin de.acme.projectx is below “1”.

To implement a second schema update, just increase the dbSchema counter in your plugin descriptor file and implement the schemaUpdate_1_2 method.

There are several helper methods available to accomplish common tasks like adding or altering columns or executing custom SQL statements:

Hinweis

Please keep in mind that a database schema update cannot be undone automatically!

Have a fresh backup at hand to be able to rollback the database if needed.