耙测试不能照搬发展的Postgres数据库与序列 [英] rake test not copying development postgres db with sequences

查看:274
本文介绍了耙测试不能照搬发展的Postgres数据库与序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用递增的基础上validates_uniqueness_of默认Ruby的方式字段,而不是一个序列,制定关于PostgreSQL的Rails应用程序。

I am trying to develop a rails application on postgresql using a sequence to increment a field instead of a default ruby approach based on validates_uniqueness_of.

此已证明对于许多原因有挑战性: 1.这是一个现有的表的迁移,而不是一个新的表或列 2.使用参数:默认=>NEXTVAL('序列')没有工作,因为它试图将它设置在括号 3.最终拿到了迁移2步工作:

This has proved challenging for a number of reasons: 1. This is a migration of an existing table, not a new table or column 2. Using parameter :default => "nextval('seq')" didn't work because it tries to set it in parenthesis 3. Eventually got migration working in 2 steps:

change_column :work_commencement_orders, :wco_number_suffix, :integer, :null => false#, :options => "set default nextval('wco_number_suffix_seq')"

execute %{
  ALTER TABLE work_commencement_orders ALTER COLUMN wco_number_suffix SET DEFAULT nextval('wco_number_suffix_seq');
}

现在这似乎已经在开发数据库做了正确的事情,该模式是这样的:

Now this would appear to have done the correct thing in the development database and the schema looks like:

wco_number_suffix      | integer                     | not null default nextval('wco_number_suffix_seq'::regclass)

不过,测试失败与

However, the tests are failing with

PGError: ERROR:  null value in column "wco_number_suffix" violates not-null constraint
: INSERT INTO "work_commencement_orders" ("expense_account_id", "created_at", 
"process_id", "vo2_issued_on", "wco_template", "updated_at", "notes", "process_type", 
"vo_number", "vo_issued_on", "vo2_number", "wco_type_id", "created_by", 
"contractor_id", "old_wco_type", "master_wco_number", "deadline", "updated_by", 
"detail", "elective_id", "authorization_batch_id", "delivery_lat", "delivery_long", 
"operational", "state", "issued_on", "delivery_detail") VALUES(226, '2010-05-31 
07:02:16.764215', 728, NULL, E'Default', '2010-05-31 07:02:16.764215', NULL, 
E'Procurement::Process', NULL, NULL, NULL, 226, NULL, 276, NULL, E'MWCO-213', 
'2010-06-14 07:02:16.756952', NULL, E'Name 4597', 220, NULL, NULL, NULL, 'f', 
E'pending', NULL, E'728 Test Road; Test Town; 1234; Test Land') RETURNING "id"

的说明可以在您检查测试数据库的模式中找到:

The explanation can be found when you inspect the schema of the test database:

wco_number_suffix      | integer                     | not null

到底发生了什么为默认?

So what happened to the default?

我尝试添加

task:
    template: smmt_ops_development

要具有颁发的效果database.yml文件

to the database.yml file which has the effect of issuing

create database smmt_ops_test template = "smmt_ops_development" encoding = 'utf8'

我已经验证了,如果我发出这则它实际上是复制默认NEXTVAL。所以很明显Rails是做一些事情之后,再次晚饭preSS吧。

I have verified that if I issue this then it does in fact copy the default nextval. So clearly rails is doing something after that to suppress it again.

任何建议,如何解决这个问题?

Any suggestions as to how to fix this?

谢谢 罗伯特·

推荐答案

我不解决这个问题本身,而是制定了以下变通方法work_commencement_orders模式,使测试运行。

I didn't solve the problem as such but developed the following workaround for the work_commencement_orders model to enable the tests to run.

###########################################################################
###########################################################################
# NOTE this is purely for test, it should have no effect on development or production
# as the field will be marked readonly and prevented from being written to the db so that
# it picks up a default value from its sequence.
public

def before_validation
  if wco_number_suffix.blank?
    maximum = WorkCommencementOrder::Entity.maximum(:wco_number_suffix)
    maximum ||= 0
    self.wco_number_suffix = maximum + 1
  end
end

private

def test?
  @is_test ||= (ENV['RAILS_ENV'] == 'test')
end

# Override default behaviour so that when not in test environment it does not try
# to write readonly attributes during create but instead allows them to be initialised
# by default value.
def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = test?, attribute_names = @attributes.keys)
  super(include_primary_key, include_readonly_attributes, attribute_names)
end

###########################################################################
###########################################################################

这篇关于耙测试不能照搬发展的Postgres数据库与序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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