如何在 laravel 中为特定环境运行迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13491536/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I run migrations for a specific environment in laravel
提问by duellsy
I'm setting up a new app with laravel (Laravel 4), and having some issues setting up the database via migrations.
我正在使用 Laravel(Laravel 4)设置一个新应用程序,并且在通过迁移设置数据库时遇到了一些问题。
I made a migration file with:
我制作了一个迁移文件:
artisan migrate:make --table="jobs" --create jobs
Which created a file in database/migrations as expected, I made some mods to this, and went to fire it up using
它按预期在数据库/迁移中创建了一个文件,我对此做了一些修改,然后使用
artisan migrate --env=local
But I'm getting the reply of "Nothing to migrate"
但我收到了回复 "Nothing to migrate"
If I try run without --env=local
, it uses the database.php in the config folder (not in the local / staging / production folder) which we don't want to use, as it won't be environment specific.
如果我尝试不--env=local
使用运行,它会使用我们不想使用的 config 文件夹(不在本地 / staging / production 文件夹中)中的 database.php,因为它不是特定于环境的。
My first thought was OK, maybe I need to put the env flag on the migrate:make
call, so I tried that, but got an error saying it couldn't create the migration file. Then I thought it doesn't make sense to make env based migrations anyway... they should be created generic, and simply run on a per env basis, so in the end, all environments are using the same migration scripts.
我的第一个想法是好的,也许我需要在migrate:make
调用中放置 env 标志,所以我尝试了这个,但得到一个错误,说它无法创建迁移文件。然后我认为无论如何进行基于 env 的迁移是没有意义的......它们应该被创建为通用的,并且只是在每个 env 的基础上运行,所以最后,所有环境都使用相同的迁移脚本。
So I'm a bit stuck now on where to go to from here
所以我现在有点不知道从这里去哪里
回答by Jason Lewis
You need to specify the environment before the migrate command.
您需要在 migrate 命令之前指定环境。
artisan --env=local migrate
Running artisan help
shows you the format in which commands are to follow.
运行会artisan help
向您显示要遵循的命令的格式。
artisan help
Usage:
[options] command [arguments]
回答by duellsy
Resolved.
解决。
Solution was simply to edit the local/database.php (or production/database.php etc), making sure that the migrations path variable is pointing to the location that migrate:make
is creating the migration files, just change
解决方案只是编辑 local/database.php(或 production/database.php 等),确保迁移路径变量指向migrate:make
创建迁移文件的位置,只需更改
'application' => __DIR__.'/../database/migrations',
to
到
'application' => DIR.'/../../database/migrations',
回答by hungneox
I figured out a solution to run migrate
for different databases. Basically, the command artisan migrate --env=local
doesn't work. But we can define a new connection string in config\database.php
. For example:
我想出了一个migrate
针对不同数据库运行的解决方案。基本上,该命令artisan migrate --env=local
不起作用。但是我们可以在config\database.php
. 例如:
<?php
'mysql_testing' => [
'driver' => 'mysql',
'host' => env('DB_TESTING_HOST'),
'database' => env('DB_TESTING_DATABASE'),
'username' => env('DB_TESTING_USERNAME'),
'password' => env('DB_TESTING_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => 'prefix_',
],
And specify the --database
when we run artisan migrate
like this:
并指定--database
我们这样运行的时间artisan migrate
:
php artisan migrate --database=mysql_testing
Hope this helps :)
希望这可以帮助 :)
回答by William Cahill-Manley
If you modify a migration after running it, you first need to rollback the migration.
如果在运行后修改迁移,则首先需要回滚迁移。
php artisan migrate:rollback
Keep running that until the migration you've changed is rolled back. Alternatively, you can reset your entire schema with
继续运行,直到您更改的迁移回滚。或者,您可以使用以下命令重置整个架构
php artisan migrate:reset
But you will then need to call your migrations like normal to bring them up to date.
但是您随后需要像往常一样调用您的迁移以使其保持最新状态。
php artisan migrate
Finally you can reset and then migrate by calling
最后,您可以通过调用重置然后迁移
php artisan rebuild
Also note that it is generally bad practice to modify your migrations after they have been made, unless you literally just made it. Once it is deployed you should not modify it, and instead create a new migration file.
另请注意,在迁移完成后对其进行修改通常是不好的做法,除非您实际上只是进行了迁移。部署后,您不应修改它,而是创建一个新的迁移文件。
Hope this helps.
希望这可以帮助。
Edit: I somehow missed the Laravel 4 indicator. Most of these commands still work I believe, but you may need to adjust.
编辑:我不知何故错过了 Laravel 4 指标。我相信这些命令中的大多数仍然有效,但您可能需要调整。