5
class UpgradeSchema implements  UpgradeSchemaInterface
{
public function upgrade(SchemaSetupInterface     $setup,ModuleContextInterface     $context){
$setup->startSetup();
            if (version_compare($context->getVersion(), '2.0.0', '<=')) {
            $installer = $setup;
            $installer->startSetup();

         $table = $installer->getConnection()
        ->newTable($installer->getTable('my_booster'))
        ->addColumn(
            'review_id',
            Table::TYPE_SMALLINT,
            null,
            ['identity' => true, 'nullable' => false, 'primary' => true],
            'Review ID'
        )
        ->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
        ->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
        ->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
        ->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom  Comments')
        ->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
        ->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
        ->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
        ->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
        ->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
        ->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');

        $installer->getConnection()->createTable($table);
        $installer->endSetup();
    }
}

}

I am using this script.

2 Answers 2

4

i have got the correct working answer.

<?php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\DB\Ddl\Table;


class UpgradeSchema implements  UpgradeSchemaInterface
{
  public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
    $setup->startSetup();

    if (version_compare($context->getVersion(), '2.0.2', '<')) {
      $setup->getConnection()->addColumn(
        $setup->getTable("enter table name here"),
        'enter new column name here',
        [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "xample"
        ]
      );
      $setup->getConnection()->addColumn(
        $setup->getTable("enter table name here"),
        'enter new second column name here',
        [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "Xample 2"
        ]
      );
      $setup->getConnection()->addColumn(
        $setup->getTable("enter table name here"),
        'enter new third column name here',
        [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "Xample 3"
        ]
      );

    }
    $setup->endSetup();
}
}

also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .

12
  • should change to whaetever we are using here? $context->getVersion(), '2.0.2' Jan 24, 2019 at 13:57
  • how to create primary key with integer? Jan 24, 2019 at 15:24
  • @jafarpinjar ->addColumn( 'voting_id', Table::TYPE_SMALLINT, 11, ['nullable' => true, 'primary' => true, 'auto_increment' => true], 'Voting ID' )
    – sudo55
    Jan 24, 2019 at 17:32
  • I am getting base table not found error Jan 25, 2019 at 2:51
  • if you please post your code then i can understand what actually problem is.
    – sudo55
    Jan 25, 2019 at 16:34
2

Try this

class UpgradeSchema implements UpgradeSchemaInterface
{

    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $setup->startSetup();

        $version = $context->getVersion();
        $connection = $setup->getConnection();

        if (version_compare($version, '2.0.0') < 0) {
            $connection->addColumn(
                $setup->getTable('my_booster'),
                'author_id',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    'nullable' => true,
                    'comment' => 'Author ID',

                ]
            );

        }
    }
5
  • kindly mention the issue as well.
    – sudo55
    Aug 13, 2018 at 22:46
  • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing Aug 13, 2018 at 22:48
  • try this if (version_compare($version, '2.0.0') < 0) Aug 13, 2018 at 22:49
  • your answer is not working for me.
    – sudo55
    Aug 13, 2018 at 22:53
  • How to copy existing table structure to the new table while install module? @sherazkhan Feb 27, 2020 at 10:24

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.