PHP code example of sympress / migration

1. Go to this page and download the library: Download sympress/migration library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sympress / migration example snippets




declare(strict_types=1);


use SymPress\WordPress\Migration\Application\MigrationSystem;
use SymPress\WordPress\Migration\Domain\AbstractMigration;

final class CreateOrdersTableMigration extends AbstractMigration
{
    protected const string VERSION = '1.0.0';

    public function up(): string|array
    {
        $tableName = $this->prefix . 'orders';

        return "CREATE TABLE {$tableName} (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            order_number varchar(191) NOT NULL,
            PRIMARY KEY (id),
            KEY order_number (order_number)
        ) {$this->charsetCollate};";
    }

    public function down(): string|array
    {
        return sprintf('DROP TABLE IF EXISTS %sorders;', $this->prefix);
    }
}

add_action('db_migration_register', static function (MigrationSystem $system): void {
    $database = $GLOBALS['wpdb'];

    $system->registerMigrations('orders-plugin', [
        new CreateOrdersTableMigration($database),
    ]);
});

$manager = MigrationSystem::getInstance()->createMigrationManager('orders-plugin');

if ($manager === null) {
    return;
}

$manager->syncMetadataStorage();
$manager->migrateTo('1.0.0');
$manager->executeMigration(CreateOrdersTableMigration::class, 'up');
$manager->markMigration(CreateOrdersTableMigration::class, 'down');

$current = $manager->getCurrentMigration();
$latest = $manager->getLatestMigration();
$history = $manager->getMigrationHistory();
$pending = $manager->getPendingMigrations();