Yii migration迁移工具的使用

这篇博客介绍了如何利用Yii2的yiisql工具进行MySQL数据库的管理,包括安装PHP、Composer,配置Yii框架,以及创建和管理数据库迁移。详细步骤涵盖了从安装必要软件到执行迁移命令的整个流程,并提供了中心数据库和游戏数据库的配置示例。
摘要由CSDN通过智能技术生成

工具目录

yiisql 目录

如果熟悉 php 可以用 yiisql 管理 mysql 的建表、表结构的修改。

安装 php

1.版本需要 >= 7.4.0。

2.下载地址

3.安装完后将 php 的安装目录的路径加到环境变量。

安装 composer

  1. 安装教程

  2. 修改镜像地址

    composer config -g repo.packagist composer https://packagist.phpcomposer.com
    

安装 Yii

1.官网地址

2.命令

 composer create-project --prefer-dist yiisoft/yii2-app-basic basic

主要配置和目录

1.目录YiiRootDirectory/migration

|--YiiRootDirectory
	|--config
		|-- db.php
		|-- migration.php
		|-- console.php
	|--migration
		|--center   -------- 中心数据库的表格(迁移记录)
		|--game     -------- 游戏数据库的表格(迁移记录)

2.yiisql/config/db.php

<?php

return [
    'components' => [      
        'dbGame' => [  // 游戏数据库
            'class' => yii\db\Connection::class,
            'dsn' => 'mysql:host=localhost;dbname=fd_game',
            'username' => 'root',
            'password' => '123456',
            'charset' => 'utf8',
            // Schema cache options (for production environment)
            //'enableSchemaCache' => true,
            //'schemaCacheDuration' => 60,
            //'schemaCache' => 'cache',
        ],
        'dbCenter' => [   // 中心数据库
            'class' => yii\db\Connection::class,
            'dsn' => 'mysql:host=localhost;dbname=fd_center',
            'username' => 'root',
            'password' => '123456',
            'charset' => 'utf8',
            // Schema cache options (for production environment)
            //'enableSchemaCache' => true,
            //'schemaCacheDuration' => 60,
            //'schemaCache' => 'cache',
        ],
    ],
];

3.迁移配置 migration.php

<?php
// https://www.yiichina.com/doc/guide/2.0/db-migrations
return [
    'controllerMap' => [
        'migrate-center' => [   // 组件名称
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => ['app\migration\center'],   // 迁移类文件的命名空间
            'migrationTable' => 'migration',    // 迁移表的名字
            'migrationPath' => null,   // 使用了命名空间,这里为 null
            'db' => 'dbCenter'    // 数据库连接, 见上面 db.php
        ],
        'migrate-game' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationNamespaces' => ['app\migration\game'],
            'migrationTable' => 'migration',
            'migrationPath' => null,
            'db' => 'dbGame'
        ]
    ],
];
  1. console.php 配置
<?php

$params = require __DIR__ . '/params.php';
$db = require __DIR__ . '/db.php';
$migration = require __DIR__ . '/migration.php';

$config = [
    'id' => 'basic-console',
    'basePath' => dirname(__DIR__),
    'bootstrap' => ['log'],
    'controllerNamespace' => 'app\commands',
    'aliases' => [
        '@bower' => '@vendor/bower-asset',
        '@npm'   => '@vendor/npm-asset',
        '@tests' => '@app/tests',
    ],
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'log' => [
            'targets' => [
                [
                    'class' => 'yii\log\FileTarget',
                    'levels' => ['error', 'warning'],
                ],
            ],
        ],
    ],
    'params' => $params, 
];

if (YII_ENV_DEV) {
    // configuration adjustments for 'dev' environment
    $config['bootstrap'][] = 'gii';
    $config['modules']['gii'] = [
        'class' => 'yii\gii\Module',
    ];
}

$config = \yii\helpers\ArrayHelper::merge($config, $db, $migration);

return $config;

命令操作

1.终端打开 yiisql目录。

2.具体命令见 Yii Migration

3.举例

 -- 中心服创建迁移
 php yii migrate-center/create  create_acccount
 
-- 中心服提交迁移
 php yii migrate-center 
  
 -- 中心服还原前一条迁移
 php yii migrate-center/down 1 
 
 
 -- 游戏服创建迁移
 php yii migrate-game/create  create_actor
 
-- 游戏服提交迁移
 php yii migrate-game 
  
 -- 游戏服还原前一条迁移
 php yii migrate-game/down 1 
 

center服的表 create_bulletin 迁移代码

<?php

namespace app\migration\center;

use yii\db\Migration;

/**
 * Class M210421030007CreateBulletin
 */
class M210421030007CreateBulletin extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $table_name = 'bulletin';

        $this->createTable($table_name, [
            'id' => $this->integer(),
        ]);
        $this->addPrimaryKey('id', 'bulletin', 'id');

        $this->addColumn($table_name, "bulletins", "blob");
        $this->addCommentOnColumn($table_name, "bulletins", "公告内容");

        $this->execute("alter table {$table_name} add column `create_time` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP");
        $this->execute("alter table {$table_name} add column `update_time` timestamp(0) NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP(0)");
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropTable('bulletin');
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值