BUG: Database migration for FinalizeUpdateDelayedProjectRemovalToNull

Running the database migrations to 16.10 fails with:

      main: == 20240209155253 RemoveColumnsFromApplicationSettings: migrating =============
      main: -- remove_column(:application_settings, :delayed_project_removal, {:if_exists=>true})
      main:    -> 0.4479s
      main: -- remove_column(:application_settings, :lock_delayed_project_removal, {:if_exists=>true})
      main:    -> 0.4362s
      main: -- remove_column(:application_settings, :delayed_group_deletion, {:if_exists=>true})
      main:    -> 0.4617s
      main: == 20240209155253 RemoveColumnsFromApplicationSettings: migrated (1.3528s) ====
      
      main: == 20240209161048 RemoveColumnsFromNamespaceSettings: migrating ===============
      main: -- remove_column(:namespace_settings, :delayed_project_removal, {:if_exists=>true})
      main:    -> 0.0073s
      main: -- remove_column(:namespace_settings, :lock_delayed_project_removal, {:if_exists=>true})
      main:    -> 0.0073s
      main: == 20240209161048 RemoveColumnsFromNamespaceSettings: migrated (0.0222s) ======
      
      main: == 20240209183815 FinalizeUpdateDelayedProjectRemovalToNull: migrating ========
      main: -- transaction_open?(nil)
      main:    -> 0.0000s
      main: -- transaction_open?(nil)
      main:    -> 0.0000s
      main: == [advisory_lock_connection] object_id: 49920, pg_backend_pid: 598551
      STDERR: 


      PG::UndefinedColumn: ERROR:  column namespace_settings.delayed_project_removal does not exist
      LINE 13:       AND namespace_settings.delayed_project_removal = FALSE...
                         ^

The culprit appears to be this query in gitlab/background_migration/update_delayed_project_removal_to_null_for_user_namespaces.rb:

            UPDATE namespace_settings
            SET delayed_project_removal = NULL
            WHERE
              namespace_settings.namespace_id IN (
                SELECT
                  namespace_settings.namespace_id
                FROM
                  namespace_settings
                  INNER JOIN namespaces ON namespaces.id = namespace_settings.namespace_id
                WHERE
                  namespaces.id IN (#{relation.select(:namespace_id).to_sql})
                  AND namespaces.type = 'User'
                  AND namespace_settings.delayed_project_removal = FALSE)
        SQL

It looks like delayed_project_removal was already deleted by the prior two migrations and thus couldn't be found here?

As a workaround (not sure if it's correct?), I ran this command:

ALTER TABLE namespace_settings
ADD COLUMN delayed_project_removal BOOLEAN DEFAULT NULL;

Finished the database migrations, and then dropped the column again:

ALTER TABLE namespace_settings
DROP COLUMN delayed_project_removal;
Edited by dxdc