openfoodnetwork icon indicating copy to clipboard operation
openfoodnetwork copied to clipboard

Fix DatabaseCleaner when pg user is not superuser

Open sauloperez opened this issue 7 years ago • 2 comments

What/Why?

This is just a tech debt issue. It is not a bug in the app but something that slows us down.

When running specs without superuser privileges for the DB user, a ActiveRecord::InvalidForeignKey is raised, apparently due to DatabaseCleaner.

This is the actual output:

➜  openfoodnetwork (master) bundle exec rspec ./spec/features/consumer/shopping/checkout_spec.rb:419                                      ✱
Knapsack adapter is off!
(eval):1: warning: encountered \r in middle of line, treated as a mere space
Run options:
  include {:locations=>{"./spec/features/consumer/shopping/checkout_spec.rb"=>[419]}}
  exclude {:skip=>true, :future=>true, :to_figure_out=>true}

An error occurred in a `before(:suite)` hook.
Failure/Error: config.before(:suite)          { DatabaseCleaner.clean_with :deletion, {except: ['spree_countries', 'spree_states']} }

ActiveRecord::InvalidForeignKey:
  PG::Error: ERROR:  update or delete on table "spree_option_types" violates foreign key constraint "spree_option_values_option_type_id_fk" on table "spree_option_values"
  DETAIL:  Key (id)=(1) is still referenced from table "spree_option_values".
  : DELETE FROM "spree_option_types";
# ./config/initializers/active_record_postgresql_referential_integrity_patch.rb:19:in `disable_referential_integrity'
# ./spec/spec_helper.rb:83:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# PG::Error:
#   ERROR:  update or delete on table "spree_option_types" violates foreign key constraint "spree_option_values_option_type_id_fk" on table "spree_option_values"
#   DETAIL:  Key (id)=(1) is still referenced from table "spree_option_values".
#   ./config/initializers/active_record_postgresql_referential_integrity_patch.rb:19:in `disable_referential_integrity'
 0/1 |>                                                                                                                    |  ETA: ??:??:??
Finished in 0.1066 seconds (files took 11.24 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

When upgrading the user with superuser privileges:

postgres=# alter user ofn with superuser;
ALTER ROLE

The test passes.

What needs to be done?

We shouldn't need to have superuser privileges in order for DatabaseCleaner to run. So we have to investigate why it fails now.

sauloperez avatar Feb 09 '18 17:02 sauloperez

@openfoodfoundation/developers do we still need this issue?

RachL avatar Oct 06 '22 14:10 RachL

Yes, we still can't use the app without the superuser privilege. We actually get another error now:

ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR:  permission denied to create extension "pg_stat_statements"
HINT:  Must be superuser to create this extension.

mkllnk avatar Oct 10 '22 01:10 mkllnk