暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

试一试数据库迁移工具吧!

蒲东平 2023-12-18
105

数据库迁移的好处

  1. 多人并行开发
  2. 代码版本管理
  3. 数据库版本控制,如:回滚/重置/更新等。
  4. 兼容多种数据库系统(哪天你不想用 mysql,想用 pqsql, sqlite 甚至 oracle 都会由 ORM 替你生成建表语句。)
  5. 部署方便

数据库迁移工具简介

迁移就像是数据库的版本控制, 允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和 Laravel 的 数据库结构生成器配合使用,让你轻松地构建数据库结构。如果你曾经试过让同事手动在数据库结构中添加字段,那么数据库迁移可以让你不再需要做这样的事情。

迁移工具如何使用?

命令流程

1. 生成迁移

使用 Artisan 命令 make:migration 来创建迁移

// 比如说创建一张 users 表

php artisan make:migration create_users_table

新的迁移位于 database/migrations
目录下。每个迁移文件名都包含时间戳,以便让 Laravel 确认迁移的顺序。

2.万一我不想要将迁移文件放入 database/migrations
目录下怎么办?

如果你想要指定生成迁移指定一个自定义输出路径,则可以在运行 make:migration 命令时添加 --path 选项,给定的路径必须是相对于应用程序的基本路径。

3.默认迁移文件

Laravel 已默认为我们创建好了两个迁移文件,一个用于构建用户表,一个用于构建密码重置表:

  1. database/migrations/2014_10_12_000000_create_users_table.php
  2. database/migrations/2014_10_12_100000_create_password_resets_table.php

database/migrations/2014_10_12_000000_create_users_table.php

<?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');
    }
}

CreateUsersTable 有两个方法 up 和 down :

  1. 当我们运行迁移时,up 方法会被调用(创建表)
  2. 当我们回滚迁移时,down 方法会被调用(删除表)
4.运行迁移

使用 Artisan命令 migrate 方法来运行所有未完成的迁移:

php artisan migrate

5.能够随意执行“破坏性”的迁移命令吗?比如回滚

一些迁移操作是具有破坏性的, 这意味着可能会导致数据丢失。为了防止有人在生产环境中运行这些命令, 系统会在这些命令被运行之前与你进行确认。

6.回滚迁移

若要回滚最后一次迁移, 可以使用 rollback 命令。此命令将回滚最后一次“迁移”的操作,其中可能包含多个迁移文件:

php artisan migrate:rollback

// 回滚最后5步迁移
php artisan migrate:rollback --step=5

// 回滚所有的迁移
php artisan migrate:reset

数据表

1.创建数据表

可以使用 Schema facade的 create 方法来创建新的数据库表。

// 创建 users 表
Schema::create('users'function (Blueprint $table) {
    $table->increments('id');
});

2.检查数据表 字段是否存在

可以使用 hasTable 和 hasColumn 方法来检查数据表或字段是否存在:

// 检查 users 表是否存在
if (Schema::hasTable('users')) {
    //
}

// 检查 users 表中的 email 字段是否存在
if (Schema::hasColumn('users''email')) {
    //
}

3.数据库连接 & 表选项

如果要对非默认连接的数据库连接执行结构操作,可以使用 connection 方法:

// 连接 foo 库的 users 表
Schema::connection('foo')->create('users'function (Blueprint $table) {
    $table->increments('id');
});

4.重命名 删除数据表

若要重命名数据表,可以使用 rename 方法:

Schema::rename($from, $to);

删除数据表, 可使用 drop 或 dropIfExists 方法:

Schema::drop('users');

Schema::dropIfExists('users');

字段

1.创建字段

使用 Schema facade 的 table 方法可以更新现有的数据表。

Schema::table('users'function (Blueprint $table) {
    $table->string('email');
});

2.修改字段
  • 重命名字段 可以使用结构生成器上的 renameColumn 方法来重命名字段。
// 修改 users 表的字段 from 为 to
Schema::table('users'function (Blueprint $table) {
    $table->renameColumn('from''to');
});

  • 更新字段属性 change 方法可以将现有的字段类型修改为新的类型或修改属性。
// 更改 users 表中的 name 长度为 50
Schema::table('users'function (Blueprint $table) {
    $table->string('name'50)->change();
});

  • 删除字段 可以使用结构生成器上的 dropColumn 方法来删除字段。
// 删除 users 表的 votes 字段
Schema::table('users'function (Blueprint $table) {
    $table->dropColumn('votes');
});


最后说个事
公号算法变了,为防止看不到我的更新
大家帮忙加个星标
点击上方的公众号卡片
再点右上角三个点
就能看到设为星标
算我跪下来求你们

往期精选:

微信,看看你的另一半跟谁聊天频繁!

让你的微信“拍一拍”有趣且不失风度

微信年度账单来了,不敢看!

张万林,下雪了……我用编程带你看场纷飞大雪

还在使用默认的微信图标?赶紧换个吧!

我的微信和你们的不一样!?

文章转载自蒲东平,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论