在迁移中创建未反映在架构中的序列 [英] Create Sequence In Migration Not Reflected In Schema

查看:35
本文介绍了在迁移中创建未反映在架构中的序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于在迁移中创建未反映在架构中的序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