在实际开发中,我们可能经常会遇到一个项目会建立两个 Laravel 项目,一个是面向用户的 web/API,一个是管理员后台,这两个项目一般情况下是共用一个数据库的,那么我们的 migration 可以共用吗?该怎么操作?

在各项目里建各自 migration

我们先在 web/APIadmin 里都建各自的 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,在团队协作的时候也不会混乱和出现冲突

标签: php, laravel, migrations

仅有一条评论

  1. 高级

添加新评论