简介
所谓迁移就像是数据库表结构的版本控制,这种机制允许团队轻松的编辑并共享数据库的表结构。
迁移通常和 Laravel 的 schema 构建器结合,从而可以很容易地构建应用的数据库表结构。
如果你曾经频繁告知团队成员需要手动添加字段到本地数据库的表结构以维护本地开发环境,那么这正是数据库迁移所致力于解决的问题。
Laravel 的 Schema 门面提供了与数据库系统无关的创建和操纵表的支持,在 Laravel 所支持的所有数据库系统中提供一致的、优雅的、流式的 API。
生成迁移
使用 Artisan 命令 make:migration 就可以创建一个新的迁移。
php artisan make:migration create_users_table
新的迁移位于 database/migrations 目录下,每个迁移文件名都包含时间戳,从而允许 Laravel 判断其顺序。
--table 和 --create 选项可以用于指定表名以及该迁移是否要创建一个新的数据表。这些选项只需要简单放在上述迁移命令后面并指定表名:
php artisan make:migration create_users_table --create=users
php artisan make:migration add_votes_to_users_table --table=users
如果你想要指定生成迁移的自定义输出路径,在执行 make:migration 命令时可以使用 --path 选项,提供的路径应该是相对于应用根目录的。
迁移的结构
迁移类(迁移文件)包含了两个方法:up 和 down。
up 方法用于新增表、字段或者索引到数据库。
而 down 方法就是 up 方法的逆操作,和 up 里的操作相反。
在这两个方法中,都会用到 Laravel 的 Schema 构建器。
下面是一个迁移类的示例:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
运行迁移
要运行应用中所有未执行的迁移,可以使用 Artisan 命令提供的 migrate 方法:
php artisan migrate
注:如果你正在使用 Homestead 虚拟机,需要在虚拟机中运行上面这条命令。
在生产环境中强制运行迁移
有些迁移操作是毁灭性的,这意味着它们可能造成数据的丢失,为了避免在生产环境数据库中运行这些命令,你将会在运行这些命令之前被提示并确认。想要强制运行这些命令而不被提示,可以使用 --force 标记:
php artisan migrate --force
回滚迁移
想要回滚最新的一次迁移”操作“,可以使用 rollback 命令,注意这将会回滚最后一批运行的迁移,可能包含多个迁移文件。
php artisan migrate:rollback
也可以通过 rollback 命令上提供的 step 选项来回滚指定数目的迁移。
例如,下面的命令将会回滚最后五条迁移:
php artisan migrate:rollback --step=5
migrate:reset 命令将会回滚所有的迁移。
php artisan migrate:reset
在单条命令中回滚 & 迁移
migrate:refresh 命令将会先回滚所有数据库迁移,然后运行 migrate 命令。它可以有效的重建整个数据库。
php artisan migrate:refresh
// 重建数据库并填充数据...
php artisan migrate:refresh --seed
当然,你也可以回滚或重建指定数量的迁移 —— 通过 refresh 命令提供的 step 选项,例如,下面的命令将会回滚或重建最后五条迁移:
php artisan migrate:refresh --step=5
删除所有表 & 迁移
migrate:fresh 命令会先从数据库中删除所有表然后执行 migrate 命令:
php artisan migrate:fresh
php artisan migrate:fresh --seed
数据表
创建表
使用 Schema 门面上的 create 方法来创建新的数据表。
create 方法接收两个参数,第一个是表名,第二个是获取用于定义新表的 Blueprint 对象的闭包。
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
创建新表时,可以使用 Schema 构建器中的任意列方法来定义数据表的列。
检查表/列是否存在
使用 hasTable 和 hasColumn 方法检查表或列是否存在:
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) |
重命名/删除表
要重命名一个已存在的数据表,使用 rename 方法:
Schema::rename($from, $to);
要删除一个已存在的数据表,可以使用 drop 或 dropIfExists 方法:
Schema::drop('users');
Schema::dropIfExists('users');
数据列(字段)
要更新一个已存在的表,可以使用 Schema 门面上的 table 方法。
table 方法接收两个参数:表名和获取用于添加列到表的 Blueprint 实例的闭包。
Schema::table('users', function (Blueprint $table) {
$table->string('email');
});
可用的字段类型
命令 | 说明 |
---|---|
$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->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 类型列 |
列修改器
除了上面列出的数据列类型之外,在添加列的时候还可以使用一些其它的列“修改器”,例如,要使列允许为 NULL,可以使用 nullable 方法。
Schema::table('users', function (Blueprint $table) {
$table->string('email')->nullable();
});
下面是所有可用的列修改器,该列表不包含索引修改器:
修改器 | 说明 |
---|---|
after('column') | 将该列置于另一个列之后 (MySQL) |
autoIncrement() | 设置为自增 |
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() | 设置为 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();
});
注:只有以下数据列类型能修改:bigInteger, binary, boolean, date, dateTime, dateTimeTz, decimal, integer, json, longText, mediumText, smallInteger, string, text, time, unsignedBigInteger, unsignedInteger 和 unsignedSmallInteger。
重命名列:
要重命名一个列,可以使用表结构构建器上的 renameColumn 方法。
Schema::table('users', function (Blueprint $table) {
$table->renameColumn('from', 'to');
});
注:暂不支持 enum 类型的列的修改和重命名。
删除数据列
要删除一个列,使用 Schema 构建器上的 dropColumn 方法。
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('votes');
});
可以通过传递列名数组到 dropColumn 方法以便可以一次从数据表中删除多个列:
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['votes', 'avatar', 'location']);
});
注:SQLite 数据库暂不支持在单个迁移中删除或修改多个列。
除了 dropColumn 方法外,还有一些其他删除字段的方法:
方法 | 说明 |
---|---|
$table->dropRememberToken(); | 删除 remember_token 列 |
$table->dropSoftDeletes(); | 删除 deleted_at 列 |
$table->dropSoftDeletesTz(); | dropSoftDeletes() 方法别名 |
$table->dropTimestamps(); | 删除 created_at 和 updated_at 列 |
$table->dropTimestampsTz(); | dropTimestamps() 方法别名 |
索引
创建索引
Schema 构建器支持多种类型的索引,首先,让我们看一个指定唯一索引的例子。要创建该索引,可以使用 unique 方法:
// 定义列的同时指定唯一索引
$table->string('email')->unique();
此外,你可以在定义列之后创建索引,例如:
$table->unique('email');
可以传递列名数组到 index 方法来创建组合索引:
$table->index(['account_id', 'created_at']);
Laravel 会自动生成合理的索引名称,不过你也可以传递第二个参数到该方法用于指定索引名称:
$table->index('email', 'unique_email');
可用的索引类型
方法 | 说明 |
---|---|
$table->primary('id'); | 添加主键索引 |
$table->primary(['first', 'last']); | 添加组合索引 |
$table->unique('email'); | 添加唯一索引 |
$table->index('state'); | 添加普通索引 |
$table->spatialIndex('location'); | 添加空间索引(不支持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
*/
public function boot()
{
Schema::defaultStringLength(191);
}
作为可选方案,你也可以为数据库启用 innodb_large_prefix 选项,至于如何合理启用这个选项,可以参考数据库文档说明。
删除索引
要删除索引,必须指定索引名称。默认情况下,Laravel 自动分配适当的名称给索引 —— 连接表名、列名和索引类型。
方法 | 说明 |
---|---|
$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) |
如果要传递数据列数组到删除索引的方法,那么相应的索引名称应该是默认的数据表名、列和索引类型的拼接。
Schema::table('geo', function (Blueprint $table) {
$table->dropIndex(['state']); // Drops index 'geo_state_index'
});
外键约束
Laravel 还提供了创建外键约束的支持,用于在数据库层面强制引用的完整性。
例如,我们在 posts 表中定义了一个引用 users 表 id 列的 user_id 列:
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();
注:由于使用外键时,级联删除风险较高,一般情况下我们很少使用外键,而是通过代码逻辑来实现级联操作。