Ruby-on-rails 架构迁移表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19100303/
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
Schema Migrations Table
提问by tommyd456
In my Rails 4 app I would like to collapse my migration files into one large file (similar to schema.rb) as it's time to do some housekeeping but I'm not sure on how to access the table in the database that stores migration data so that when I run a migration I don't receive any errors/conflicts.
在我的 Rails 4 应用程序中,我想将我的迁移文件折叠成一个大文件(类似于 schema.rb),因为是时候做一些内务了,但我不确定如何访问存储迁移数据的数据库中的表这样当我运行迁移时,我不会收到任何错误/冲突。
QuestionHow can I access and delete the data in the table that stores migration data?
问题如何访问和删除存储迁移数据的表中的数据?
回答by David Lowenfels
for fun, you can also manipulate these in the console by making a model class for them...
为了好玩,您还可以通过为它们创建模型类在控制台中操作它们...
class SchemaMigration < ActiveRecord::Base; self.primary_key = :version; end
then you can do SchemaMigration.all, SchemaMigration.last.delete, etc.
然后你可以做 SchemaMigration.all、SchemaMigration.last.delete 等。
Really just a substitute for using SQL, and it is very rare that you would need to mess around at this low level… generally a bad idea but cool to see how to do it :)
真的只是使用 SQL 的替代品,而且您很少需要在这种低级别上搞砸……通常是个坏主意,但很酷,看看如何去做:)
回答by Peter Piper
Another solution could be to access it through:
另一种解决方案可能是通过以下方式访问它:
ActiveRecord::SchemaMigration
The answer given by David didn't work in my context.
大卫给出的答案在我的上下文中不起作用。
回答by steakchaser
The schema_migrationstable holds the revision numbers; with the last record being the most recently executed migration. You can just manipulate these records manually.
该schema_migrations表包含修订号;最后一条记录是最近执行的迁移。您可以手动操作这些记录。
回答by Agustin
Not sure why you want to do this but here you go:
不知道你为什么要这样做,但你去吧:
ActiveRecord::Migrator.get_all_versions
ActiveRecord::Migrator.get_all_versions
回答by localhostdotdev
to get the last version:
获取最新版本:
ActiveRecord::SchemaMigration.last.version
or all versions:
或所有版本:
ActiveRecord::SchemaMigration.all.map(&:version)

