Skip to content

Drop user defined functions in `rake gitlab:db:drop_tables`

Stan Hu requested to merge sh-drop-tables-remove-functions into master

What does this MR do and why?

Previously the backup restore Rake task would fail if the schema were loaded and then dropped via rake gitlab:db:drop_tables because the task would not clean up the 13 user-defined functions in the schema. The CREATE FUNCTION in db/structure.sql would fail the restore because it would collide with an existing function.

Since DROP DATABASE may need superuser permissions, the backup restore task expects that rake gitlab:db:drop_tables brings the database into a clean state. To ensure that's the case, we now search for all user defined functions and drop them. Note that while most of the functions are used with triggers, next_traversal_ids_sibling is a noteable exception.

Relates to #356032 (closed)

How to set up and validate locally

  1. Run RAILS_ENV=test bundle exec rake db:reset
  2. gdk psql -d gitlabhq_test
  3. See the list of functions: \df+
  4. See the list of plpgsql functions:
SELECT p.proname FROM pg_catalog.pg_proc p
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
     LEFT JOIN pg_catalog.pg_language l ON l.oid = p.prolang
WHERE pg_catalog.pg_function_is_visible(p.oid)
      AND n.nspname <> 'pg_catalog'
      AND n.nspname <> 'information_schema'
      AND l.lanname = 'plpgsql';
  1. Run RAILS_ENV=test bundle exec rake gitlab:db:drop_tables.
  2. Repeat steps 3 and 4. You should all the plpgsql functions wiped.
  3. Repeat step 1 to bring your database back to normal.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Stan Hu

Merge request reports