Rails在没有迁移的情况下创建表

时间:2021-05-02 23:24:58

I have a rails app that has a particular table where the data and even the structure is dynamically generated outside of rails and ruby. This is by design, it is a special table where the structure is self-contained from the rest of active record and relationships. The models that work on it are also atomic. Again all by design, and purposeful. I don't not want a specific structure for this table, meaning that the column names and number of columns can change each time the table is initialized. If there are changes to the table structure I can manage the changes to my model class.

我有一个rails应用程序,它有一个特定的表,其中数据甚至结构是在rails和ruby之外动态生成的。这是一个设计,它是一个特殊的表,其中结构是从其他活动记录和关系中自包含的。对其起作用的模型也是原子的。所有这一切都是通过设计和有目的的。我不想要这个表的特定结构,这意味着每次初始化表时列名和列数都可以更改。如果表结构有变化,我可以管理对模型类的更改。

My issue is that the rails migration process seems to get in the way, and I don't want to have to keep stepping back and forth between migration and rollback, just to get the state of this single table reset.

我的问题是rails迁移过程似乎阻碍了我,并且我不想在迁移和回滚之间来回徘徊,只是为了让这个单个表的状态重置。

The behavior I am looking for is literally every time I "generate" the data for this table I want to drop what table might already exist (in all environments: production, dev and test).

我正在寻找的行为实际上是每次我“生成”该表的数据时我想删除可能已经存在的表(在所有环境中:生产,开发和测试)。

Is there a clear way to bypass the migration process? Or else create a special migration that is independent of the sequence of other migrations in the app?

是否有明确的方法绕过迁移过程?或者创建一个独立于应用程序中其他迁移序列的特殊迁移?

The entire database is not disposable, but this one table is.

整个数据库不是一次性的,但这一个表是。

Thoughts on how I might achieve this behavior?

关于我如何实现这种行为的想法?

Rails 3, PostgreSQL database, git version control, heroku hosting

Rails 3,PostgreSQL数据库,git版本控制,heroku主机

2 个解决方案

#1


2  

I think the simple answer is "don't use migrations" -- they are designed to help you relatively gracefully extend an otherwise static database schema. A migration does many things beyond generating/executing the data definition language (DDL) of your database -- it knows how to go forward and backward, knows by a linkage between source code (schema.rb) and data (in the schema_migrations table) how to determine which migrations need to be run, and so on. All you need is the part that executes the DDL (which is, after all, just a kind of SQL).

我认为简单的答案是“不要使用迁移” - 它们旨在帮助您相对优雅地扩展静态数据库模式。迁移除了生成/执行数据库的数据定义语言(DDL)之外还做很多事情 - 它知道如何前进和后退,通过源代码(schema.rb)和数据之间的链接(在schema_migrations表中)知道如何确定需要运行哪些迁移,等等。您所需要的只是执行DDL的部分(毕竟,这只是一种SQL)。

And at least some of that part is here in the TableDefinition API. All of the infrastructure you might need seems to be present.

至少部分内容在TableDefinition API中。您可能需要的所有基础设施似乎都存在。

#2


2  

You can set this up as a rake task (it sounds like what you are using it as) that contains the SQL/rails commands used to construct the table, similar to what is done with rake db:seed

您可以将其设置为rake任务(听起来就像您使用的那样),其中包含用于构造表的SQL / rails命令,类似于rake db:seed所做的操作

#1


2  

I think the simple answer is "don't use migrations" -- they are designed to help you relatively gracefully extend an otherwise static database schema. A migration does many things beyond generating/executing the data definition language (DDL) of your database -- it knows how to go forward and backward, knows by a linkage between source code (schema.rb) and data (in the schema_migrations table) how to determine which migrations need to be run, and so on. All you need is the part that executes the DDL (which is, after all, just a kind of SQL).

我认为简单的答案是“不要使用迁移” - 它们旨在帮助您相对优雅地扩展静态数据库模式。迁移除了生成/执行数据库的数据定义语言(DDL)之外还做很多事情 - 它知道如何前进和后退,通过源代码(schema.rb)和数据之间的链接(在schema_migrations表中)知道如何确定需要运行哪些迁移,等等。您所需要的只是执行DDL的部分(毕竟,这只是一种SQL)。

And at least some of that part is here in the TableDefinition API. All of the infrastructure you might need seems to be present.

至少部分内容在TableDefinition API中。您可能需要的所有基础设施似乎都存在。

#2


2  

You can set this up as a rake task (it sounds like what you are using it as) that contains the SQL/rails commands used to construct the table, similar to what is done with rake db:seed

您可以将其设置为rake任务(听起来就像您使用的那样),其中包含用于构造表的SQL / rails命令,类似于rake db:seed所做的操作