First, I'm assuming when you say that nothing happens, you mean that the setup script runs as usual, but no errors are output and no changes were made to your database. If this isn't a correct assumption, please let me know!
When I do an upgrade to the schema, I don't do a getTable()
, I think that that is superfluous.
I tested the above script with that change, and it worked, so the two troubleshooting steps I would take are to:
- Ensure that you have incremented the
setup_version
in your module.xml
(or your script won't run at all)
- Add some obvious error in your upgrade script to see if it is running at all...if there is an error and the script is running, you will get error messages when running
setup:upgrade
I hope that helps!
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
Best Answer
Try below code in your UpgradeSchema.php file :
Note :