Menu

Setup management on Magento 2

Magento 2 Training : Unit 3 – Lesson A

Module presentation

To begin this part of the formation, we will create a new module. It will be a job offer module with 2 models :
– A department : this is the department of the society
– A job : this is the job offer, linked to a department.

Firstly I declare the elements I need to create my table :
– A name : $tableName
– A table comment : $tableComment
– Table columns with their type, their size, some options and a comment
– Some indexes if I need (we will see that on the next part of the lesson) : $indexes
– Some foreign keys if I nedd (we will see that on the next part of the lesson) : $foreignKeys

Let’s see available columns options :
– identity : we can declare an auto_increment column
– unsigned : we work with an unsigned number
– nullable : when it false, it add “NOT NULL” SQL to the column
– primary : we can declare our primary key
– default : we can declare a default value for our column

Secondly, I loop on each parameters to use the correct Magento 2 methods and setup all things we need. You can look more precisly the code if you want to analyze it’s behaviour 😉

Now, we can launch the upgrade. So you can go on your Magento root folder and launch :

./bin/magento setup:upgrade

You will see lines like :

Schema creation/updates:
Module 'Maxime_Jobs':
Installing schema..

Our table is created on database !

You can see the “name” field has the VARCHAR type, whereas “description” has the TEXT type.
On the setup, we declared the same type : “Table::TYPE_TEXT”
Magento will choose one of both according to the field size.

Upgrade Setup Creation

I decided to split the creation of the to tables in order to explain how to create an upgrade setup.

The file has a lot of content identical to the previous file. But in an upgrade setup, we have to compare the old module version with the new module version :

if (version_compare($context->getVersion(), '1.0.0.0') < 0)

The getVersion method give us the current module version. For us, it’s 0.1.0.0.

version_compare return -1 if current version is less than new version
version_compare retourne 0 if current version is equal new version
version_compare retourne 1 if current version is greater than new version

For each module upgrades, you have to put this condition on this file, like this :

<?php namespace Maxime\Jobs\Setup;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema for a module
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$installer = $setup;
$installer->startSetup();
if (version_compare($context->getVersion(), '1.0.0.0') < 0) {
// Action to do if module version is less than 1.0.0.0
}
if (version_compare($context->getVersion(), '1.0.0.1') < 0) {
// Action to do if module version is less than 1.0.0.1
}
if (version_compare($context->getVersion(), '1.1.0.0') < 0) {
// Action to do if module version is less than 1.1.0.0
}
if (version_compare($context->getVersion(), '1.1.0.1') < 0) {
// Action to do if module version is less than 1.1.0.1
}
if (version_compare($context->getVersion(), '2.0.0.0') < 0) {
// Action to do if module version is less than 2.0.0.0
}
$installer->endSetup();
}
}

I don’t put elseif condition, nor a switch because if someone download the module from github for example, the upgrade must do all the if conditions.
So you must put these conditions from the older to the newest in order to avoid errors during the upgrade process.

So you can go on the file :app/code/Maxime/Jobs/etc/module.xml
And modify the setup_version attribute to 1.0.0.0

Launch the upgrade command :

./bin/magento setup:upgrade

You will see lines like these during the upgrade :

Schema creation/updates:
Module 'Maxime_Jobs':
Upgrading schema..

On database we have our table :

On a next lesson we will see how to add datas on our tables. But we have to create Magento 2 models before !