Anybody have any idea/suggestion about creating Magento 2(CE Stable version) Upgrade Script(in custom-module) for adding/updating new field into custom table ?
I know about "InstallSchema" but is there something like "UpgradeSchema" for upgrading the module tables ?
Please explain in detail with examples.
-
@Pradeep Kumar Your answer was very helpful. Thanks for the head-start. Next I have gone through a little more deep inside and found that, we should use <i><b>changeColumn</b></i> method while we change the column name or name & definition both. And we should use <i><b>modifyColumn</b></i> to change definition of column. More details at <i>Magento\Framework\DB\Adapter\AdapterInterface</i> Example at <i>Magento\SalesRule\Setup\UpgradeSchema</i> ThanksSandipan S– Sandipan S2016年11月02日 07:10:24 +00:00Commented Nov 2, 2016 at 7:10
1 Answer 1
create a app\code\Sugarcode\Test\Setup\UpgradeSchema.php and run upgrade command
when ever version was changed just change in module.xml and in UpgradeSchema.php add one more if condition with version compare
if (version_compare($context->getVersion(), '2.0.1', '<')) {
// Changes here.
}
so when you run upgrade command it will run UpgradeSchema.php file and in that it will compare the version based on that version it will execute the code
ex
<?php
namespace Sugarcode\Test\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
{
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
$tableName = $setup->getTable('testtable');
if (version_compare($context->getVersion(), '2.0.0') < 0) {
// Changes here.
}
if (version_compare($context->getVersion(), '2.0.1', '<')) {
// Changes here.
}
if (version_compare($context->getVersion(), '2.0.2', '<')) {
if ($setup->getConnection()->isTableExists($tableName) == true) {
$connection = $setup->getConnection();
/* $connection->addColumn(
$tableName,
'updated_at',
['type' => Table::TYPE_DATETIME,'nullable' => false, 'default' => '', 'afters' => 'created_at'],
'Updated At'
); */
$connection->changeColumn(
$tableName,
'summary',
'short_summary',
['type' => Table::TYPE_TEXT, 'nullable' => false, 'default' => ''],
'Short Summary'
);
// Changes here.
}
}
$setup->endSetup();
}
}
module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Sugarcode_Test" setup_version="2.0.2" schema_version="2.0.2" />
</config>
if it works accept the answer by clicking right symbol
-
1@pradeep-kumar The column comment is where addColumn and changeColumn expect the schema name.
public function addColumn($tableName, $columnName, $definition, $schemaName = null);. You can put the comment 'Updated At' in the $definition array like['comment' => 'Updated At'].user2450– user24502016年02月17日 08:36:24 +00:00Commented Feb 17, 2016 at 8:36 -
@Pradeep, My version is "1.0.0", so if (version_compare($context->getVersion(), '1.0.0', '<')) {} now it will work or not?Jafar Pinjar– Jafar Pinjar2019年05月31日 14:43:08 +00:00Commented May 31, 2019 at 14:43
-
what is the older version , if older version is less then 1.0.0 then it worksPradeep Kumar– Pradeep Kumar2019年06月11日 12:22:54 +00:00Commented Jun 11, 2019 at 12:22
Explore related questions
See similar questions with these tags.