Ruby-on-rails 只进行一次迁移
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1331367/
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
Rake just one migration
提问by Anon
I'm trying to run just one migration out of a whole bunch in my rails app. How can I do this? I don't want to run any of the migrations before or after it. Thanks.
我试图在我的 rails 应用程序中只运行一个迁移。我怎样才能做到这一点?我不想在它之前或之后运行任何迁移。谢谢。
回答by Ryan Bigg
rake db:migrate:redo VERSION=xxxxxxx, but that will run the downand then the upstep. You could do this in conjunction with commenting out the down step temporarily.
rake db:migrate:redo VERSION=xxxxxxx,但这将运行down,然后是up步骤。您可以结合暂时注释掉向下步骤来执行此操作。
回答by Shadwell
rake db:migrate:up VERSION=1234567890
similarly rake db:migrate:downto take a specific migration down. You can get a list of available rake tasks with rake -T.
类似rake db:migrate:down地采取特定的迁移。您可以使用rake -T.
回答by Shadwell
I've had to run a single migration that changed and needed to be re-run independently of all other migrations. Fire up the console and do this:
我不得不运行一个已更改并且需要独立于所有其他迁移重新运行的迁移。启动控制台并执行以下操作:
>> require 'db/migrate/your_migrations.rb'
=> ["YourMigrations"]
>> YourMigrations.up
=> etc... as the migration runs
>> YourMigration.down
More usefully this could be put into a rake task etc.
更有用的是,这可以放入 rake 任务等中。
回答by reshma
rake db:migrate:up VERSION=version_no
rake db:migrate:up VERSION=version_no
Will migrate( add) specific migration script
将迁移(添加)特定的迁移脚本
rake db:migrate:down VERSION=version_no
rake db:migrate:down VERSION=version_no
Will delete specific migration script
将删除特定的迁移脚本
回答by JP Silvashy
rake db:migrate VERSION=20098252345
give that a try.
试一试。
回答by hexinpeter
rake db:migrate:redo version='xxxx'
Remember to put the quotation mark around xxxx, xxxx is the timestamp (or Migration ID) for your migration.
请记住在 xxxx 周围加上引号,xxxx 是您迁移的时间戳(或迁移 ID)。
You may check the timestamps (Migration ID) for the previous migrations you've done by using
您可以使用以下命令检查您之前完成的迁移的时间戳(迁移 ID)
rake db:migrate:status
回答by bhfailor
Expanding on the answer by korch above, requiredid not work for me, but loaddid. To be concrete, for the migration file:
扩展上面 korch 的答案,require对我不起作用,但load确实如此。具体来说,对于迁移文件:
class ChangeMinQuantityToRaces < ActiveRecord::Migration
def change
change_column :races, :min_quantity, :integer, :default => 0
end
end
in the console typing
在控制台输入
> load 'db/migrate/30130925110821_change_min_quantity_to_races.rb'
> ChangeMinQuantityToRaces.new.change
worked for me.
对我来说有效。
> Race.new.min_quantity # => 0
This was for ruby 1.9.3p484 (2013-11-22 revision 43786) [x86_64-linux] and Rails 3.2.13.
这是针对 ruby 1.9.3p484(2013-11-22 修订版 43786)[x86_64-linux] 和 Rails 3.2.13。
回答by Ken Simon
Adding my 2¢ to this because I ran into this same issue:
将我的 2¢ 添加到此,因为我遇到了同样的问题:
If you absolutely want to run a migration over again without creating a new one, you can do the following:
如果您绝对想重新运行迁移而不创建新迁移,您可以执行以下操作:
rails dbconsole -p
devdb=# delete from public.schema_migrations where version = '20150105181157';
rails dbconsole -p
devdb=# delete from public.schema_migrations where version = '20150105181157';
And rails will "forget" that it ran the migration for 20150105181157. Now when you run db:migrate it will run it again.
而 rails 会“忘记”它运行了 20150105181157 的迁移。现在当你运行 db:migrate 时,它会再次运行它。
This is almost always a bad idea though. The one instance where it could make sense is if you have a development branch and you haven't fleshed out your migration yet and want to add some things to it in development. But even then it's better to make your migration 2-way so you can properly rollback and retry repeatedly.
不过,这几乎总是一个坏主意。它可能有意义的一个例子是,如果您有一个开发分支,并且您还没有充实您的迁移并希望在开发中向它添加一些东西。但即便如此,最好使您的迁移采用 2 向方式,以便您可以正确回滚并反复重试。
回答by Terry G Lorber
There's got to be a way to run the migration class via the console. I can't seem to get the migrations code to be recognizable.
必须有一种方法可以通过控制台运行迁移类。我似乎无法识别迁移代码。
However, as the comments indicate, it's preferred to run the migrations in order. Use:
但是,正如注释所示,最好按顺序运行迁移。用:
rake db:migrate VERSION=##########
Copy and paste your code in the migration to script/console?
在迁移到脚本/控制台时复制并粘贴您的代码?
回答by aceofspades
I have a utility method that makes this very easy in development. I find that it helps me avoid creating too many migrations--normally I modify migrations until they have been deployed.
我有一个实用方法,可以让这在开发中变得非常容易。我发现它可以帮助我避免创建过多的迁移——通常我会修改迁移,直到它们被部署。
http://fullware.net/index.php/2011/05/26/easily-load-rails-migrations-for-console-execution/
http://fullware.net/index.php/2011/05/26/easily-load-rails-migrations-for-console-execution/

