How to Create and Upgrade Database in Magento 2
Managing database is essential for any platform and Magento 2 E-commerce store is no exception! One may want to add a new database or update the existing ones. Add new rows or columns or delete particular data from the database and manage the database easily in Magento 2.
I have come up with a process to create and upgrade database in Magento 2 the easy way. Hope it is helpful.
Steps to create a database and upgrade in Magento 2:
Magento 2 provides some classes in the directory app/code/[Namespace]/[ModuleName]/Setup to create or upgrade database.
- InstallSchema – setup database structure
- InstallData – initial the data for database table.
- UpgradeSchema – upgraded database structure
- UpgradeData – upgraded (add/remove) data from table.
Functions of these classes will be run when we install or upgrade module by this command line:
1 |
php bin/magento setup:upgrade |
Elaborating each step,
- Install Schema
Create file: app/code/Meetanshi/HelloWorld/Setup/InstallSchema.php
12345678910111213141516171819202122232425262728<?phpnamespace Magestore\DataExample\Setup;use Magento\Framework\Setup\SchemaSetupInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\InstallSchemaInterface;class InstallSchema implements InstallSchemaInterface{public function install(SchemaSetupInterface $setup,ModuleContextInterface $context){$installer = $setup;$installer->startSetup();$table = $installer->getConnection()->newTable($installer->getTable('data_example'))->addColumn('example_id',\Magento\Framework\Db\Ddl\Table::TYPE_INTEGER,null,['identity' => true, 'nullable' => false, 'primary' => true, 'unsigned' => true],'Example Id')->addColumn('title',\Magento\Framework\Db\Ddl\Table::TYPE_TEXT,255,['nullable' => false],'Example Title')->addColumn('content',\Magento\Framework\Db\Ddl\Table::TYPE_TEXT,'2M',[],'Example Content')->addColumn('created_at',\Magento\Framework\Db\Ddl\Table::TYPE_TIMESTAMP,null,['nullable'=>false,'default'=>\Magento\Framework\Db\Ddl\Table::TIMESTAMP_INIT],'Created At');$installer->getConnection()->createTable($table);$installer->endSetup();}}
You can review all data types in this file: \Magento\Framework\Db\Ddl\Table.php - Install Data
File: app/code/Meetanshi/HelloWorld/Setup/InstallData.php
12345678910111213141516171819<?phpnamespace Meetanshi\HelloWorld\Setup;use Magento\Framework\Setup\InstallDataInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;class InstallData implements InstallDataInterface{protected $_exampleFactory;public function __construct(\Meetanshi\HelloWorld\Model\ExampleFactory $exampleFactory){$this->_exampleFactory = $exampleFactory;}public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){$data = ['title' => "First example title",'content' => "First Example content"];$example = $this->_exampleFactory->create();$example->addData($data)->save();}} - Upgrade Schema
File: app/code/ Meetanshi/HelloWorld/Setup/UpgradeSchema.php
123456789101112131415161718<?phpnamespace Meetanshi\HelloWorld\Setup;use Magento\Framework\Setup\UpgradeSchemaInterface;use Magento\Framework\Setup\SchemaSetupInterface;use Magento\Framework\Setup\ModuleContextInterface;class UpgradeSchema implements UpgradeSchemaInterface{public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context){$installer = $setup;$installer->startSetup();if (version_compare($context->getVersion(), '1.0.1', '<')){$installer->getConnection()->dropColumn($installer->getTable('data_example'), 'created_at');}$installer->endSetup();}} - Upgrade Data
File: app/code/ Meetanshi/HelloWorld/Setup/UpgradeData.php
12345678910111213141516171819202122<?phpnamespace Meetanshi\HelloWorld\Setup;use Magento\Framework\Setup\UpgradeDataInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;use Magento\Framework\Setup\ModuleContextInterface;class UpgradeData implements UpgradeDataInterface{protected $_exampleFactory;public function __construct(\Meetanshi\HelloWorld\Model\ExampleFactory $exampleFactory){$this->_exampleFactory = $exampleFactory;}public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context){if (version_compare($context->getVersion(), '1.0.1', '<')){$data = ['title' => "The second example title", 'content' => "The second example content"];$example = $this->_exampleFactory->create();$example > addData($dalogta)->save();}}}
Play with your database and control it easily in your Magento 2 store:) You may also love to read our blog post on How to Change Database Name in Magento 2.
Let me know if you have any doubts in the Comments below and I’ll help you out to solve it.
Rate my article with 5 stars if you find it useful.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Comments are closed here.