检查 Rails 中是否存在表
我有一个 rake 任务,除非存在表,否则该任务将无法工作。我在一个网站上与 20 多名工程师合作,因此我想确保他们在执行填充相应表的 rake 任务之前已经迁移了表。
AR有没有Table.exists之类的方法?我如何确保他们已成功迁移表?
I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table.
Does AR have a method such as Table.exists
? How can I make sure they have migrated the table successfully?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 Rails 5 中,API 对于表/视图变得明确,统称为数据源< /em>.
在 Rails 2、3 和4 API 与表有关。
获取迁移状态:
如果您需要更多用于迁移或元数据的 API,请参阅:
这是
schema_migrations
表的ActiveRecord::Base
类运行迁移时发生所有操作的地方
In Rails 5 the API became explicit regarding tables/views, collectively data sources.
In Rails 2, 3 & 4 the API is about tables.
Getting the status of migrations:
If you need more APIs for migrations or metadata see:
this is the
ActiveRecord::Base
class for theschema_migrations
tablewhere all the action happens when migrations are run
即使表不存在:
模型
Kitten
,预期表kittens
Rails 3:
Kitten.table_存在吗? #=>错误的
even if table is not exists:
model
Kitten
, expected tablekittens
rails 3:
Kitten.table_exists? #=> false
我在尝试通过迁移删除表时发现了这一点:
适用于 Rails 3.2
这种更简单的形式将在 Rails 5 中可用:
参考:https://github.com/rails/rails/pull/16366
这是 Rails 5 ActiveRecord 的 变更日志:
I found this out while I was trying to remove a table via a migration:
works for Rails 3.2
This simpler form will become available in Rails 5:
Reference: https://github.com/rails/rails/pull/16366
And here's the Rails 5 ActiveRecord's CHANGELOG:
Rails 5.1
或
Rails 5.1
or
执行此操作的正确方法是 Model.table_exists?
The proper way to do this is Model.table_exists?