Rails rake db:migrate 没有效果

发布于 2024-10-30 23:18:34 字数 189 浏览 1 评论 0原文

我今天制作了一个新的 Rails 3 应用程序,添加了一个简单的迁移,但由于某种原因,当我执行 rake db:migrate 时没有任何反应。它只是暂停几秒钟,然后返回到命令提示符,没有错误或任何内容。 Schema.rb 和数据库保持为空。

有什么想法可能会发生什么吗?我制作了很多应用程序,但从来没有遇到过这个问题。一切也都是完全标准的设置。

I made a new Rails 3 app today, added a simple migration, and for some reason, nothing happens when I do rake db:migrate. It simply pauses a few seconds, then returns to the command prompt, with no errors or anything. Schema.rb and the database stay empty.

Any ideas what could be going on? I've made many apps and never had this problem. Everything is a totally standard setup too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

度的依靠╰つ 2024-11-06 23:18:34

迁移无法运行的原因有多种,但最常见的是系统已经认为您定义的所有迁移都已运行。

每次迁移都会在 schema_migrations 表中创建一个条目,其中 version 列与标识符号相对应。如果您想强制重新运行迁移,通常可以将其退出并重试。例如,如果您有 20100421175455_create_things.rb,那么您可以使用以下方法重新运行它:

rake db:migrate:redo VERSION=20100421175455

常见情况是您的迁移一开始就无法运行,例如它生成了异常,并且但 Rails 仍然认为它是完整的。要强制重新运行迁移,请从 schema_migrations 表中删除相应记录,然后再次运行 rake db:migrate

将来避免此类问题的一种方法是使用自动退出过程定义迁移:

class CreateThings < ActiveRecord::Migration
  def self.up
    # ... (migration) ...

  rescue
    # If an exception occurs, back out of this migration, but ignore any
    # exceptions generated there. Do the best you can.
    self.down rescue nil

    # Re-raise this exception for diagnostic purposes.
    raise
  end
end

如果迁移中出现错误,您将看到控制台上列出的异常。由于迁移已自动回滚,您应该能够一次又一次地运行它,直到正确为止。

There's a few reasons why your migrations won't run, but the most common is that the system is already under the impression that all the migrations you've defined have already run.

Each migration creates an entry in the schema_migrations table with the version column corresponding to the identifier number. If you want to force a migration to re-run you can usually back it out and retry it. For example, if you had 20100421175455_create_things.rb then you would re-run it using:

rake db:migrate:redo VERSION=20100421175455

A common situation is that your migration has failed to run in the first place, that it generated an exception for instance, and yet Rails still considers it complete. To forcibly re-run a migration, delete the corresponding record from the schema_migrations table and run rake db:migrate again.

One way to avoid this kind of problem in the future is to define your migrations with an automatic back-out procedure:

class CreateThings < ActiveRecord::Migration
  def self.up
    # ... (migration) ...

  rescue
    # If an exception occurs, back out of this migration, but ignore any
    # exceptions generated there. Do the best you can.
    self.down rescue nil

    # Re-raise this exception for diagnostic purposes.
    raise
  end
end

If you have a mistake in your migration you will see the exception listed on the console. Since the migration has automatically been rolled back you should be able to run it again and again until you get it right.

成熟稳重的好男人 2024-11-06 23:18:34

我遇到了同样的问题。我做了一个简短的技巧,对我有帮助。我发布它只是为了以防万一有人想要一个简短而甜蜜的解决方案。我同意塔德曼的说法

“系统已经认为所有迁移
你定义的已经运行了”

我所做的是更改 /app_folder/db/migrate 文件夹中迁移文件的名称。我认为 ruby​​ 迁移文件名称中的数字部分是文件创建的时间。

每次您想重新运行迁移时,都可以在文件名中添加 1 ,然后删除/删除表(我使用 mysql 命令行工具进行删除)。然后运行rake db:migrate并且应该完成迁移。

I faced the same problem. I did a kind of a short hack that helped me. I am posting it just in case anyone wants a short and sweet solution. I agree with what Tadman is saying

"the system is already under the impression that all the migrations
you've defined have already run"

What I did was to change the name of the migrate file in the /app_folder/db/migrate folder. I think the numeric part in the name of the ruby migrate file is the time at which the file was created.

You can add say 1 , to the filename, every time you want to re-run the migrate. After changing the name drop/delete the table (I used mysql command line tool for deleting) and then run rake db:migrate and the migrations should be done.

因为看清所以看轻 2024-11-06 23:18:34

调用 spring stop 可能会解决您的问题。

Calling spring stop might solve your problems.

无风消散 2024-11-06 23:18:34

好吧,我发现是什么导致了我的问题。我正在使用 slim_scrooge gem 并将其注释掉使一切正常进行。虽然不知道为什么...

Well, I found out what was causing my problem. I'm using the slim_scrooge gem and commenting it out makes everything proceed normally. Don't know why though...

汹涌人海 2024-11-06 23:18:34

我今天在使用插件迁移 Redmine 插件时遇到了类似的问题,

rake redmine:plugins:migrate RAILS_ENV=production NAME=plugin_name

其中plugin_name实际上是插件 init.rb 中定义的插件名称。

我挣扎了4个小时,终于发现我的插件目录名称与插件名称不一样(注意redmine_前缀):

~/redmine/plugins/redmine_plugin_name

所以,请确保您的插件放置在以插件名称命名的文件夹中。我相信它也适用于其他 Rails 应用程序。

I faced similar problem today while migrating plugin for Redmine using

rake redmine:plugins:migrate RAILS_ENV=production NAME=plugin_name

where plugin_name is actually plugin name defined in init.rb of the plugin.

I struggled for 4 hours and finally figured out that my plugin directory name was not the same as the plugin name (note redmine_ prefix):

~/redmine/plugins/redmine_plugin_name

So, make sure your plugin is placed in folder named after plugin name. I believe it applies to other rails apps as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文