• Thong Kuah's avatar
    Fix PG::ObjectInUse error when dropping databases · 200f0d90
    Thong Kuah authored
    This requires a few things to be true:
    
    1. Database connections are opened during routes. This is true for the
       GitLab application. Note this is not allowed anymore in Rails 7.
    2. Multiple databases are configured. With a single database, Rails has
       always accidentally closed the database connection for the main
       database.
    
    This fix removes prior attempts to drop the connection(s)
    
    Database connections opened in other initializers are still an issue but
    Rails kindly closes them for us. Unfortunately Rails has an initializer,
    set_routes_reloader_hook which is run _after_ `config.after_initialize`.
    The sequence goes like this:
    
    1. Various initializers run, including route loading.
    1. `config.after_initialize` blocks run (called by
       Gitlab::Application.finisher_hook). One of the
       `config.after_initialize` blocks run is to close all database
       connections.
    1. `Gitlab::Application.set_routes_reloader_hook` initializer runs. This
       loads routes again.
    
    So this fix is simple: we simply call the same thing Rails to close all
    database connections again.
    200f0d90
db.rake 14.5 KB