运行一个特定的 Laravel 迁移(单个文件)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19102197/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:39:06  来源:igfitidea点击:

Running one specific Laravel migration (single file)

migrationlaravellaravel-4

提问by Mamadou

I don't want to run All Outstanding Migrations on laravel 4. I have 5 migrations. Now I just want to run one migration. instead of doing : php artisan migrate I would like to run one specific migration like : php artisan migrate MY_MIGRATION_TO_RUN

我不想在 laravel 4 上运行所有未完成的迁移。我有 5 次迁移。现在我只想运行一次迁移。而不是做: php artisan migrate 我想运行一个特定的迁移,如: php artisan migrate MY_MIGRATION_TO_RUN

采纳答案by Antonio Carlos Ribeiro

Looks like you're doing it wrong.

看起来你做错了。

Migrations were made to be executed by Laravel one by one, in the exact order they were created, so it can keep track of execution and order of execution. That way Laravel will be able to SAFELY rollback a batch of migrations, without risking breaking your database.

迁移由 Laravel 一一执行,按照它们创建的确切顺序,因此它可以跟踪执行和执行顺序。这样 Laravel 将能够安全地回滚一批迁移,而不会冒破坏数据库的风险。

Giving the user the power to execute them manually, make it impossible to know (for sure) how to rollback changes in your database.

赋予用户手动执行它们的权力,使其无法(肯定)知道如何回滚数据库中的更改。

If your really need to execute something in your database, you better create a DDL script and manually execute it on your webserver.

如果您确实需要在数据库中执行某些操作,您最好创建一个 DDL 脚本并在您的网络服务器上手动执行它。

Or just create a new migration and execute it using artisan.

或者只是创建一个新的迁移并使用 artisan 执行它。

EDIT:

编辑:

If you need to run it first, you need to create it first.

如果您需要先运行它,则需要先创建它。

If you just need to reorder them, rename the file to be the first. Migrations are created with a timestemp:

如果您只需要对它们重新排序,请将文件重命名为第一个。迁移是使用时间模板创建的:

2013_01_20_221554_table

To create a new migration before this one you can name it

要在此之前创建新迁移,您可以命名它

2013_01_19_221554_myFirstMigration

回答by Pradyumna Challa

Just move the already run migrations out of the app/config/database/migrations/ folder . Then run the command php artisan migrate. Worked like a charm for me .

只需将已经运行的迁移移出 app/config/database/migrations/ 文件夹。然后运行命令php artisan migrate。对我来说就像一种魅力。

回答by fico7489

You can put migrations in more folders and run something like:

您可以将迁移放在更多文件夹中并运行以下内容:

php artisan migrate --path=/app/database/migrations/my_migrations

回答by Erik Aybar

A nice little snippet to ease any fears when running Laravel 4 migrations php artisan migrate --pretend. This will only output the SQL that would have been run if you ran the actual migration.

一个不错的小片段,可以在运行 Laravel 4 迁移时缓解任何恐惧php artisan migrate --pretend。如果您运行实际迁移,这将只输出本来会运行的 SQL。

It sounds like your initial 4 migrations have already been run. I would guess that when you php artisan migrateit will only run the new, recent migration.

听起来您最初的 4 次迁移已经运行。我猜你php artisan migrate什么时候它只会运行新的、最近的迁移。

Word of advice: makes sure all of your up() and down() work how you expect them to. I like to run up(), down(), up() when I run my migrations, just to test them. It would be awful for you to get 5-6 migrations in and realize you can't roll them back without hassle, because you didn't match the down() with the up() 100% percent.

忠告:确保您的所有 up() 和 down() 都按照您的预期工作。我喜欢在运行迁移时运行 up()、down()、up(),只是为了测试它们。如果您进行 5-6 次迁移并意识到您无法轻松回滚它们,那将是很糟糕的,因为您没有将 down() 与 up() 100% 匹配。

Just my two cents! Hope the --pretendhelps.

