第二章 数据库迁移 —— 以版本控制的方式维护数据表

一.简介

所谓迁移就像是 数据库的版本控制

这种机制允许团队简单轻松的编辑并共享应用的数据库表结构。如果你曾经频繁告知团队成员需要手动添加列到本地数据库表结构以维护本地开发环境,那么这正是数据库迁移所致力于解决的问题。

迁移通常和 Laravel 的 schema 构建器结合,从而可以很容易地构建应用的数据库表结构。

二.生成迁移文件

新的迁移位于 database/migrations 目录下,每个迁移文件名都包含时间戳从而允许 Laravel 判断其顺序

  • 创建Model类时,附带‘-m’参数同时创建迁移文件。
php artisan make:model UserModel -m
  • 直接生成迁移文件
php artisan make:migration create_users_table
  • 指定表名称
php artisan make:migration add_votes_to_users_table --table=users
  • 创建一个新的数据表
php artisan make:migration create_users_table --create=users

三.迁移结构

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFlightsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
}

up 方法用于新增表,列或者索引到数据库;down反之

四.运行迁移

php artisan migrate
php artisan migrate --force //强制

五.回滚迁移

  • 按照迁移顺序回滚
php artisan migrate:rollback          //回滚最后一次迁移
php artisan migrate:rollback --step=5 //回滚最后五条迁移
php artisan migrate:reset             //回滚所有的应用迁移
  • 在单个命令中回滚 & 迁移
php artisan migrate:refresh  //命令将会先回滚所有数据库迁移,然后运行 migrate 命令。这个命令可以有效的重建整个数据库

php artisan migrate:refresh --seed  // 重建数据库并填充数据...

php artisan migrate:refresh --step=5 //回滚或重建最后五条迁移
  • 删除所有表 & 迁移
php artisan migrate:fresh  // 命令将会先从数据库中删除所有表然后执行 migrate 命令

php artisan migrate:fresh --seed // 填充

六.填充假数据到数据库

详解三.迁移结构

  • 创建表、字段
Schema::create('users', function ($table) {
    $table->increments('id');
});
  • 检查表/列是否存在
if (Schema::hasTable('users')) {
    //
}

if (Schema::hasColumn('users', 'email')) {
    //
}
  • 数据库连接 & 表选项
// 如果你想要在一个数据库连接上执行表结构操作,而该数据库连接并不是默认数据库连接,可以使用 connection 方法:

Schema::connection('foo')->create('users', function (Blueprint $table) {
    $table->increments('id');
});
// 要设置表的存储引擎、字符编码等选项,可以在 Schema 构建器上使用如下命令:

$table->engine = 'InnoDB'; // 指定表的存储引擎(MySQL)
$table->charset = 'utf8';  // 指定数据表的默认字符集(MySQL)
$table->collation = 'utf8_unicode_ci'; // 指定数据表的字符序(MySQL)
$table->temporary();   // 创建临时表(除SQL Server)
  • 重命名/删除表
Schema::rename($from, $to); // 重命名表名

Schema::drop('users');  // 删除表
Schema::dropIfExists('users');

七.数据列

