在迁移中创建未反映在架构中的序列

编程入门 行业动态 更新时间:2024-10-25 10:23:44
本文介绍了在迁移中创建未反映在架构中的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个应用程序需要一个序列存在于数据库中.我有一个执行以下操作的迁移:

I have an application that requires a sequence to be present in the database. I have a migration that does the following:

class CreateSequence < ActiveRecord::Migration def self.up execute "CREATE SEQUENCE sequence" end def self.down execute "DROP SEQUENCE sequence" end end

这不会修改schema.rb,从而破坏rake db:setup.如何强制架构包含序列?

This does not modify the schema.rb and thus breaks rake db:setup. How can I force the schema to include the sequence?

注意:运行rake db:migrate后序列存在.

Note: The sequence exists after running rake db:migrate.

推荐答案

Rails 迁移,因为它们的目标是表和字段的架构,而不是包括存储过程、函数、种子数据的完整数据库表示.

Rails Migrations because they aim toward a schema of tables and fields, instead of a complete database representation including stored procedures, functions, seed data.

当您运行 rake db:setup 时,这将创建数据库,加载架构,然后加载种子数据.

When you run rake db:setup, this will create the db, load the schema and then load the seed data.

一些供您考虑的解​​决方案:

A few solutions for you to consider:

选择 1:创建您自己的 rake 任务,独立于 Rails 向上/向下迁移执行这些迁移.Rails 迁移只是普通的类,您可以随意使用它们.例如:

Choice 1: create your own rake task that does these migrations independent of the Rails Migration up/down. Rails Migrations are just normal classes, and you can make use of them however you like. For example:

rake db:create_sequence

选择 2:在您像这样加载架构后运行您的特定迁移:

Choice 2: run your specific migration after you load the schema like this:

rake db:setup rake db:migrate:up VERSION=20080906120000

选择 3:将序列创建为种子数据,因为它本质上是提供数据(而不是更改架构).

Choice 3: create your sequence as seed data, because it's essentially providing data (rather than altering the schema).

db/seeds.rb

选择 4 和我的个人偏好:将迁移运行到一个已知的好点,包括您的序列,并保存该空白数据库.更改 rake db:setup 以克隆该空白数据库.这有点棘手,它牺牲了一些功能——让所有迁移都是可逆的,让迁移在多个数据库供应商之上工作,等等.根据我的经验,这些都是很好的权衡.例如:

Choice 4 and my personal preference: run the migrations up to a known good point, including your sequence, and save that blank database. Change rake db:setup to clone that blank database. This is a bit trickier and it sacrifices some capabilities - having all migrations be reversible, having migrations work on top of multiple database vendors, etc. In my experience these are fine tradeoffs. For example:

rake db:fresh #=> clones the blank database, which you store in version control

更多推荐

在迁移中创建未反映在架构中的序列

本文发布于:2023-11-10 06:33:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:序列   架构

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!