四 hyperf 数据库迁移

328 阅读1分钟

一 创建迁移文件

php bin/hyperf.php gen:migration create_demo_table --create=demo

创建文件如下

<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateDemoTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('demo', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('tname', 50);
            $table->datetimes();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('demo');
    }
}

二 运行迁移文件

php bin/hyperf.php migrate

image.png

遇坑

image.png

修改.env 文件

DB_CHARSET=utf8mb4 DB_COLLATION=utf8mb4_general_ci

三 更新表结构

php bin/hyperf.php gen:migration create_demo_table --table=demo

<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::table('demo', function (Blueprint $table) {
            $table->integer('created_at')->change();
            $table->integer('updated_at')->change();
            $table->integer('deleted_at')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('demo', function (Blueprint $table) {
            //
        });
    }
}


php bin/hyperf.php migrate

image.png