PHP code example of erikgreasy / wp-db-migrations

1. Go to this page and download the library: Download erikgreasy/wp-db-migrations 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/ */

    

erikgreasy / wp-db-migrations example snippets


add_filter('dbmigrator_migrations_dirs', function($migrationDirs) {
    $migrationDirs[] = __DIR__ . '/my_plugin_migrations';

    return $migrationDirs;
});



// 001_reviewplugin_create_test_table.php

use DbMigrator\Migration;

return new class extends Migration
{
    public function up()
    {
        $tableName = $this->getPrefixedTable('test');
        $charset_collate = $this->wpdb->get_charset_collate();

        $sql = "CREATE TABLE $tableName (
            id int(11) NOT NULL auto_increment,
            name varchar(60) NOT NULL,
            UNIQUE KEY id (id)
        ) $charset_collate;";

        $this->wpdb->query($sql);
    }
};

register_activation_hook(__FILE__, function() {
    (new \DbMigrator\Migrator())->migrate();
});