• Yorick Peterse's avatar
    Fix Connection#exists? when using the DB LB · abbc8b8f
    Yorick Peterse authored
    Database::Connection#exists? would call `connection` to determine if
    the database exists. Without load balancing enabled, this would call
    `ActiveRecord::Base.connection`, which in turn calls
    `ActiveRecord::Base.retrieve_connection`. If no connection could be
    established or the database doesn't exist, this will raise an error.
    
    When using the load balancer, the `Connection#connection` method instead
    just returns a `ConnectionProxy`, without running any database queries.
    The result is that `exists?` would return `true` even if the database
    didn't exist.
    
    To work around this, `Connection#exists?` obtains the database version
    and caches this using Rails' schema cache. This ensures we run an actual
    database query the first time, without running a query _every_ time.
    Using the schema cache means we don't need to implement our own caching
    logic.
    
    Changelog: fixed
    abbc8b8f
connection_spec.rb 12.9 KB