mysqlphp开发技巧

phplaravel 表迁移并使用mysql分区

2023-09-06  本文已影响0人  顶尖少爷

直接上代码
我是用command 创建了一张表

<?php

namespace App\Console\Commands\CreateTable;

use App\Modules\Game\Repositories\GameRepository;
use Illuminate\Console\Command;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Brokenice\LaravelMysqlPartition\Models\Partition;
use Brokenice\LaravelMysqlPartition\Schema\Schema;
use Log;
class CreateTabUserPayGameTable extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migrate:tab_user_pay_game';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '创建游戏的订单详细表(支持按照年分区从2021年开启创建30年)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(GameRepository $gameRepository)
    {
        //先创建表在创建分区
        //按照创建时间天分区
        //一个表最多只能有 1024 个分区
     
   Schema::create($tableName, function (Blueprint $table) {
                    $table->bigInteger('id');
                    $table->string("pay_order_number",100)->index()->default("")->comment("订单号");
                    $table->dateTime('created_at', 0)->nullable();
                    $table->timestamp('updated_at', 0)->nullable();
                    $table->softDeletes();
                    $table->unique(["pay_order_number","created_at"]);
                    $table->primary(["id","created_at"]);
                });
                //强制id自增
                Schema::forceAutoIncrement($tableName, 'id');
              //按照年和月分区                Schema::partitionByYearsAndMonths($tableName,"created_at",2022,2080);
                Log::Info("创建支付表成功",["tableName"=>$tableName]);
        return Command::SUCCESS;
    }
}
上一篇下一篇

猜你喜欢

热点阅读