只有我的两分钱!希望有所--pretend帮助。

回答by elfif

The only way to re run a migrattion is a dirty one. You need to open your database and delete the line in the migrations table that represents your migration.

重新运行迁移的唯一方法是脏迁移。您需要打开数据库并删除迁移表中代表迁移的行。

Then run php artisan migrate again.

然后再次运行 php artisan migrate 。

回答by jidesakin

You can create a separate directory for your migrations from your terminal as follows:

您可以从终端为您的迁移创建一个单独的目录,如下所示:

mkdir /database/migrations/my_migrations

And then move the specific migration you want to run to that directory and run this command:

然后将要运行的特定迁移移动到该目录并运行以下命令:

php artisan migrate --path=/database/migrations/my_migrations

Hope this helps!

希望这可以帮助!

回答by Colin

I gave this answer on another post, but you can do this: run artisan migrateto run all the migrations, then the following SQL commands to update the migrations table, making it look like the migrations were run one at a time:

我在另一篇文章中给出了这个答案,但您可以这样做:运行artisan migrate以运行所有迁移,然后使用以下 SQL 命令更新迁移表,使其看起来像是一次运行一个迁移:

SET @a = 0;  
UPDATE migrations SET batch = @a:=@a+1;

That will change the batch column to 1, 2, 3, 4 .. etc. Add a WHERE batch>=...condition on there (and update the initial value of @a) if you only want to affect certain migrations.

这会将批处理列更改为 1、2、3、4 等。如果您只想影响某些迁移,请WHERE batch>=...在那里添加一个条件(并更新 的初始值@a)。

After this, you can artisan migrate:rollbackas much as is required, and it'll step through the migrations one at a time.

在此之后,您可以artisan migrate:rollback根据需要进行尽可能多的迁移,并且一次一个地逐步完成迁移。

回答by Ahmed Mabrouk

There is one easy way I know to do this can only be available for you on just local host

我知道有一种简单的方法可以做到这一点只能在本地主机上为您提供

  1. Modifyyour migration file as needed
  2. open your phpMyAdmin or whatever you use to see your database table
  3. find the desired table and drop it
  4. find migrations table and open it
  5. in this table under migration field find your desired table name and delete its row
  6. finally run the command php artisan migratefrom your command line or terminal. this will only migrate that tables which not exists in the migrations table in database.
  1. 根据需要修改您的迁移文件
  2. 打开你的 phpMyAdmin 或任何你用来查看你的数据库表的东西
  3. 找到所需的表并将其删除
  4. 找到迁移表并打开它
  5. 在迁移字段下的此表中找到所需的表名并删除其行
  6. 最后php artisan migrate从命令行或终端运行命令。这只会迁移数据库中迁移表中不存在的表。

This way is completely safe and will not make any errors or problems while it looks like un-professional way but it still works perfectly.

这种方式是完全安全的,不会出现任何错误或问题,虽然它看起来像不专业的方式,但它仍然可以完美运行。

good luck

祝你好运

回答by Sayed Sajad Hosseini

You can use below solution:

您可以使用以下解决方案:

  1. create your migration.
  2. check your migration status like: php artisan migrate:status.
  3. copy the full name of new migration and do this like: php artisan migrate:rollback --path:2018_07_13_070910_table_tests.
  4. and then do this php artisan migrate.
  1. 创建您的迁移。
  2. 检查您的迁移状态,例如:php artisan migrate:status
  3. 复制新移民的全名,并做到这一点,如:php artisan migrate:rollback --path:2018_07_13_070910_table_tests
  4. 然后这样做php artisan migrate

finally, you migrate specific table. Goodluck.

最后,您迁移特定表。祝你好运。

回答by Jammer

If you want to run your latest migration file you would do the following:

如果要运行最新的迁移文件,请执行以下操作:

php artisan migrate

You can also revert back to before you added the migration with:

您还可以在添加迁移之前恢复为:

php artisan migrate: rollback