Database Migration¶
Alternative way: look at- _backend/extension/payment/pp_express/init.php
Start Step by Step¶
If you are going to migrate for the first time the run the command below
vendor/bin/phpmig init
## Step-01: Open file and add the code below: backend/extension/[module]/[myextension]/init.php
<?php
...
public function init()
{
$this->setMigrationTable('migrate_module_myextension');
$this->setMigrationPath(DIR_EXTENSION . 'module/myextension/database/migrations');
$this->migration();
}
...
public function install()
{
Migration::up();
}
public function uninstall()
{
Migration::down();
}
``
## Step-02: Open the folder: backend/extension/[module]/[myextension]
File/Folder Structure at the Location: backend/extension/[module]/[myextension]/
+ database
+ migrations
- [myextension_module].sql
*** Example content of [myextension_module].sql
``
--
-- Database: `phspark`
--
-- ---------------------------------------------------------
SET sql_mode = '';
-- --------------------------------------------------------
--
-- Table structure for table `ps_myextension`
--
DROP TABLE IF EXISTS `ps_myextension`;
CREATE TABLE `ps_myextension` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(32) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
## Step-03: Create a database migration table with the command below
vendor/bin/phpmig generate AddTablesOfCustomerModule -s module_myextension
The command above will create a file at: backend/extension/[module]/[myextension]/database/migrations/
File name will be like this: 20210812035729_AddTablesOfMyextensionModule.php at the location: backend/extension/[module]/[myextension]/database/migrations/
<?php
use Phpmig\Migration\Migration;
class AddTablesOfCustomerModule extends Migration
{
use Src\Traits\Extension\MigrationTrait;
/**
* Extension Type
*/
public function getExtensionType()
{
return 'module';
}
/**
* Extension Name
*/
public function getExtension()
{
return 'myextension';
}
}
## Step-04: Migration Up/Down
vendor/bin/phpmig up -s module_myextension 20210812035729 vendor/bin/phpmig down -s module_myextension 20210812035729
For More Studies¶
Package and Commands for Database Migration
Package name: vendor/davedevelopment/phpmig
## Example Commands
vendor/bin/phpmig generate MyTestTable -s phspark
vendor/bin/phpmig generate AddTablesOfMyExtensionModule -s module_myextension
## Commands
vendor/bin/phpmig --version
vendor/bin/phpmig --help
vendor/bin/phpmig --list
vendor/bin/phpmig init
vendor/bin/phpmig status
vendor/bin/phpmig migrate
vendor/bin/phpmig rollback
vendor/bin/phpmig rollback -t 20111101000144
vendor/bin/phpmig rollback -t 0
vendor/bin/phpmig down 20111101000144
vendor/bin/phpmig up -s <SET NAME HERE> --<VERSION HERE>
vendor/bin/phpmig down -s <SET NAME HERE> --<VERSION HERE>
vendor/bin/phpmig generate AddRatingToLolCats
vendor/bin/phpmig generate AddRatingToLolCats ./migrations
check Check all migrations have been run, exit with non-zero if not
down Revert a specific migration
generate Generate a new migration
help Displays help for a command
init Initialise this directory for use with phpmig
list Lists commands
migrate Run all migrations
redo Redo a specific migration
rollback Rollback last, or to a specific migration
status Show the up/down status of all migrations
up Run a specific migration