命令 描述
$table->bigIncrements(‘id’); 等同于自增 UNSIGNED BIGINT(主键)列
$table->bigInteger(‘votes’); 等同于 BIGINT 类型列
$table->binary(‘data’); 等同于 BLOB 类型列
$table->boolean(‘confirmed’); 等同于 BOOLEAN 类型列
$table->char(‘name’, 4); 等同于 CHAR 类型列
$table->date(‘created_at’); 等同于 DATE 类型列
$table->dateTime(‘created_at’); 等同于 DATETIME 类型列
$table->dateTimeTz(‘created_at’); 等同于 DATETIME 类型(带时区)列
$table->decimal(‘amount’, 5, 2); 等同于 DECIMAL 类型列,带精度和范围
$table->double(‘column’, 15, 8); 等同于 DOUBLE 类型列,带精度, 总共15位数字,小数点后8位
$table->enum(‘level’, [‘easy’, ‘hard’]); 等同于 ENUM 类型列
$table->float(‘amount’, 8, 2); 等同于 FLOAT 类型列,带精度和总位数
$table->geometry(‘positions’); 等同于 GEOMETRY 类型列
$table->geometryCollection(‘positions’); 等同于 GEOMETRYCOLLECTION 类型列
$table->increments(‘id’); 等同于自增 UNSIGNED INTEGER (主键)类型列
$table->integer(‘votes’); 等同于 INTEGER 类型列
$table->ipAddress(‘visitor’); 等同于 IP 地址类型列
$table->json(‘options’); 等同于 JSON 类型列
$table->jsonb(‘options’); 等同于 JSONB 类型列
$table->lineString(‘positions’); 等同于 LINESTRING 类型列
$table->longText(‘description’); 等同于 LONGTEXT 类型列
$table->macAddress(‘device’); 等同于 MAC 地址类型列
$table->mediumIncrements(‘id’); 等同于自增 UNSIGNED MEDIUMINT 类型列(主键)
$table->mediumInteger(‘numbers’); 等同于 MEDIUMINT 类型列
$table->mediumText(‘description’); 等同于 MEDIUMTEXT 类型列
$table->morphs(‘taggable’); 添加一个 UNSIGNED INTEGER 类型的 taggable_id 列和一个 VARCHAR 类型的 taggable_type 列
$table->multiLineString(‘positions’); 等同于 MULTILINESTRING 类型列
$table->multiPoint(‘positions’); 等同于 MULTIPOINT 类型列
$table->multiPolygon(‘positions’); 等同于 MULTIPOLYGON 类型列
$table->nullableMorphs(‘taggable’); morphs() 列的 nullable 版本
$table->nullableTimestamps(); timestamps() 的别名
$table->point(‘position’); 等同于 POINT 类型列
$table->polygon(‘positions’); 等同于 POLYGON 类型列
$table->rememberToken(); 等同于添加一个允许为空的 remember_token VARCHAR(100) 列
$table->smallIncrements(‘id’); 等同于自增 UNSIGNED SMALLINT (主键)类型列
$table->smallInteger(‘votes’); 等同于 SMALLINT 类型列
$table->softDeletes(); 新增一个允许为空的 deleted_at TIMESTAMP 列用于软删除
$table->softDeletesTz(); 新增一个允许为空的 deleted_at TIMESTAMP (带时区)列用于软删除
$table->string(‘name’, 100); 等同于 VARCHAR 类型列,带一个可选长度参数
$table->text(‘description’); 等同于 TEXT 类型列
$table->time(‘sunrise’); 等同于 TIME 类型列
$table->timeTz(‘sunrise’); 等同于 TIME 类型(带时区)
$table->timestamp(‘added_on’); 等同于 TIMESTAMP 类型列
$table->timestampTz(‘added_on’); 等同于 TIMESTAMP 类型(带时区)列
$table->timestamps(); 添加允许为空的 created_at 和 updated_at TIMESTAMP 类型列
$table->timestampsTz(); 添加允许为空的 created_at 和 updated_at TIMESTAMP 类型列(带时区)
$table->tinyIncrements(‘numbers’); 等同于自增的 UNSIGNED TINYINT 类型列(主键)
$table->tinyInteger(‘numbers’); 等同于 TINYINT 类型列
$table->unsignedBigInteger(‘votes’); 等同于无符号的 BIGINT 类型列
$table->unsignedDecimal(‘amount’, 8, 2); 等同于 UNSIGNED DECIMAL 类型列,带有总位数和精度
$table->unsignedInteger(‘votes’); 等同于无符号的 INTEGER 类型列
$table->unsignedMediumInteger(‘votes’); 等同于无符号的 MEDIUMINT 类型列
$table->unsignedSmallInteger(‘votes’); 等同于无符号的 SMALLINT 类型列
$table->unsignedTinyInteger(‘votes’); 等同于无符号的 TINYINT 类型列
$table->uuid(‘id’); 等同于 UUID 类型列
$table->year(‘birth_year’); 等同于 YEAR 类型列

八.列修改

