Laravel 的数据库迁移
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
生成遷移
--table 和 --create 選項可用來指定數(shù)據(jù)表的名稱,或是該遷移被執(zhí)行時會創(chuàng)建的新數(shù)據(jù)表。這些選項需在預(yù)生成遷移文件時填入指定的數(shù)據(jù)表:
php artisan make:migration create_users_table php artisan make:migration create_users_table --create=users php artisan make:migration add_votes_to_users_table --table=users添加字段
\database\migrations\2017_07_30_133748_create_users_table.php
<?phpuse Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;class CreateUsersTable extends Migration {/*** 運行數(shù)據(jù)庫遷移** @return void*/public function up(){//Schema::create('users',function (Blueprint $table){$table->increments('id')->comment('遞增ID');$table->string('email',60)->comment('會員Email');$table->string('phone',20)->comment('會員手機號');$table->string('username',60)->comment('用戶名');$table->string('password',32)->comment('用戶密碼');$table->char('rank',10)->comment('會員等級');$table->unsignedSmallInteger('sex')->comment('性別;0保密;1男;2女');$table->unsignedSmallInteger('status')->comment('用戶狀態(tài)');$table->ipAddress('last_ip')->default('0.0.0.0')->comment('最后一次登錄IP');$table->timeTz('last_login')->comment('最后一次登錄時間');$table->timestamps();});}/*** 回滾數(shù)據(jù)庫遷移** @return void*/public function down(){//Schema::drop('users');} }? ? 要創(chuàng)建一張新的數(shù)據(jù)表,可以使用 Schema facade 的 create 方法。create 方法接收兩個參數(shù):第一個參數(shù)為數(shù)據(jù)表的名稱,第二個參數(shù)為一個 閉包 ,此閉包會接收一個用于定義新數(shù)據(jù)表的 Blueprint 對象
? ?你可以方便地使用 hasTable 和 hasColumn 方法來檢查數(shù)據(jù)表或字段是否存在:
if (Schema::hasTable('users')) {// } if (Schema::hasColumn('users', 'email')) {// }? ? 如果你想要在一個非默認的數(shù)據(jù)庫連接中進行數(shù)據(jù)庫結(jié)構(gòu)操作,可以使用 connection 方法:
Schema::connection('foo')->create('users', function (Blueprint $table) {$table->increments('id'); });? ? 你可以在數(shù)據(jù)庫結(jié)構(gòu)構(gòu)造器上設(shè)置 engine 屬性來設(shè)置數(shù)據(jù)表的存儲引擎:
Schema::create('users', function (Blueprint $table) {$table->engine = 'InnoDB';$table->increments('id'); });? ? 重命名與刪除數(shù)據(jù)表
Schema::rename($from, $to);//重命名//刪除已存在的數(shù)據(jù)表 Schema::drop('users'); Schema::dropIfExists('users');創(chuàng)建字段
Schema::table('users', function (Blueprint $table) {$table->string('email'); });| $table->bigIncrements('id'); | 遞增 ID(主鍵),相當于「UNSIGNED BIG INTEGER」型態(tài)。 |
| $table->bigInteger('votes'); | 相當于 BIGINT 型態(tài)。 |
| $table->binary('data'); | 相當于 BLOB 型態(tài)。 |
| $table->boolean('confirmed'); | 相當于 BOOLEAN 型態(tài)。 |
| $table->char('name', 4); | 相當于 CHAR 型態(tài),并帶有長度。 |
| $table->date('created_at'); | 相當于 DATE 型態(tài) |
| $table->dateTime('created_at'); | 相當于 DATETIME 型態(tài)。 |
| $table->dateTimeTz('created_at'); | DATETIME (帶時區(qū)) 形態(tài) |
| $table->decimal('amount', 5, 2); | 相當于 DECIMAL 型態(tài),并帶有精度與基數(shù)。 |
| $table->double('column', 15, 8); | 相當于 DOUBLE 型態(tài),總共有 15 位數(shù),在小數(shù)點后面有 8 位數(shù)。 |
| $table->enum('choices', ['foo', 'bar']); | 相當于 ENUM 型態(tài)。 |
| $table->float('amount', 8, 2); | 相當于 FLOAT 型態(tài),總共有 8 位數(shù),在小數(shù)點后面有 2 位數(shù)。 |
| $table->increments('id'); | 遞增的 ID (主鍵),使用相當于「UNSIGNED INTEGER」的型態(tài)。 |
| $table->integer('votes'); | 相當于 INTEGER 型態(tài)。 |
| $table->ipAddress('visitor'); | 相當于 IP 地址形態(tài)。 |
| $table->json('options'); | 相當于 JSON 型態(tài)。 |
| $table->jsonb('options'); | 相當于 JSONB 型態(tài)。 |
| $table->longText('description'); | 相當于 LONGTEXT 型態(tài)。 |
| $table->macAddress('device'); | 相當于 MAC 地址形態(tài)。 |
| $table->mediumIncrements('id'); | 遞增 ID (主鍵) ,相當于「UNSIGNED MEDIUM INTEGER」型態(tài)。 |
| $table->mediumInteger('numbers'); | 相當于 MEDIUMINT 型態(tài)。 |
| $table->mediumText('description'); | 相當于 MEDIUMTEXT 型態(tài)。 |
| $table->morphs('taggable'); | 加入整數(shù)?taggable_id?與字符串?taggable_type。 |
| $table->nullableMorphs('taggable'); | 與?morphs()?字段相同,但允許為NULL。 |
| $table->nullableTimestamps(); | 與?timestamps()?相同,但允許為 NULL。 |
| $table->rememberToken(); | 加入?remember_token?并使用 VARCHAR(100) NULL。 |
| $table->smallIncrements('id'); | 遞增 ID (主鍵) ,相當于「UNSIGNED SMALL INTEGER」型態(tài)。 |
| $table->smallInteger('votes'); | 相當于 SMALLINT 型態(tài)。 |
| $table->softDeletes(); | 加入?deleted_at?字段用于軟刪除操作。 |
| $table->string('email'); | 相當于 VARCHAR 型態(tài)。 |
| $table->string('name', 100); | 相當于 VARCHAR 型態(tài),并帶有長度。 |
| $table->text('description'); | 相當于 TEXT 型態(tài)。 |
| $table->time('sunrise'); | 相當于 TIME 型態(tài)。 |
| $table->timeTz('sunrise'); | 相當于 TIME (帶時區(qū)) 形態(tài)。 |
| $table->tinyInteger('numbers'); | 相當于 TINYINT 型態(tài)。 |
| $table->timestamp('added_on'); | 相當于 TIMESTAMP 型態(tài)。 |
| $table->timestampTz('added_on'); | 相當于 TIMESTAMP (帶時區(qū)) 形態(tài)。 |
| $table->timestamps(); | 加入?created_at?和?updated_at?字段。 |
| $table->timestampsTz(); | 加入?created_at?and?updated_at?(帶時區(qū)) 字段,并允許為NULL。 |
| $table->unsignedBigInteger('votes'); | 相當于 Unsigned BIGINT 型態(tài)。 |
| $table->unsignedInteger('votes'); | 相當于 Unsigned INT 型態(tài)。 |
| $table->unsignedMediumInteger('votes'); | 相當于 Unsigned MEDIUMINT 型態(tài)。 |
| $table->unsignedSmallInteger('votes'); | 相當于 Unsigned SMALLINT 型態(tài)。 |
| $table->unsignedTinyInteger('votes'); | 相當于 Unsigned TINYINT 型態(tài)。 |
| $table->uuid('id'); | 相當于 UUID 型態(tài)。 |
字段修飾
Schema::table('users', function (Blueprint $table) {$table->string('email')->nullable(); });| ->after('column') | 將此字段放置在其它字段「之后」(僅限 MySQL) |
| ->comment('my comment') | 增加注釋 |
| ->default($value) | 為此字段指定「默認」值 |
| ->first() | 將此字段放置在數(shù)據(jù)表的「首位」(僅限 MySQL) |
| ->nullable() | 此字段允許寫入 NULL 值 |
| ->storedAs($expression) | 創(chuàng)建一個存儲的生成字段 (僅限 MySQL) |
| ->unsigned() | 設(shè)置?integer?字段為?UNSIGNED |
| ->virtualAs($expression) | 創(chuàng)建一個虛擬的生成字段 (僅限 MySQL) |
字段更新
Schema::table('users', function (Blueprint $table) {$table->string('phone',20)->change();$table->string('username',60)->->nullable()->change(); });重命名字段
Schema::table('users', function (Blueprint $table) {$table->renameColumn('from', 'to'); });字段移除
Schema::table('users', function (Blueprint $table) {$table->dropColumn(['last_ip', 'last_login']); });? ? ?在使用字段更新,重命名字段,字段移除之前,請務(wù)必在你的 composer.json文件require鍵名中添加<?"doctrine/dbal": "^2.5">值。然后composer update進行更新或
composer require doctrine/dbal創(chuàng)建索引
$table->string('email')->unique();| $table->primary('id'); | 加入主鍵。 |
| $table->primary(['first', 'last']); | 加入復(fù)合鍵。 |
| $table->unique('email'); | 加入唯一索引。 |
| $table->unique('state', 'my_index_name'); | 自定義索引名稱。 |
| $table->unique(['first', 'last']); | 加入復(fù)合唯一鍵。 |
| $table->index('state'); | 加入基本索引。 |
開啟和關(guān)閉外鍵約束
Schema::enableForeignKeyConstraints();Schema::disableForeignKeyConstraints();運行遷移
php artisan migrate在線上環(huán)境強制執(zhí)行遷移
php artisan migrate --force回滾遷移
若要回滾最后一次遷移,則可以使用 rollback 命令。此命令是對上一次執(zhí)行的「批量」遷移回滾,其中可能包括多個遷移文件:
php artisan migrate:rollback在 rollback 命令后加上 step 參數(shù),你可以限制回滾遷移的個數(shù)。例如,下面的命令將會回滾最后的 5 個遷移。
php artisan migrate:rollback --step=5migrate:reset 命令可以回滾應(yīng)用程序中的所有遷移:
php artisan migrate:reset使用單個命令來執(zhí)行回滾或遷移
migrate:refresh 命令不僅會回滾數(shù)據(jù)庫的所有遷移還會接著運行 migrate 命令。所以此命令可以有效的重新創(chuàng)建整個數(shù)據(jù)庫:
php artisan migrate:refresh // 刷新數(shù)據(jù)庫結(jié)構(gòu)并執(zhí)行數(shù)據(jù)填充 php artisan migrate:refresh --seed使用 refresh 命令并加上 step 參數(shù),你也可以限制執(zhí)行回滾和再遷移的個數(shù)。比如,下面的命令會回滾并再遷移最后的 5 個遷移:
php artisan migrate:refresh --step=5無法生成遷移文件
在 Laravel 項目中,由于測試,有時候用 PHP artisan make:migration create_xxx_table 創(chuàng)建數(shù)據(jù)庫遷移。如果把創(chuàng)建的遷移文件 database/migrations/2017_07_30_133748_create_xxx_table.php 文件給刪除了,再次執(zhí)行 php artisan make:migration create_xxx_table 會報錯:
[ErrorException] include(E:\laraver\vendor\composer/../../database/migrations/2017_07_30_133748_create_users_table.php): failed to open stream: No such file or directory重新運行?composer update?又可以執(zhí)行上面的命令了。
?
全棧工程師技術(shù)QQ交流群:368683534
轉(zhuǎn)載于:https://my.oschina.net/zhiqiangwang/blog/1492411
總結(jié)
以上是生活随笔為你收集整理的Laravel 的数据库迁移的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第一个WindowService服务
- 下一篇: JavaSE之ClassLoader