PHP code example of easyswoole / db-migrate

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

    

easyswoole / db-migrate example snippets


\EasySwoole\Command\CommandManager::getInstance()->addCommand(new \EasySwoole\DatabaseMigrate\MigrateCommand());
$config = new \EasySwoole\DatabaseMigrate\Config\Config();
// 数据地址
$config->setHost("127.0.0.1");
// 数据库端口
$config->setPort(3306);
// 数据库用户名
$config->setUser("root");
// 数据库密码
$config->setPassword("123456");
// 数据库库名
$config->setDatabase("easyswoole");
// 数据库超时时长
$config->setTimeout(5.0);
// 数据库字符集
$config->setCharset("utf8mb4");
//===========可选配置修改项,以下参数均有默认值===========
// 迁移记录的数据库表名
$config->setMigrateTable("migrations");
// 迁移文件目录的绝对路径
$config->setMigratePath(EASYSWOOLE_ROOT . '/Database/Migrates/');
// 迁移模板文件的绝对路径
$config->setMigrateTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/migrate._php');
// 迁移模板类的类名
$config->setMigrateTemplateClassName("MigratorClassName");
// 迁移模板类的表名
$config->setMigrateTemplateTableName("MigratorTableName");
// 迁移模板创建表的模板文件的绝对路径
$config->setMigrateCreateTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/migrate_create._php');
// 迁移模板修改表的模板文件的绝对路径
$config->setMigrateAlterTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/migrate_alter._php');
// 迁移模板删除表的模板文件的绝对路径
$config->setMigrateDropTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/migrate_drop._php');
// 数据填充目录绝对路径
$config->setSeederPath(EASYSWOOLE_ROOT . '/Database/Seeds/');
// 数据填充模板类的类名
$config->setSeederTemplateClassName("SeederClassName");
// 数据填充模板文件的绝对路径
$config->setSeederTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/seeder._php');
// 逆向生成迁移文件的模板文件绝对路径
$config->setMigrateGenerateTemplate(EASYSWOOLE_ROOT . '/vendor/easyswoole/db-migrate/src/Resource/migrate_generate._php');
// 逆向生成迁移模板SQL语句的DDL代码块
$config->setMigrateTemplateDdlSyntax("DDLSyntax");
\EasySwoole\DatabaseMigrate\MigrateManager::getInstance($config);



use EasySwoole\DDL\Blueprint\Create\Table as CreateTable;
use EasySwoole\DDL\Blueprint\Alter\Table as AlterTable;
use EasySwoole\DDL\Blueprint\Drop\Table as DropTable;
use EasySwoole\DDL\DDLBuilder;
use EasySwoole\DDL\Enum\Character;
use EasySwoole\DDL\Enum\Engine;

/**
 * migrate create
 * Class User
 */
class User
{
    /**
     * migrate run
     * @return string
     */
    public function up()
    {
        return DDLBuilder::create('User',function (CreateTable $table){
            $table->setIfNotExists(true);
        });
    }

    /**
     * migrate rollback
     * @return string
     */
    public function down()
    {
        return DDLBuilder::dropIfExists('User');
    }
}