数据库迁移--迁移文件
2025年5月21日大约 2 分钟
前提
在修改字段之前,请确保将 doctrine/dbal 依赖添加到 composer.json 文件中。Doctrine DBAL 库用于确定字段的当前状态, 并创建对该字段进行指定调整所需的 SQL 查询:
composer require doctrine/dbal
任何项目都需要先创建数据库,设计数据库表.因此先整理数据库迁移
迁移文件
创建迁移文件模版
src\stubs\migration.create.stub
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//注意是否需要修改mysql连接名和表名
if (!Schema::connection('mysql')->hasTable('{{ table }}'))
{
Schema::connection('mysql')->create('{{ table }}', function (Blueprint $table)
{
$table->id()->comment('主键');
$table->unsignedBigInteger('revision')->default(0)->comment('乐观锁');
$table->unsignedBigInteger('admin_id')->default(0)->comment('管理员id');
$table->unsignedtinyInteger('type')->default(0)->comment('类型');
$table->string('name',32)->unique()->nullable()->comment('名称 唯一');
$table->string('name',32)->default('')->comment('名称');
$table->char('char',12)->default('')->comment('名称');
$table->unsignedTinyInteger('deep')->index()->default(0)->comment('');
$table->decimal('price',32,8,true)->default(0)->comment('商品价格');
$table->string('note',128)->default('')->comment('备注');
$table->unsignedTinyInteger('sort')->default(100)->comment('排序');
$table->dateTime('created_at')->useCurrent()->comment('创建时间string');
$table->unsignedInteger('created_time')->index(0)->default(0)->comment('创建时间int');
$table->dateTime('updated_at')->nullable()->comment('更新时间string');
$table->unsignedInteger('updated_time')->default(0)->comment('更新时间int');
$table->dateTime('deleted_at')->nullable()->comment('删除时间string');
$table->unsignedInteger('deleted_time')->default(0)->comment('删除时间int');
});
//注意是否需要修改mysql连接名
$prefix = config('database.connections.mysql.prefix');
DB::statement("ALTER TABLE `{$prefix}{{table}}` comment '表名'");
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (Schema::connection('mysql')->hasTable('{{ table }}'))
{
Schema::connection('mysql')->dropIfExists('{{ table }}');
}
}
};
修改迁移文件模版
stubs\migration.update.stub
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//注意是否需要修改mysql连接名和表名
if (Schema::connection('mysql')->hasTable('{{ table }}'))
{
if (!Schema::hasColumn('{{ table }}', 'revision'))
{
Schema::connection('mysql')->table({{ table }}, function (Blueprint $table)
{
$table->unsignedBigInteger('revision')->default(0)->comment('乐观锁')->after('id');
$table->string('remember_token',128)->unique()->nullable()->comment('登录token')->after('revision');
$table->dateTime('created_at')->useCurrent()->comment('创建时间string');
$table->unsignedInteger('created_time')->index(0)->default(0)->comment('创建时间int');
$table->dateTime('updated_at')->nullable()->comment('更新时间string');
$table->unsignedInteger('updated_time')->index(0)->default(0)->comment('跟新时间int');
$table->dateTime('deleted_at')->nullable()->comment('删除时间string');
});
}
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
if (Schema::connection('mysql')->hasTable('{{ table }}'))
{
if (Schema::hasColumn('{{ table }}', 'revision'))
{
$table->dropColumn([
'revision',
'remember_token',
'created_at',
'created_time',
'created_time',
'updated_at',
'updated_time',
'deleted_at'
]);
}
}
}
}