Rake test not copying postgres db development with sequences

I am trying to develop a rails application on postgresql using a sequence to increase the field instead of the standard ruby ​​approach based on validates_uniqueness_of.

This turned out to be difficult for several reasons: 1. This is a transfer of an existing table, not a new table or column 2. Using the parameter: default => "nextval (" seq ")" does not work, because it tries to set it in brackets 3. In As a result, migration appeared, working in 2 stages:

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 seems to have done the right thing in the development database, and the schema looks like this:

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

However, tests do not work 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" 

An explanation can be found when checking the test database schema:

 wco_number_suffix | integer | not null 

So what happened with the default?

I tried adding

 task: template: smmt_ops_development 

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

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

I checked that if I do this, it will actually copy the nextval value by default. Therefore, it is clear that the rails do something after this in order to crush it again.

Any suggestions on how to fix this?

Thanks Robert

0
source share
2 answers

I did not solve the problem as such, but I developed the following workaround for the work_commencement_orders model, which allows running tests.

 ########################################################################### ########################################################################### # 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 ########################################################################### ########################################################################### 
+1
source

I came across the same problem. For future visitors

Basically, this is in your config / environment / development.rb and run your tests.

  config.active_record.schema_format = :sql 

For more information - https://rails.lighthouseapp.com/projects/8994/tickets/5849-rails3-rake-task-dbtestprepare-no-longer-handles-stored-procedures

0
source

Source: https://habr.com/ru/post/907665/


All Articles