修改器 描述
->after(‘column’) 将该列置于另一个列之后 (MySQL)
->autoIncrement() 设置 INTEGER 列为自增主键
->charset(‘utf8’) 指定数据列字符集(MySQL)
->collation(‘utf8_unicode_ci’) 指定数据列字符序(MySQL/SQL Server)
->comment(‘my comment’) 添加注释信息
->default($value) 指定列的默认值
->first() 将该列置为表中第一个列 (MySQL)
->nullable($value = true) 允许该列的值为 NULL
->storedAs($expression) 创建一个存储生成列(MySQL)
->unsigned() 设置 INTEGER 列为 UNSIGNED(MySQL)
->useCurrent() 设置 TIMESTAMP 列使用 CURRENT_TIMESTAMP 作为默认值
->virtualAs($expression) 创建一个虚拟生成列(MySQL)

在修改列之前,确保已经将 doctrine/dbal 依赖添加到 composer.json 文件,Doctrine DBAL 库用于判断列的当前状态并创建对列进行指定调整所需的 SQL 语句:
composer require doctrine/dbal

更新列属性

// change 方法允许你修改已存在的列为新的类型,或者修改列的属性。例如,你可能想要增加 字符串类型列的尺寸,下面让我们将 name 列的尺寸从 25 增加到 50:
Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

// 我们还可以修改该列允许 NULL 值:
Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->nullable()->change();
});


// 重命名。注:暂不支持 enum 类型的列的修改和重命名。
Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});


// 删除数据列
Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('votes');
});

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});
命令 描述
$table->dropRememberToken(); 删除 remember_token 列
$table->dropSoftDeletes(); 删除 deleted_at 列
$table->dropSoftDeletesTz(); dropSoftDeletes() 方法别名
$table->dropTimestamps(); 删除 created_at 和 updated_at 列
$table->dropTimestampsTz(); dropTimestamps() 方法别名

九.索引

命令 描述
$table->primary(‘id’); 添加主键索引
$table->primary([‘first’, ‘last’]); 添加组合索引
$table->unique(‘email’); 添加唯一索引
$table->index(‘state’); 添加普通索引
$table->spatialIndex(‘location’); 添加空间索引(不支持SQLite)
$table->dropPrimary(‘users_id_primary’); 从 “users” 表中删除主键索引
$table->dropUnique(‘users_email_unique’); 从 “users” 表中删除唯一索引
$table->dropIndex(‘geo_state_index’); 从 “geo” 表中删除普通索引
$table->dropSpatialIndex(‘geo_location_spatialindex’); 从 “geo” 表中删除空间索引(不支持SQLite)

十.索引长度 & MySQL / MariaDB

Laravel 默认使用 utf8mb4 字符集,支持在数据库中存储 emoji 表情。如果你现在运行的 MySQL 版本低于 5.7.7(或者低于 10.2.2 版本的 MariaDB),需要手动配置迁移命令生成的默认字符串长度,以便 MySQL 为它们创建索引。你可以通过在 AppServiceProvider 中调用 Schema::defaultStringLength 方法来完成配置:

use Illuminate\Support\Facades\Schema;

/**
 * Bootstrap any application services.
 *
 * @return void
 * @translator laravelacademy.org
 */
public function boot()
{
    Schema::defaultStringLength(191);
}

十一.外键约束

Schema::table('posts', function (Blueprint $table) {
    $table->integer('user_id')->unsigned();
    $table->foreign('user_id')->references('id')->on('users');
});

// 你还可以为约束的“on delete”和“on update”属性指定期望的动作:
$table->foreign('user_id')
      ->references('id')->on('users')
      ->onDelete('cascade');

// 要删除一个外键,可以使用 dropForeign 方法。外键约束和索引使用同样的命名规则 —— 连接表名、外键名然后加上“_foreign”后缀:
$table->dropForeign('posts_user_id_foreign');

//或者,你还可以传递在删除时会自动使用基于惯例的约束名数值数组:
$table->dropForeign(['user_id']);

//你可以在迁移时通过以下方法启用或关闭外键约束:
Schema::enableForeignKeyConstraints();
Schema::disableForeignKeyConstraints();

猜你喜欢

转载自blog.csdn.net/DarkAngel1228/article/details/81225229
今日推荐