温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Laravel项目共用migrations

发布时间:2020-12-28 15:58:23 来源:亿速云 阅读:130 作者:Leah 栏目:开发技术

今天就跟大家聊聊有关怎么在Laravel项目共用migrations,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

在各项目里建各自 migration

我们先在 web/API 和 admin 里都建各自的 migration:

## web 目录

php artisan make:migration foo
# Created Migration: 2018_09_19_144940_foo

php artisan migrate
# Migration table created successfully.
# Migrating: 2018_09_19_144940_foo
# Migrated: 2018_09_19_144940_foo

php artisan migrate:status
# +------+-----------------------+-------+
# | Ran? | Migration    | Batch |
# +------+-----------------------+-------+
# | Yes | 2018_09_19_144940_foo | 1  |
# +------+-----------------------+-------+

## admin 目录

php artisan make:migration bar
# Created Migration: 2018_09_19_145255_bar

php artisan migrate
# Migrating: 2018_09_19_145255_bar
# Migrated: 2018_09_19_145255_bar

php artisan migrate:status
# +------+-----------------------+-------+
# | Ran? | Migration    | Batch |
# +------+-----------------------+-------+
# | Yes | 2018_09_19_144940_foo | 1  |
# +------+-----------------------+-------+
# | Yes | 2018_09_19_145255_bar | 2  |
# +------+-----------------------+-------+

从 artisan migrate:status 的结果来看,两个 migration 都正常执行了,接下来我们试一下回滚操作。

先直接在 web 目录执行

php artisan migrate:rollback
# Migration not found: 2018_09_19_145255_bar

报错了,因为在 web 项目里找不到 bar 这个 migration 文件;那如果我们刚刚是直接在 admin 目录执行,是能够正常回滚的,但是如果我们指定回滚两个版本:

php artisan migrate:rollback --step=2

# Migration not found: 2018_09_19_144940_foo
# Rolling back: 2018_09_19_145255_bar
# Rolled back: 2018_09_19_145255_bar

这次回滚操作也是有问题的,只回滚了一半。

所以我们应该按照 migrate 的相反顺序执行回滚,即先在 admin 执行一次,然后再到 web 里再执行一次。我们上面的实验很简单,要记住这些顺序也不难,可是在实际的项目中,你的 migrations 就比这个复杂多了,而且只通过 migrate:status 你也看不出来执行顺序到底是怎么样的,所以在各个项目里各自维护各自的 migrations 似乎行不通...

共用一份 migration

上面的实验我们可以知道,我们在执行 artisan migrate 的时候,Laravel 会读取 migrations 目录里的文件和数据库里的记录,然后再执行相应的操作(并记录这次操作);回滚的时候 Laravel 会读取数据库中的记录,然后执行 migrations 目录里相应的文件中的 down 方法。

而当 migrations 分散在不同的项目(目录)里的时候,不管你在哪个项目中执行 migrate:rollback 时,都可能只有一部分 migration 文件被加载进来,因此会造成一些奇奇怪怪的问题。

那我们可以将所有 migrations 放在同一个地方,怎么操作呢?再建一个新的项目似乎有点麻烦了...我们先看看帮助吧:

php artisan migrate --help

Description:
 Run the database migrations

Usage:
 migrate [options]

Options:
  --database[=DATABASE] The database connection to use
  --force    Force the operation to run when in production
  --path[=PATH]   The path to the migrations files to be executed
  --realpath    Indicate any provided migration file paths are pre-resolved absolute paths
  --pretend    Dump the SQL queries that would be run
  --seed     Indicates if the seed task should be re-run
  --step     Force the migrations to be run so they can be rolled back individually
 -h, --help     Display this help message
 -q, --quiet    Do not output any message
 -V, --version    Display this application version
  --ansi     Force ANSI output
  --no-ansi    Disable ANSI output
 -n, --no-interaction  Do not ask any interactive question
  --env[=ENV]   The environment the command should run under
 -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

果然有我们想要的东西:--path 和 --realpath,先来看看这两个参数是什么用途:

--path[=PATH]  指定 migrations 文件的路径
--realpath     表示 --path 指定的路径为绝对路径

那我们在进行 migrations 操作的时候,指定同一个路径,那就可以共用 migrations 了:

php artisan make:migration foo --path="../admin/database/migrations"

# or

php artisan make:migration foo --path="/the/absolute_path/to/admin/database/migrations" --realpath


# migrate
php artisan migrate --path="../admin/database/migrations"

# migrate:rollback
php artisan migrate:rollback --path="../admin/database/migrations"

注:当你不带 --realpath 的时候,path 是以项目的根目录为 / 的

总结

所以,当我们需要在多个 Laravel 项目中共用 migrations 的时候,最好的做法是通过 --path 指定 migrations 文件的目录,这个目录可以是一个独立的 git repo,也可以是其中一个 Laravel 项目(我个人推荐放在其中一个项目中,采用独立的 git 分支),这样既可以共用 migrations,在团队协作的时候也不会混乱和出现冲突

看完上述内容,你们对怎么在Laravel项目共用migrations有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI