1

I created a custom module which has below UpgradeData.php.

<?php

namespace Akhil\Database\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeData implements UpgradeDataInterface {

    protected $newsFactory = '';

    public function __construct(\Akhil\News\Model\NewsFactory $newsFactory) {
        $this->newsFactory = $newsFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context) {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
        $data = [
            'news_title' => "News Title upgrade",
            'news_content' => "News content upgrade"
        ];
        $news = $this->newsFactory->create();
        $news->addData($data)->save();
        }
        if (version_compare($context->getVersion(), '1.0.2', '<')) {
        $data = [
            'news_title' => "News Title upgrade 1",
            'news_content' => "News content upgrade 1"
        ];
        $news = $this->newsFactory->create();
        $news->addData($data)->save();
        }
        if (version_compare($context->getVersion(), '1.0.3', '<')) {
        $data = [
            'news_title' => "News Title upgrade 2",
            'news_content' => "News content upgrade 2"
        ];
        $news = $this->newsFactory->create();
        $news->addData($data)->save();
        }

    }

}

Here is my module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Akhil_Database" setup_version="1.0.1" data_version="1.0.2" />
</config>

Now if I run below command:

php bin/magento setup:db-data:upgrade

It does not upgrade the data version. Even in the table setup_module I don't see upgraded version.

Although, I am able to make my work done by changing setup_version="1.0.1" to setup_version="1.0.2" and run php bin/magento setup:upgrade

But my question why php bin/magento setup:db-data:upgrade not working for me. What I am doing wrong here?

As per my knowledge:

php bin/magento setup:upgrade //To upgrade both schema and data
php bin/magento setup:db-schema:upgrade //To upgrade only schema
php bin/magento setup:db-data:upgrade //To upgrade only data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.