29

I am working on magento custom module, in module have Setup\InstallSchema.php file which is installed before. i have added some more database fields in InstallSchema.php so i want to update table structure but table have not applied any changes.

How i can apply schema changes to database table?

I have process cli commands to update schema but not success.

php bin/magento setup:db-schema:upgrade

and

php bin/magento setup:upgrade
  • You can uninstall with "php bin/magento module:uninstall" and install your extension again. Another point to check it's UpgradeSchema.php like github.com/magento/magento2/commit/… Seems like at this moment here is no clean explanation how to upgrade the data base, so i also looking forward to correct answer here – FireBear Oct 12 '15 at 6:20
  • @FireBear appy below answer code ? – Suresh Chikani Oct 12 '15 at 6:48
  • not try yet, but looks correct according to sample from Catalog core module github.com/magento/magento2/blob/… – FireBear Oct 12 '15 at 6:50
  • Most of the time, the error comes from not having a defined namespace for the class. Check that you have defined a namespace for you class. – soukaina Jul 9 '18 at 8:37
48

If you want to add more column into existing table of your module you could do following.

Step 1: Create UpgradeSchema.php under Setup folder. Get Idea from following code.

namespace Vendor\ModuleName\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
class UpgradeSchema implements  UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup,
                            ModuleContextInterface $context){
        $setup->startSetup();
        if (version_compare($context->getVersion(), '1.0.1') < 0) {

            // Get module table
            $tableName = $setup->getTable('table_name');

            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($tableName) == true) {
                // Declare data
                $columns = [
                    'imagename' => [
                        'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                        'nullable' => false,
                        'comment' => 'image name',
                    ],
                ];

                $connection = $setup->getConnection();
                foreach ($columns as $name => $definition) {
                    $connection->addColumn($tableName, $name, $definition);
                }

            }
        }

        $setup->endSetup();
    }
}

Step 2: Change the setup_version value in module.xml

Step 3: Run php bin/magento setup:upgrade command from CLI

  • 1
    can you exmplain how i can add primary key using upgrade script ? in my table have 'customer_id' but it is not primary key now i want to add it as primary key. – Suresh Chikani Oct 12 '15 at 7:17
  • Yes, you can change it using modifyColumnByDdl() function. Following is the schema. ` public function modifyColumn($tableName, $columnName, $definition, $flushData = false, $schemaName = null);` – Praful Rajput Oct 12 '15 at 7:30
  • How i can apply 'custome_id' field as primary key ? explain code exanple. – Suresh Chikani Oct 12 '15 at 8:41
  • Can you please Add "How we can modified type of existing column ?" in your question? – Praful Rajput Oct 12 '15 at 9:01
  • 3
    @Keyur Shah, you do not need to create a new file M2. You have to put following code into UpgradeSchema.php and update setup_version into module.xml as well. if (version_compare($context->getVersion(), '1.0.0') < 0) { // you code } if (version_compare($context->getVersion(), '1.0.1') < 0) { // your code} – Praful Rajput Oct 12 '15 at 12:52
2

To upgrade Installer Schema you have to write 'UpgradeSchema.php',

example of UpgradeSchema.php:

namespace <namespace>\<modulename>\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

/**
* @codeCoverageIgnore
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
    /**
 * {@inheritdoc}
 */
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $installer = $setup;

    $installer->startSetup();

    /*your code here*/

    $installer->endSetup();
}
}

Step 2: In your module you will find module.xml inside etc folder in that file change setup_version value(ex: 1.0.1 to 1.0.2) version value should be higher then current version value.

Step 3: Run php bin/magento setup:upgrade command from CLI

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.