检查 Rails 中是否存在表

发布于 2024-11-18 09:47:53 字数 138 浏览 5 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(5

错爱 2024-11-25 09:47:53

在 Rails 5 中,API 对于表/视图变得明确,统称为数据源< /em>.

# Tables and views
ActiveRecord::Base.connection.data_sources
ActiveRecord::Base.connection.data_source_exists? 'kittens'

# Tables
ActiveRecord::Base.connection.tables
ActiveRecord::Base.connection.table_exists? 'kittens'

# Views
ActiveRecord::Base.connection.views
ActiveRecord::Base.connection.view_exists? 'kittens'

在 Rails 2、3 和4 API 与有关。

# Listing of all tables and views
ActiveRecord::Base.connection.tables

# Checks for existence of kittens table/view (Kitten model)
ActiveRecord::Base.connection.table_exists? 'kittens'

获取迁移状态:

# Tells you all migrations run
ActiveRecord::Migrator.get_all_versions

# Tells you the current schema version
ActiveRecord::Migrator.current_version

如果您需要更多用于迁移或元数据的 API,请参阅:

In Rails 5 the API became explicit regarding tables/views, collectively data sources.

# Tables and views
ActiveRecord::Base.connection.data_sources
ActiveRecord::Base.connection.data_source_exists? 'kittens'

# Tables
ActiveRecord::Base.connection.tables
ActiveRecord::Base.connection.table_exists? 'kittens'

# Views
ActiveRecord::Base.connection.views
ActiveRecord::Base.connection.view_exists? 'kittens'

In Rails 2, 3 & 4 the API is about tables.

# Listing of all tables and views
ActiveRecord::Base.connection.tables

# Checks for existence of kittens table/view (Kitten model)
ActiveRecord::Base.connection.table_exists? 'kittens'

Getting the status of migrations:

# Tells you all migrations run
ActiveRecord::Migrator.get_all_versions

# Tells you the current schema version
ActiveRecord::Migrator.current_version

If you need more APIs for migrations or metadata see:

我要还你自由 2024-11-25 09:47:53

即使表不存在:

模型 Kitten,预期表 kittens
Rails 3:

Kitten.table_存在吗? #=>错误的

even if table is not exists:

model Kitten, expected table kittens
rails 3:

Kitten.table_exists? #=> false

就是爱搞怪 2024-11-25 09:47:53

我在尝试通过迁移删除表时发现了这一点:

drop_table :kittens if (table_exists? :kittens)
ActiveRecord::Migration.drop_table :kittens if (ActiveRecord::Base.connection.table_exists? :kittens)

适用于 Rails 3.2

这种更简单的形式将在 Rails 5 中可用:

drop_table :kittens, if_exists: true

参考:https://github.com/rails/rails/pull/16366

这是 Rails 5 ActiveRecord 的 变更日志

为 drop_table 引入 :if_exists 选项。

示例:

drop_table(:posts, if_exists: true)

这将执行:

如果存在则删除表帖子

如果表不存在,if_exists: false(默认值)会引发异常,而 if_exists: true 则不执行任何操作。

I found this out while I was trying to remove a table via a migration:

drop_table :kittens if (table_exists? :kittens)
ActiveRecord::Migration.drop_table :kittens if (ActiveRecord::Base.connection.table_exists? :kittens)

works for Rails 3.2

This simpler form will become available in Rails 5:

drop_table :kittens, if_exists: true

Reference: https://github.com/rails/rails/pull/16366

And here's the Rails 5 ActiveRecord's CHANGELOG:

Introduce the :if_exists option for drop_table.

Example:

drop_table(:posts, if_exists: true)

That would execute:

DROP TABLE IF EXISTS posts

If the table doesn't exist, if_exists: false (the default) raises an exception whereas if_exists: true does nothing.

恬淡成诗 2024-11-25 09:47:53

Rails 5.1

if ActiveRecord::Base.connection.data_source_exists? 'table_name'
   drop_table :table_name
end

drop_table :table_name, if_exists: true

Rails 5.1

if ActiveRecord::Base.connection.data_source_exists? 'table_name'
   drop_table :table_name
end

or

drop_table :table_name, if_exists: true
泪眸﹌ 2024-11-25 09:47:53

执行此操作的正确方法是 Model.table_exists?

class Dog < ApplicationRecord
  # something
end

do_something if Dog.table_exists?

The proper way to do this is Model.table_exists?

class Dog < ApplicationRecord
  # something
end

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