Skip to content
  • jcoleman's avatar
    9ad36e06
    Eliminate queries loading dumped model schema on Postgres · 9ad36e06
    jcoleman authored
    Commit 835eb8a2 (#48743) adds a comment making it clear the intention is
    to avoid connections being opened/used at app boot:
    
        For resiliency, it is critical that a Rails application should be
        able to boot without depending on the database (or any other service)
        being responsive.
    
    Additionally the followup commit 35a614c2 (#48793) is even more explicit
    with the title: "Never connect to the database in
    define_attribute_methods initializer".
    
    And in commit 618db137 (#49452) the commit message noted:
    
        Note, this really isn't good, ideally types should be stored in the
        schema cache so that we don't have to extract types every time.
    
    Unfortunately neither of these changes added any test coverage for those
    stated invariants (they simply modified existing tests to pass).
    
    I assume that the code likely had internal testing done where it was
    developed in house (I think using MySQL) and was validated in
    production, but the lack of test coverage in Rails itself led to missing
    the fact that the invariant is _not_ respected by the PostgreSQL
    adapater because looking up the cast types (used when defining default
    attributes) requires a connection to be open to retrieve the type map
    (which in the PostgreSQL adapter is OID based rather than type name
    based). Opening a connection not only requires the database to be
    available at boot, it also triggers multiple queries to load the type
    map as well as configuring things like timezones which, beyond causing
    resiliency concerns, further slows down app boot.
    
    In this commit we store the cast type value in the schema dump so that
    it can be loaded along with the dumped schema file at boot rather than,
    even when using cached schemas, having to connect to the database to
    finish defining attribute methods.
    9ad36e06
    Eliminate queries loading dumped model schema on Postgres
    jcoleman authored
    Commit 835eb8a2 (#48743) adds a comment making it clear the intention is
    to avoid connections being opened/used at app boot:
    
        For resiliency, it is critical that a Rails application should be
        able to boot without depending on the database (or any other service)
        being responsive.
    
    Additionally the followup commit 35a614c2 (#48793) is even more explicit
    with the title: "Never connect to the database in
    define_attribute_methods initializer".
    
    And in commit 618db137 (#49452) the commit message noted:
    
        Note, this really isn't good, ideally types should be stored in the
        schema cache so that we don't have to extract types every time.
    
    Unfortunately neither of these changes added any test coverage for those
    stated invariants (they simply modified existing tests to pass).
    
    I assume that the code likely had internal testing done where it was
    developed in house (I think using MySQL) and was validated in
    production, but the lack of test coverage in Rails itself led to missing
    the fact that the invariant is _not_ respected by the PostgreSQL
    adapater because looking up the cast types (used when defining default
    attributes) requires a connection to be open to retrieve the type map
    (which in the PostgreSQL adapter is OID based rather than type name
    based). Opening a connection not only requires the database to be
    available at boot, it also triggers multiple queries to load the type
    map as well as configuring things like timezones which, beyond causing
    resiliency concerns, further slows down app boot.
    
    In this commit we store the cast type value in the schema dump so that
    it can be loaded along with the dumped schema file at boot rather than,
    even when using cached schemas, having to connect to the database to
    finish defining attribute methods.
Loading