Using Rails 6.0.3.4, Minitest, PG, Ruby 2.7.2
I created a migration in my Rails project which introduces a CONSTRAINT on my Service model.
ALTER TABLE services
ADD CONSTRAINT version CHECK (
CASE WHEN version = 1 AND id != original_service_id then FALSE
WHEN version != 1 AND id = original_service_id then FALSE
WHEN version != 1 AND deprecated_since IS NULL then FALSE
WHEN version =1 AND deprecated_since IS NOT NULL then FALSE
END
);
After executing rails db:migrate successfully, I use rails console to test the CONSTRAINT "manually", I get the expected behaviour. An exception is raised.
Console input:
Forge::Service.insert(specialty_id: 1, name: "a", description: "a",
created_at: Time.now, updated_at: Time.now, version: 2)
Console output:
ActiveRecord::StatementInvalid: PG::CheckViolation: ERROR: new row for relation "services" violates check constraint "version" DETAIL: Failing row contains (71, a, a, f, 2, null, 1, null, 2021-02-13 18:46:43.635609, 2021-02-13 18:46:43.635614).
I then created a test to confirm the behaviour but I am not able to reproduce the same result.
test "cannot insert to db with invalid version" do
assert_raise(Exception) do
Forge::Service.insert(specialty_id: 1, name: "a", description: "a",
created_at: Time.now, updated_at: Time.now, version: 2)
end
end
The test produces the following result.
Failure:
Forge::ServiceTest#test_cannot_insert_to_db_with_invalid_version [/home/francis/code/s21385/bumble-z/test/models/forge/service_test.rb:129]:
Exception expected but nothing was raised.
rails test test/models/forge/service_test.rb:128
I used Service.count before and after my test and a new record is effectively created so clearly my PG CONSTRAINT is not applied while testing.
I tried dropping the RAILS_ENV=test db and loading it but I am getting the same issue.
Aucun commentaire:
Enregistrer un commentaire