Skip to content

Update routes for lost-and-found group and projects - Ghost namespace conflict with existing ghost path

Problem Description

A regression has been reported on at least one environment when the db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb post-migration runs:

  * bash[migrate gitlab-rails database] action run
    [execute] rake aborted!
              StandardError: An error has occurred, this and all later migrations canceled:

              PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_routes_on_path"
              DETAIL:  Key (path)=(ghost) already exists.
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:78:in `ensure_route!'
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:163:in `up'
              /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:49:in `block (3 levels) in <top (required)>'
              /opt/gitlab/embedded/bin/bundle:23:in `load'
              /opt/gitlab/embedded/bin/bundle:23:in `<main>'

              Caused by:
              ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_routes_on_path"
              DETAIL:  Key (path)=(ghost) already exists.
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:78:in `ensure_route!'
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:163:in `up'
              /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:49:in `block (3 levels) in <top (required)>'
              /opt/gitlab/embedded/bin/bundle:23:in `load'
              /opt/gitlab/embedded/bin/bundle:23:in `<main>'

              Caused by:
              PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_routes_on_path"
              DETAIL:  Key (path)=(ghost) already exists.
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:78:in `ensure_route!'
              /opt/gitlab/embedded/service/gitlab-rails/db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb:163:in `up'
              /opt/gitlab/embedded/service/gitlab-rails/lib/tasks/gitlab/db.rake:49:in `block (3 levels) in <top (required)>'
              /opt/gitlab/embedded/bin/bundle:23:in `load'
              /opt/gitlab/embedded/bin/bundle:23:in `<main>'
              Tasks: TOP => db:migrate
              (See full trace by running task with --trace)
              == 20200602143020 UpdateRoutesForLostAndFoundGroupAndOrphanedProjects: migrating

(For reference, this migration was introduced with !34285 (merged))

Root Cause

We have identified that this issue can occur in instances with an existing route with a path = 'ghost'

Way to reproduce in a local dev environment:

$ bundle exec rake dev:setup RAILS_ENV=development

$ gdk psql

 DELETE from schema_migrations where version='20200511080113';
 DELETE from schema_migrations where version='20200511083541';
 DELETE from schema_migrations where version='20200511220023';
 DELETE from schema_migrations where version='20200602143020';
 ALTER TABLE projects DROP CONSTRAINT fk_projects_namespace_id;

 UPDATE projects 
 SET namespace_id = 999999, 
 archived = TRUE, visibility_level = 0,
 name = name,
 path = path
 where path like '%gitlab-test%' and id = 1;
 UPDATE routes SET path = 'ghost' WHERE path = 'h5bp';

 \q

$ bundle exec rake db:migrate

== 20200602143020 UpdateRoutesForLostAndFoundGroupAndOrphanedProjects: migrating
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_routes_on_path"
DETAIL:  Key (path)=(ghost) already exists.

Solution

Update the db/post_migrate/20200602143020_update_routes_for_lost_and_found_group_and_orphaned_projects.rb post-migration to ensure that unique path is generated for the Ghost namespace.

If a new path is chosen (i.e. the existing path was conflicting), also update the Ghost user's username.