activerecord 6.1.6 → 7.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +4 -4
 - data/CHANGELOG.md +1627 -983
 - data/MIT-LICENSE +1 -1
 - data/README.rdoc +18 -18
 - data/lib/active_record/aggregations.rb +17 -14
 - data/lib/active_record/association_relation.rb +1 -11
 - data/lib/active_record/associations/association.rb +50 -19
 - data/lib/active_record/associations/association_scope.rb +17 -12
 - data/lib/active_record/associations/belongs_to_association.rb +28 -9
 - data/lib/active_record/associations/belongs_to_polymorphic_association.rb +10 -2
 - data/lib/active_record/associations/builder/association.rb +11 -5
 - data/lib/active_record/associations/builder/belongs_to.rb +40 -14
 - data/lib/active_record/associations/builder/collection_association.rb +10 -3
 - data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
 - data/lib/active_record/associations/builder/has_many.rb +3 -2
 - data/lib/active_record/associations/builder/has_one.rb +2 -1
 - data/lib/active_record/associations/builder/singular_association.rb +6 -2
 - data/lib/active_record/associations/collection_association.rb +35 -31
 - data/lib/active_record/associations/collection_proxy.rb +30 -15
 - data/lib/active_record/associations/disable_joins_association_scope.rb +59 -0
 - data/lib/active_record/associations/foreign_association.rb +10 -3
 - data/lib/active_record/associations/has_many_association.rb +28 -18
 - data/lib/active_record/associations/has_many_through_association.rb +12 -7
 - data/lib/active_record/associations/has_one_association.rb +20 -10
 - data/lib/active_record/associations/has_one_through_association.rb +1 -1
 - data/lib/active_record/associations/join_dependency.rb +26 -16
 - data/lib/active_record/associations/preloader/association.rb +207 -52
 - data/lib/active_record/associations/preloader/batch.rb +48 -0
 - data/lib/active_record/associations/preloader/branch.rb +147 -0
 - data/lib/active_record/associations/preloader/through_association.rb +50 -14
 - data/lib/active_record/associations/preloader.rb +50 -121
 - data/lib/active_record/associations/singular_association.rb +9 -3
 - data/lib/active_record/associations/through_association.rb +25 -14
 - data/lib/active_record/associations.rb +439 -305
 - data/lib/active_record/asynchronous_queries_tracker.rb +60 -0
 - data/lib/active_record/attribute_assignment.rb +1 -3
 - data/lib/active_record/attribute_methods/before_type_cast.rb +24 -2
 - data/lib/active_record/attribute_methods/dirty.rb +73 -22
 - data/lib/active_record/attribute_methods/primary_key.rb +78 -26
 - data/lib/active_record/attribute_methods/query.rb +31 -19
 - data/lib/active_record/attribute_methods/read.rb +25 -10
 - data/lib/active_record/attribute_methods/serialization.rb +194 -37
 - data/lib/active_record/attribute_methods/time_zone_conversion.rb +4 -3
 - data/lib/active_record/attribute_methods/write.rb +10 -13
 - data/lib/active_record/attribute_methods.rb +121 -40
 - data/lib/active_record/attributes.rb +27 -38
 - data/lib/active_record/autosave_association.rb +61 -30
 - data/lib/active_record/base.rb +25 -2
 - data/lib/active_record/callbacks.rb +18 -34
 - data/lib/active_record/coders/column_serializer.rb +61 -0
 - data/lib/active_record/coders/json.rb +1 -1
 - data/lib/active_record/coders/yaml_column.rb +70 -34
 - data/lib/active_record/connection_adapters/abstract/connection_handler.rb +367 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +211 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +78 -0
 - data/lib/active_record/connection_adapters/abstract/connection_pool.rb +96 -590
 - data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -17
 - data/lib/active_record/connection_adapters/abstract/database_statements.rb +172 -50
 - data/lib/active_record/connection_adapters/abstract/query_cache.rb +77 -27
 - data/lib/active_record/connection_adapters/abstract/quoting.rb +87 -73
 - data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
 - data/lib/active_record/connection_adapters/abstract/schema_creation.rb +21 -20
 - data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +186 -31
 - data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
 - data/lib/active_record/connection_adapters/abstract/schema_statements.rb +360 -138
 - data/lib/active_record/connection_adapters/abstract/transaction.rb +281 -59
 - data/lib/active_record/connection_adapters/abstract_adapter.rb +631 -149
 - data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +285 -156
 - data/lib/active_record/connection_adapters/column.rb +13 -0
 - data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
 - data/lib/active_record/connection_adapters/mysql/database_statements.rb +25 -134
 - data/lib/active_record/connection_adapters/mysql/quoting.rb +56 -25
 - data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
 - data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
 - data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
 - data/lib/active_record/connection_adapters/mysql/schema_statements.rb +38 -14
 - data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
 - data/lib/active_record/connection_adapters/mysql2_adapter.rb +104 -53
 - data/lib/active_record/connection_adapters/pool_config.rb +20 -11
 - data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
 - data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
 - data/lib/active_record/connection_adapters/postgresql/database_statements.rb +89 -52
 - data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
 - data/lib/active_record/connection_adapters/postgresql/oid/date.rb +8 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +5 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +53 -14
 - data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
 - data/lib/active_record/connection_adapters/postgresql/oid/range.rb +12 -3
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp.rb +15 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +30 -0
 - data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +18 -6
 - data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -0
 - data/lib/active_record/connection_adapters/postgresql/quoting.rb +89 -56
 - data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +28 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +92 -2
 - data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +153 -3
 - data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +78 -0
 - data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +394 -74
 - data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
 - data/lib/active_record/connection_adapters/postgresql_adapter.rb +509 -247
 - data/lib/active_record/connection_adapters/schema_cache.rb +319 -90
 - data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
 - data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +72 -53
 - data/lib/active_record/connection_adapters/sqlite3/quoting.rb +37 -21
 - data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
 - data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +43 -22
 - data/lib/active_record/connection_adapters/sqlite3_adapter.rb +294 -102
 - data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
 - data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
 - data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
 - data/lib/active_record/connection_adapters.rb +9 -6
 - data/lib/active_record/connection_handling.rb +107 -136
 - data/lib/active_record/core.rb +202 -223
 - data/lib/active_record/counter_cache.rb +46 -25
 - data/lib/active_record/database_configurations/connection_url_resolver.rb +2 -1
 - data/lib/active_record/database_configurations/database_config.rb +21 -12
 - data/lib/active_record/database_configurations/hash_config.rb +84 -16
 - data/lib/active_record/database_configurations/url_config.rb +18 -12
 - data/lib/active_record/database_configurations.rb +95 -59
 - data/lib/active_record/delegated_type.rb +61 -15
 - data/lib/active_record/deprecator.rb +7 -0
 - data/lib/active_record/destroy_association_async_job.rb +3 -1
 - data/lib/active_record/disable_joins_association_relation.rb +39 -0
 - data/lib/active_record/dynamic_matchers.rb +1 -1
 - data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
 - data/lib/active_record/encryption/cipher/aes256_gcm.rb +101 -0
 - data/lib/active_record/encryption/cipher.rb +53 -0
 - data/lib/active_record/encryption/config.rb +68 -0
 - data/lib/active_record/encryption/configurable.rb +60 -0
 - data/lib/active_record/encryption/context.rb +42 -0
 - data/lib/active_record/encryption/contexts.rb +76 -0
 - data/lib/active_record/encryption/derived_secret_key_provider.rb +18 -0
 - data/lib/active_record/encryption/deterministic_key_provider.rb +14 -0
 - data/lib/active_record/encryption/encryptable_record.rb +224 -0
 - data/lib/active_record/encryption/encrypted_attribute_type.rb +151 -0
 - data/lib/active_record/encryption/encrypted_fixtures.rb +38 -0
 - data/lib/active_record/encryption/encrypting_only_encryptor.rb +12 -0
 - data/lib/active_record/encryption/encryptor.rb +155 -0
 - data/lib/active_record/encryption/envelope_encryption_key_provider.rb +55 -0
 - data/lib/active_record/encryption/errors.rb +15 -0
 - data/lib/active_record/encryption/extended_deterministic_queries.rb +157 -0
 - data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +28 -0
 - data/lib/active_record/encryption/key.rb +28 -0
 - data/lib/active_record/encryption/key_generator.rb +53 -0
 - data/lib/active_record/encryption/key_provider.rb +46 -0
 - data/lib/active_record/encryption/message.rb +33 -0
 - data/lib/active_record/encryption/message_serializer.rb +92 -0
 - data/lib/active_record/encryption/null_encryptor.rb +21 -0
 - data/lib/active_record/encryption/properties.rb +76 -0
 - data/lib/active_record/encryption/read_only_null_encryptor.rb +24 -0
 - data/lib/active_record/encryption/scheme.rb +96 -0
 - data/lib/active_record/encryption.rb +56 -0
 - data/lib/active_record/enum.rb +154 -63
 - data/lib/active_record/errors.rb +171 -15
 - data/lib/active_record/explain.rb +23 -3
 - data/lib/active_record/explain_registry.rb +11 -6
 - data/lib/active_record/explain_subscriber.rb +1 -1
 - data/lib/active_record/fixture_set/file.rb +15 -1
 - data/lib/active_record/fixture_set/model_metadata.rb +14 -4
 - data/lib/active_record/fixture_set/render_context.rb +2 -0
 - data/lib/active_record/fixture_set/table_row.rb +70 -14
 - data/lib/active_record/fixture_set/table_rows.rb +4 -4
 - data/lib/active_record/fixtures.rb +131 -86
 - data/lib/active_record/future_result.rb +164 -0
 - data/lib/active_record/gem_version.rb +3 -3
 - data/lib/active_record/inheritance.rb +81 -29
 - data/lib/active_record/insert_all.rb +135 -22
 - data/lib/active_record/integration.rb +11 -10
 - data/lib/active_record/internal_metadata.rb +119 -33
 - data/lib/active_record/legacy_yaml_adapter.rb +2 -39
 - data/lib/active_record/locking/optimistic.rb +36 -21
 - data/lib/active_record/locking/pessimistic.rb +15 -6
 - data/lib/active_record/log_subscriber.rb +52 -19
 - data/lib/active_record/marshalling.rb +56 -0
 - data/lib/active_record/message_pack.rb +124 -0
 - data/lib/active_record/middleware/database_selector/resolver.rb +10 -10
 - data/lib/active_record/middleware/database_selector.rb +23 -13
 - data/lib/active_record/middleware/shard_selector.rb +62 -0
 - data/lib/active_record/migration/command_recorder.rb +112 -14
 - data/lib/active_record/migration/compatibility.rb +221 -48
 - data/lib/active_record/migration/default_strategy.rb +23 -0
 - data/lib/active_record/migration/execution_strategy.rb +19 -0
 - data/lib/active_record/migration/join_table.rb +1 -1
 - data/lib/active_record/migration/pending_migration_connection.rb +21 -0
 - data/lib/active_record/migration.rb +358 -171
 - data/lib/active_record/model_schema.rb +120 -101
 - data/lib/active_record/nested_attributes.rb +37 -18
 - data/lib/active_record/no_touching.rb +3 -3
 - data/lib/active_record/normalization.rb +167 -0
 - data/lib/active_record/persistence.rb +405 -85
 - data/lib/active_record/promise.rb +84 -0
 - data/lib/active_record/query_cache.rb +3 -21
 - data/lib/active_record/query_logs.rb +174 -0
 - data/lib/active_record/query_logs_formatter.rb +41 -0
 - data/lib/active_record/querying.rb +29 -6
 - data/lib/active_record/railtie.rb +219 -43
 - data/lib/active_record/railties/controller_runtime.rb +13 -9
 - data/lib/active_record/railties/databases.rake +188 -252
 - data/lib/active_record/railties/job_runtime.rb +23 -0
 - data/lib/active_record/readonly_attributes.rb +41 -3
 - data/lib/active_record/reflection.rb +241 -80
 - data/lib/active_record/relation/batches/batch_enumerator.rb +23 -7
 - data/lib/active_record/relation/batches.rb +192 -63
 - data/lib/active_record/relation/calculations.rb +219 -90
 - data/lib/active_record/relation/delegation.rb +27 -13
 - data/lib/active_record/relation/finder_methods.rb +108 -51
 - data/lib/active_record/relation/merger.rb +22 -13
 - data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
 - data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
 - data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
 - data/lib/active_record/relation/predicate_builder.rb +27 -20
 - data/lib/active_record/relation/query_attribute.rb +30 -12
 - data/lib/active_record/relation/query_methods.rb +654 -127
 - data/lib/active_record/relation/record_fetch_warning.rb +7 -9
 - data/lib/active_record/relation/spawn_methods.rb +20 -3
 - data/lib/active_record/relation/where_clause.rb +10 -19
 - data/lib/active_record/relation.rb +262 -120
 - data/lib/active_record/result.rb +37 -11
 - data/lib/active_record/runtime_registry.rb +18 -13
 - data/lib/active_record/sanitization.rb +65 -20
 - data/lib/active_record/schema.rb +36 -22
 - data/lib/active_record/schema_dumper.rb +73 -24
 - data/lib/active_record/schema_migration.rb +68 -33
 - data/lib/active_record/scoping/default.rb +72 -15
 - data/lib/active_record/scoping/named.rb +5 -13
 - data/lib/active_record/scoping.rb +65 -34
 - data/lib/active_record/secure_password.rb +60 -0
 - data/lib/active_record/secure_token.rb +21 -3
 - data/lib/active_record/serialization.rb +6 -1
 - data/lib/active_record/signed_id.rb +10 -8
 - data/lib/active_record/store.rb +16 -11
 - data/lib/active_record/suppressor.rb +13 -15
 - data/lib/active_record/table_metadata.rb +16 -3
 - data/lib/active_record/tasks/database_tasks.rb +225 -136
 - data/lib/active_record/tasks/mysql_database_tasks.rb +16 -7
 - data/lib/active_record/tasks/postgresql_database_tasks.rb +35 -26
 - data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
 - data/lib/active_record/test_databases.rb +1 -1
 - data/lib/active_record/test_fixtures.rb +123 -99
 - data/lib/active_record/timestamp.rb +29 -18
 - data/lib/active_record/token_for.rb +113 -0
 - data/lib/active_record/touch_later.rb +11 -6
 - data/lib/active_record/transactions.rb +48 -27
 - data/lib/active_record/translation.rb +3 -3
 - data/lib/active_record/type/adapter_specific_registry.rb +32 -14
 - data/lib/active_record/type/hash_lookup_type_map.rb +34 -1
 - data/lib/active_record/type/internal/timezone.rb +7 -2
 - data/lib/active_record/type/serialized.rb +9 -5
 - data/lib/active_record/type/time.rb +4 -0
 - data/lib/active_record/type/type_map.rb +17 -20
 - data/lib/active_record/type.rb +1 -2
 - data/lib/active_record/validations/absence.rb +1 -1
 - data/lib/active_record/validations/associated.rb +4 -4
 - data/lib/active_record/validations/numericality.rb +5 -4
 - data/lib/active_record/validations/presence.rb +5 -28
 - data/lib/active_record/validations/uniqueness.rb +51 -6
 - data/lib/active_record/validations.rb +8 -4
 - data/lib/active_record/version.rb +1 -1
 - data/lib/active_record.rb +335 -32
 - data/lib/arel/attributes/attribute.rb +0 -8
 - data/lib/arel/crud.rb +28 -22
 - data/lib/arel/delete_manager.rb +18 -4
 - data/lib/arel/errors.rb +10 -0
 - data/lib/arel/factory_methods.rb +4 -0
 - data/lib/arel/filter_predications.rb +9 -0
 - data/lib/arel/insert_manager.rb +2 -3
 - data/lib/arel/nodes/and.rb +4 -0
 - data/lib/arel/nodes/binary.rb +6 -1
 - data/lib/arel/nodes/bound_sql_literal.rb +61 -0
 - data/lib/arel/nodes/casted.rb +1 -1
 - data/lib/arel/nodes/cte.rb +36 -0
 - data/lib/arel/nodes/delete_statement.rb +12 -13
 - data/lib/arel/nodes/filter.rb +10 -0
 - data/lib/arel/nodes/fragments.rb +35 -0
 - data/lib/arel/nodes/function.rb +1 -0
 - data/lib/arel/nodes/homogeneous_in.rb +1 -9
 - data/lib/arel/nodes/insert_statement.rb +2 -2
 - data/lib/arel/nodes/leading_join.rb +8 -0
 - data/lib/arel/nodes/node.rb +111 -2
 - data/lib/arel/nodes/select_core.rb +2 -2
 - data/lib/arel/nodes/select_statement.rb +2 -2
 - data/lib/arel/nodes/sql_literal.rb +6 -0
 - data/lib/arel/nodes/table_alias.rb +4 -0
 - data/lib/arel/nodes/update_statement.rb +8 -3
 - data/lib/arel/nodes.rb +5 -0
 - data/lib/arel/predications.rb +13 -3
 - data/lib/arel/select_manager.rb +10 -4
 - data/lib/arel/table.rb +9 -6
 - data/lib/arel/tree_manager.rb +0 -12
 - data/lib/arel/update_manager.rb +18 -4
 - data/lib/arel/visitors/dot.rb +80 -90
 - data/lib/arel/visitors/mysql.rb +16 -3
 - data/lib/arel/visitors/postgresql.rb +0 -10
 - data/lib/arel/visitors/to_sql.rb +139 -19
 - data/lib/arel/visitors/visitor.rb +2 -2
 - data/lib/arel.rb +18 -3
 - data/lib/rails/generators/active_record/application_record/USAGE +8 -0
 - data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +1 -1
 - data/lib/rails/generators/active_record/migration.rb +3 -1
 - data/lib/rails/generators/active_record/model/USAGE +113 -0
 - data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
 - data/lib/rails/generators/active_record/model/templates/abstract_base_class.rb.tt +1 -1
 - data/lib/rails/generators/active_record/model/templates/model.rb.tt +1 -1
 - data/lib/rails/generators/active_record/model/templates/module.rb.tt +2 -2
 - data/lib/rails/generators/active_record/multi_db/multi_db_generator.rb +16 -0
 - data/lib/rails/generators/active_record/multi_db/templates/multi_db.rb.tt +44 -0
 - metadata +93 -13
 - data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
 - data/lib/active_record/null_relation.rb +0 -67
 
| 
         @@ -0,0 +1,84 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 4 
     | 
    
         
            +
              class Promise < BasicObject
         
     | 
| 
      
 5 
     | 
    
         
            +
                undef_method :==, :!, :!=
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
                def initialize(future_result, block) # :nodoc:
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @future_result = future_result
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @block = block
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                # Returns whether the associated query is still being executed or not.
         
     | 
| 
      
 13 
     | 
    
         
            +
                def pending?
         
     | 
| 
      
 14 
     | 
    
         
            +
                  @future_result.pending?
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                # Returns the query result.
         
     | 
| 
      
 18 
     | 
    
         
            +
                # If the query wasn't completed yet, accessing +#value+ will block until the query completes.
         
     | 
| 
      
 19 
     | 
    
         
            +
                # If the query failed, +#value+ will raise the corresponding error.
         
     | 
| 
      
 20 
     | 
    
         
            +
                def value
         
     | 
| 
      
 21 
     | 
    
         
            +
                  return @value if defined? @value
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                  result = @future_result.result
         
     | 
| 
      
 24 
     | 
    
         
            +
                  @value = if @block
         
     | 
| 
      
 25 
     | 
    
         
            +
                    @block.call(result)
         
     | 
| 
      
 26 
     | 
    
         
            +
                  else
         
     | 
| 
      
 27 
     | 
    
         
            +
                    result
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
                end
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
                # Returns a new +ActiveRecord::Promise+ that will apply the passed block
         
     | 
| 
      
 32 
     | 
    
         
            +
                # when the value is accessed:
         
     | 
| 
      
 33 
     | 
    
         
            +
                #
         
     | 
| 
      
 34 
     | 
    
         
            +
                #   Post.async_pick(:title).then { |title| title.upcase }.value
         
     | 
| 
      
 35 
     | 
    
         
            +
                #   # => "POST TITLE"
         
     | 
| 
      
 36 
     | 
    
         
            +
                def then(&block)
         
     | 
| 
      
 37 
     | 
    
         
            +
                  Promise.new(@future_result, @block ? @block >> block : block)
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
                [:class, :respond_to?, :is_a?].each do |method|
         
     | 
| 
      
 41 
     | 
    
         
            +
                  define_method(method, ::Object.instance_method(method))
         
     | 
| 
      
 42 
     | 
    
         
            +
                end
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
                def inspect # :nodoc:
         
     | 
| 
      
 45 
     | 
    
         
            +
                  "#<ActiveRecord::Promise status=#{status}>"
         
     | 
| 
      
 46 
     | 
    
         
            +
                end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                def pretty_print(q) # :nodoc:
         
     | 
| 
      
 49 
     | 
    
         
            +
                  q.text(inspect)
         
     | 
| 
      
 50 
     | 
    
         
            +
                end
         
     | 
| 
      
 51 
     | 
    
         
            +
             
     | 
| 
      
 52 
     | 
    
         
            +
                private
         
     | 
| 
      
 53 
     | 
    
         
            +
                  def status
         
     | 
| 
      
 54 
     | 
    
         
            +
                    if @future_result.pending?
         
     | 
| 
      
 55 
     | 
    
         
            +
                      :pending
         
     | 
| 
      
 56 
     | 
    
         
            +
                    elsif @future_result.canceled?
         
     | 
| 
      
 57 
     | 
    
         
            +
                      :canceled
         
     | 
| 
      
 58 
     | 
    
         
            +
                    else
         
     | 
| 
      
 59 
     | 
    
         
            +
                      :complete
         
     | 
| 
      
 60 
     | 
    
         
            +
                    end
         
     | 
| 
      
 61 
     | 
    
         
            +
                  end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                  class Complete < self # :nodoc:
         
     | 
| 
      
 64 
     | 
    
         
            +
                    attr_reader :value
         
     | 
| 
      
 65 
     | 
    
         
            +
             
     | 
| 
      
 66 
     | 
    
         
            +
                    def initialize(value)
         
     | 
| 
      
 67 
     | 
    
         
            +
                      @value = value
         
     | 
| 
      
 68 
     | 
    
         
            +
                    end
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
                    def then
         
     | 
| 
      
 71 
     | 
    
         
            +
                      Complete.new(yield @value)
         
     | 
| 
      
 72 
     | 
    
         
            +
                    end
         
     | 
| 
      
 73 
     | 
    
         
            +
             
     | 
| 
      
 74 
     | 
    
         
            +
                    def pending?
         
     | 
| 
      
 75 
     | 
    
         
            +
                      false
         
     | 
| 
      
 76 
     | 
    
         
            +
                    end
         
     | 
| 
      
 77 
     | 
    
         
            +
             
     | 
| 
      
 78 
     | 
    
         
            +
                    private
         
     | 
| 
      
 79 
     | 
    
         
            +
                      def status
         
     | 
| 
      
 80 
     | 
    
         
            +
                        :complete
         
     | 
| 
      
 81 
     | 
    
         
            +
                      end
         
     | 
| 
      
 82 
     | 
    
         
            +
                  end
         
     | 
| 
      
 83 
     | 
    
         
            +
              end
         
     | 
| 
      
 84 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -26,32 +26,14 @@ module ActiveRecord 
     | 
|
| 
       26 
26 
     | 
    
         
             
                end
         
     | 
| 
       27 
27 
     | 
    
         | 
| 
       28 
28 
     | 
    
         
             
                def self.run
         
     | 
| 
       29 
     | 
    
         
            -
                   
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
       31 
     | 
    
         
            -
                  if ActiveRecord::Base.legacy_connection_handling
         
     | 
| 
       32 
     | 
    
         
            -
                    ActiveRecord::Base.connection_handlers.each do |key, handler|
         
     | 
| 
       33 
     | 
    
         
            -
                      pools.concat(handler.connection_pool_list.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
         
     | 
| 
       34 
     | 
    
         
            -
                    end
         
     | 
| 
       35 
     | 
    
         
            -
                  else
         
     | 
| 
       36 
     | 
    
         
            -
                    pools.concat(ActiveRecord::Base.connection_handler.all_connection_pools.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! })
         
     | 
| 
       37 
     | 
    
         
            -
                  end
         
     | 
| 
       38 
     | 
    
         
            -
             
     | 
| 
       39 
     | 
    
         
            -
                  pools
         
     | 
| 
      
 29 
     | 
    
         
            +
                  ActiveRecord::Base.connection_handler.each_connection_pool.reject { |p| p.query_cache_enabled }.each { |p| p.enable_query_cache! }
         
     | 
| 
       40 
30 
     | 
    
         
             
                end
         
     | 
| 
       41 
31 
     | 
    
         | 
| 
       42 
32 
     | 
    
         
             
                def self.complete(pools)
         
     | 
| 
       43 
33 
     | 
    
         
             
                  pools.each { |pool| pool.disable_query_cache! }
         
     | 
| 
       44 
34 
     | 
    
         | 
| 
       45 
     | 
    
         
            -
                   
     | 
| 
       46 
     | 
    
         
            -
                     
     | 
| 
       47 
     | 
    
         
            -
                      handler.connection_pool_list.each do |pool|
         
     | 
| 
       48 
     | 
    
         
            -
                        pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
         
     | 
| 
       49 
     | 
    
         
            -
                      end
         
     | 
| 
       50 
     | 
    
         
            -
                    end
         
     | 
| 
       51 
     | 
    
         
            -
                  else
         
     | 
| 
       52 
     | 
    
         
            -
                    ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
         
     | 
| 
       53 
     | 
    
         
            -
                      pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
         
     | 
| 
       54 
     | 
    
         
            -
                    end
         
     | 
| 
      
 35 
     | 
    
         
            +
                  ActiveRecord::Base.connection_handler.each_connection_pool do |pool|
         
     | 
| 
      
 36 
     | 
    
         
            +
                    pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
         
     | 
| 
       55 
37 
     | 
    
         
             
                  end
         
     | 
| 
       56 
38 
     | 
    
         
             
                end
         
     | 
| 
       57 
39 
     | 
    
         | 
| 
         @@ -0,0 +1,174 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            require "active_support/core_ext/module/attribute_accessors_per_thread"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "active_record/query_logs_formatter"
         
     | 
| 
      
 5 
     | 
    
         
            +
             
     | 
| 
      
 6 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 7 
     | 
    
         
            +
              # = Active Record Query Logs
         
     | 
| 
      
 8 
     | 
    
         
            +
              #
         
     | 
| 
      
 9 
     | 
    
         
            +
              # Automatically append comments to SQL queries with runtime information tags. This can be used to trace troublesome
         
     | 
| 
      
 10 
     | 
    
         
            +
              # SQL statements back to the application code that generated these statements.
         
     | 
| 
      
 11 
     | 
    
         
            +
              #
         
     | 
| 
      
 12 
     | 
    
         
            +
              # Query logs can be enabled via \Rails configuration in <tt>config/application.rb</tt> or an initializer:
         
     | 
| 
      
 13 
     | 
    
         
            +
              #
         
     | 
| 
      
 14 
     | 
    
         
            +
              #     config.active_record.query_log_tags_enabled = true
         
     | 
| 
      
 15 
     | 
    
         
            +
              #
         
     | 
| 
      
 16 
     | 
    
         
            +
              # By default the name of the application, the name and action of the controller, or the name of the job are logged.
         
     | 
| 
      
 17 
     | 
    
         
            +
              # The default format is {SQLCommenter}[https://open-telemetry.github.io/opentelemetry-sqlcommenter/].
         
     | 
| 
      
 18 
     | 
    
         
            +
              # The tags shown in a query comment can be configured via \Rails configuration:
         
     | 
| 
      
 19 
     | 
    
         
            +
              #
         
     | 
| 
      
 20 
     | 
    
         
            +
              #     config.active_record.query_log_tags = [ :application, :controller, :action, :job ]
         
     | 
| 
      
 21 
     | 
    
         
            +
              #
         
     | 
| 
      
 22 
     | 
    
         
            +
              # Active Record defines default tags available for use:
         
     | 
| 
      
 23 
     | 
    
         
            +
              #
         
     | 
| 
      
 24 
     | 
    
         
            +
              # * +application+
         
     | 
| 
      
 25 
     | 
    
         
            +
              # * +pid+
         
     | 
| 
      
 26 
     | 
    
         
            +
              # * +socket+
         
     | 
| 
      
 27 
     | 
    
         
            +
              # * +db_host+
         
     | 
| 
      
 28 
     | 
    
         
            +
              # * +database+
         
     | 
| 
      
 29 
     | 
    
         
            +
              #
         
     | 
| 
      
 30 
     | 
    
         
            +
              # Action Controller adds default tags when loaded:
         
     | 
| 
      
 31 
     | 
    
         
            +
              #
         
     | 
| 
      
 32 
     | 
    
         
            +
              # * +controller+
         
     | 
| 
      
 33 
     | 
    
         
            +
              # * +action+
         
     | 
| 
      
 34 
     | 
    
         
            +
              # * +namespaced_controller+
         
     | 
| 
      
 35 
     | 
    
         
            +
              #
         
     | 
| 
      
 36 
     | 
    
         
            +
              # Active Job adds default tags when loaded:
         
     | 
| 
      
 37 
     | 
    
         
            +
              #
         
     | 
| 
      
 38 
     | 
    
         
            +
              # * +job+
         
     | 
| 
      
 39 
     | 
    
         
            +
              #
         
     | 
| 
      
 40 
     | 
    
         
            +
              # New comment tags can be defined by adding them in a +Hash+ to the tags +Array+. Tags can have dynamic content by
         
     | 
| 
      
 41 
     | 
    
         
            +
              # setting a +Proc+ or lambda value in the +Hash+, and can reference any value stored by \Rails in the +context+ object.
         
     | 
| 
      
 42 
     | 
    
         
            +
              # ActiveSupport::CurrentAttributes can be used to store application values. Tags with +nil+ values are
         
     | 
| 
      
 43 
     | 
    
         
            +
              # omitted from the query comment.
         
     | 
| 
      
 44 
     | 
    
         
            +
              #
         
     | 
| 
      
 45 
     | 
    
         
            +
              # Escaping is performed on the string returned, however untrusted user input should not be used.
         
     | 
| 
      
 46 
     | 
    
         
            +
              #
         
     | 
| 
      
 47 
     | 
    
         
            +
              # Example:
         
     | 
| 
      
 48 
     | 
    
         
            +
              #
         
     | 
| 
      
 49 
     | 
    
         
            +
              #     config.active_record.query_log_tags = [
         
     | 
| 
      
 50 
     | 
    
         
            +
              #       :namespaced_controller,
         
     | 
| 
      
 51 
     | 
    
         
            +
              #       :action,
         
     | 
| 
      
 52 
     | 
    
         
            +
              #       :job,
         
     | 
| 
      
 53 
     | 
    
         
            +
              #       {
         
     | 
| 
      
 54 
     | 
    
         
            +
              #         request_id: ->(context) { context[:controller]&.request&.request_id },
         
     | 
| 
      
 55 
     | 
    
         
            +
              #         job_id: ->(context) { context[:job]&.job_id },
         
     | 
| 
      
 56 
     | 
    
         
            +
              #         tenant_id: -> { Current.tenant&.id },
         
     | 
| 
      
 57 
     | 
    
         
            +
              #         static: "value",
         
     | 
| 
      
 58 
     | 
    
         
            +
              #       },
         
     | 
| 
      
 59 
     | 
    
         
            +
              #     ]
         
     | 
| 
      
 60 
     | 
    
         
            +
              #
         
     | 
| 
      
 61 
     | 
    
         
            +
              # By default the name of the application, the name and action of the controller, or the name of the job are logged
         
     | 
| 
      
 62 
     | 
    
         
            +
              # using the {SQLCommenter}[https://open-telemetry.github.io/opentelemetry-sqlcommenter/] format. This can be changed
         
     | 
| 
      
 63 
     | 
    
         
            +
              # via {config.active_record.query_log_tags_format}[https://guides.rubyonrails.org/configuring.html#config-active-record-query-log-tags-format]
         
     | 
| 
      
 64 
     | 
    
         
            +
              #
         
     | 
| 
      
 65 
     | 
    
         
            +
              # Tag comments can be prepended to the query:
         
     | 
| 
      
 66 
     | 
    
         
            +
              #
         
     | 
| 
      
 67 
     | 
    
         
            +
              #    ActiveRecord::QueryLogs.prepend_comment = true
         
     | 
| 
      
 68 
     | 
    
         
            +
              #
         
     | 
| 
      
 69 
     | 
    
         
            +
              # For applications where the content will not change during the lifetime of
         
     | 
| 
      
 70 
     | 
    
         
            +
              # the request or job execution, the tags can be cached for reuse in every query:
         
     | 
| 
      
 71 
     | 
    
         
            +
              #
         
     | 
| 
      
 72 
     | 
    
         
            +
              #    config.active_record.cache_query_log_tags = true
         
     | 
| 
      
 73 
     | 
    
         
            +
              module QueryLogs
         
     | 
| 
      
 74 
     | 
    
         
            +
                mattr_accessor :taggings, instance_accessor: false, default: {}
         
     | 
| 
      
 75 
     | 
    
         
            +
                mattr_accessor :tags, instance_accessor: false, default: [ :application ]
         
     | 
| 
      
 76 
     | 
    
         
            +
                mattr_accessor :prepend_comment, instance_accessor: false, default: false
         
     | 
| 
      
 77 
     | 
    
         
            +
                mattr_accessor :cache_query_log_tags, instance_accessor: false, default: false
         
     | 
| 
      
 78 
     | 
    
         
            +
                mattr_accessor :tags_formatter, instance_accessor: false
         
     | 
| 
      
 79 
     | 
    
         
            +
                thread_mattr_accessor :cached_comment, instance_accessor: false
         
     | 
| 
      
 80 
     | 
    
         
            +
             
     | 
| 
      
 81 
     | 
    
         
            +
                class << self
         
     | 
| 
      
 82 
     | 
    
         
            +
                  def call(sql, connection) # :nodoc:
         
     | 
| 
      
 83 
     | 
    
         
            +
                    comment = self.comment(connection)
         
     | 
| 
      
 84 
     | 
    
         
            +
             
     | 
| 
      
 85 
     | 
    
         
            +
                    if comment.blank?
         
     | 
| 
      
 86 
     | 
    
         
            +
                      sql
         
     | 
| 
      
 87 
     | 
    
         
            +
                    elsif prepend_comment
         
     | 
| 
      
 88 
     | 
    
         
            +
                      "#{comment} #{sql}"
         
     | 
| 
      
 89 
     | 
    
         
            +
                    else
         
     | 
| 
      
 90 
     | 
    
         
            +
                      "#{sql} #{comment}"
         
     | 
| 
      
 91 
     | 
    
         
            +
                    end
         
     | 
| 
      
 92 
     | 
    
         
            +
                  end
         
     | 
| 
      
 93 
     | 
    
         
            +
             
     | 
| 
      
 94 
     | 
    
         
            +
                  def clear_cache # :nodoc:
         
     | 
| 
      
 95 
     | 
    
         
            +
                    self.cached_comment = nil
         
     | 
| 
      
 96 
     | 
    
         
            +
                  end
         
     | 
| 
      
 97 
     | 
    
         
            +
             
     | 
| 
      
 98 
     | 
    
         
            +
                  # Updates the formatter to be what the passed in format is.
         
     | 
| 
      
 99 
     | 
    
         
            +
                  def update_formatter(format)
         
     | 
| 
      
 100 
     | 
    
         
            +
                    self.tags_formatter =
         
     | 
| 
      
 101 
     | 
    
         
            +
                      case format
         
     | 
| 
      
 102 
     | 
    
         
            +
                      when :legacy
         
     | 
| 
      
 103 
     | 
    
         
            +
                        LegacyFormatter.new
         
     | 
| 
      
 104 
     | 
    
         
            +
                      when :sqlcommenter
         
     | 
| 
      
 105 
     | 
    
         
            +
                        SQLCommenter.new
         
     | 
| 
      
 106 
     | 
    
         
            +
                      else
         
     | 
| 
      
 107 
     | 
    
         
            +
                        raise ArgumentError, "Formatter is unsupported: #{formatter}"
         
     | 
| 
      
 108 
     | 
    
         
            +
                      end
         
     | 
| 
      
 109 
     | 
    
         
            +
                  end
         
     | 
| 
      
 110 
     | 
    
         
            +
             
     | 
| 
      
 111 
     | 
    
         
            +
                  ActiveSupport::ExecutionContext.after_change { ActiveRecord::QueryLogs.clear_cache }
         
     | 
| 
      
 112 
     | 
    
         
            +
             
     | 
| 
      
 113 
     | 
    
         
            +
                  private
         
     | 
| 
      
 114 
     | 
    
         
            +
                    # Returns an SQL comment +String+ containing the query log tags.
         
     | 
| 
      
 115 
     | 
    
         
            +
                    # Sets and returns a cached comment if <tt>cache_query_log_tags</tt> is +true+.
         
     | 
| 
      
 116 
     | 
    
         
            +
                    def comment(connection)
         
     | 
| 
      
 117 
     | 
    
         
            +
                      if cache_query_log_tags
         
     | 
| 
      
 118 
     | 
    
         
            +
                        self.cached_comment ||= uncached_comment(connection)
         
     | 
| 
      
 119 
     | 
    
         
            +
                      else
         
     | 
| 
      
 120 
     | 
    
         
            +
                        uncached_comment(connection)
         
     | 
| 
      
 121 
     | 
    
         
            +
                      end
         
     | 
| 
      
 122 
     | 
    
         
            +
                    end
         
     | 
| 
      
 123 
     | 
    
         
            +
             
     | 
| 
      
 124 
     | 
    
         
            +
                    def formatter
         
     | 
| 
      
 125 
     | 
    
         
            +
                      self.tags_formatter || self.update_formatter(:legacy)
         
     | 
| 
      
 126 
     | 
    
         
            +
                    end
         
     | 
| 
      
 127 
     | 
    
         
            +
             
     | 
| 
      
 128 
     | 
    
         
            +
                    def uncached_comment(connection)
         
     | 
| 
      
 129 
     | 
    
         
            +
                      content = tag_content(connection)
         
     | 
| 
      
 130 
     | 
    
         
            +
             
     | 
| 
      
 131 
     | 
    
         
            +
                      if content.present?
         
     | 
| 
      
 132 
     | 
    
         
            +
                        "/*#{escape_sql_comment(content)}*/"
         
     | 
| 
      
 133 
     | 
    
         
            +
                      end
         
     | 
| 
      
 134 
     | 
    
         
            +
                    end
         
     | 
| 
      
 135 
     | 
    
         
            +
             
     | 
| 
      
 136 
     | 
    
         
            +
                    def escape_sql_comment(content)
         
     | 
| 
      
 137 
     | 
    
         
            +
                      # Sanitize a string to appear within a SQL comment
         
     | 
| 
      
 138 
     | 
    
         
            +
                      # For compatibility, this also surrounding "/*+", "/*", and "*/"
         
     | 
| 
      
 139 
     | 
    
         
            +
                      # characters, possibly with single surrounding space.
         
     | 
| 
      
 140 
     | 
    
         
            +
                      # Then follows that by replacing any internal "*/" or "/ *" with
         
     | 
| 
      
 141 
     | 
    
         
            +
                      # "* /" or "/ *"
         
     | 
| 
      
 142 
     | 
    
         
            +
                      comment = content.to_s.dup
         
     | 
| 
      
 143 
     | 
    
         
            +
                      comment.gsub!(%r{\A\s*/\*\+?\s?|\s?\*/\s*\Z}, "")
         
     | 
| 
      
 144 
     | 
    
         
            +
                      comment.gsub!("*/", "* /")
         
     | 
| 
      
 145 
     | 
    
         
            +
                      comment.gsub!("/*", "/ *")
         
     | 
| 
      
 146 
     | 
    
         
            +
                      comment
         
     | 
| 
      
 147 
     | 
    
         
            +
                    end
         
     | 
| 
      
 148 
     | 
    
         
            +
             
     | 
| 
      
 149 
     | 
    
         
            +
                    def tag_content(connection)
         
     | 
| 
      
 150 
     | 
    
         
            +
                      context = ActiveSupport::ExecutionContext.to_h
         
     | 
| 
      
 151 
     | 
    
         
            +
                      context[:connection] ||= connection
         
     | 
| 
      
 152 
     | 
    
         
            +
             
     | 
| 
      
 153 
     | 
    
         
            +
                      pairs = tags.flat_map { |i| [*i] }.filter_map do |tag|
         
     | 
| 
      
 154 
     | 
    
         
            +
                        key, handler = tag
         
     | 
| 
      
 155 
     | 
    
         
            +
                        handler ||= taggings[key]
         
     | 
| 
      
 156 
     | 
    
         
            +
             
     | 
| 
      
 157 
     | 
    
         
            +
                        val = if handler.nil?
         
     | 
| 
      
 158 
     | 
    
         
            +
                          context[key]
         
     | 
| 
      
 159 
     | 
    
         
            +
                        elsif handler.respond_to?(:call)
         
     | 
| 
      
 160 
     | 
    
         
            +
                          if handler.arity == 0
         
     | 
| 
      
 161 
     | 
    
         
            +
                            handler.call
         
     | 
| 
      
 162 
     | 
    
         
            +
                          else
         
     | 
| 
      
 163 
     | 
    
         
            +
                            handler.call(context)
         
     | 
| 
      
 164 
     | 
    
         
            +
                          end
         
     | 
| 
      
 165 
     | 
    
         
            +
                        else
         
     | 
| 
      
 166 
     | 
    
         
            +
                          handler
         
     | 
| 
      
 167 
     | 
    
         
            +
                        end
         
     | 
| 
      
 168 
     | 
    
         
            +
                        [key, val] unless val.nil?
         
     | 
| 
      
 169 
     | 
    
         
            +
                      end
         
     | 
| 
      
 170 
     | 
    
         
            +
                      self.formatter.format(pairs)
         
     | 
| 
      
 171 
     | 
    
         
            +
                    end
         
     | 
| 
      
 172 
     | 
    
         
            +
                end
         
     | 
| 
      
 173 
     | 
    
         
            +
              end
         
     | 
| 
      
 174 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,41 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # frozen_string_literal: true
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            module ActiveRecord
         
     | 
| 
      
 4 
     | 
    
         
            +
              module QueryLogs
         
     | 
| 
      
 5 
     | 
    
         
            +
                class LegacyFormatter # :nodoc:
         
     | 
| 
      
 6 
     | 
    
         
            +
                  def initialize
         
     | 
| 
      
 7 
     | 
    
         
            +
                    @key_value_separator = ":"
         
     | 
| 
      
 8 
     | 
    
         
            +
                  end
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
                  # Formats the key value pairs into a string.
         
     | 
| 
      
 11 
     | 
    
         
            +
                  def format(pairs)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    pairs.map! do |key, value|
         
     | 
| 
      
 13 
     | 
    
         
            +
                      "#{key}#{key_value_separator}#{format_value(value)}"
         
     | 
| 
      
 14 
     | 
    
         
            +
                    end.join(",")
         
     | 
| 
      
 15 
     | 
    
         
            +
                  end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
                  private
         
     | 
| 
      
 18 
     | 
    
         
            +
                    attr_reader :key_value_separator
         
     | 
| 
      
 19 
     | 
    
         
            +
             
     | 
| 
      
 20 
     | 
    
         
            +
                    def format_value(value)
         
     | 
| 
      
 21 
     | 
    
         
            +
                      value
         
     | 
| 
      
 22 
     | 
    
         
            +
                    end
         
     | 
| 
      
 23 
     | 
    
         
            +
                end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                class SQLCommenter < LegacyFormatter # :nodoc:
         
     | 
| 
      
 26 
     | 
    
         
            +
                  def initialize
         
     | 
| 
      
 27 
     | 
    
         
            +
                    @key_value_separator = "="
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                  def format(pairs)
         
     | 
| 
      
 31 
     | 
    
         
            +
                    pairs.sort_by!(&:first)
         
     | 
| 
      
 32 
     | 
    
         
            +
                    super
         
     | 
| 
      
 33 
     | 
    
         
            +
                  end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                  private
         
     | 
| 
      
 36 
     | 
    
         
            +
                    def format_value(value)
         
     | 
| 
      
 37 
     | 
    
         
            +
                      "'#{ERB::Util.url_encode(value)}'"
         
     | 
| 
      
 38 
     | 
    
         
            +
                    end
         
     | 
| 
      
 39 
     | 
    
         
            +
                end
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -3,7 +3,7 @@ 
     | 
|
| 
       3 
3 
     | 
    
         
             
            module ActiveRecord
         
     | 
| 
       4 
4 
     | 
    
         
             
              module Querying
         
     | 
| 
       5 
5 
     | 
    
         
             
                QUERYING_METHODS = [
         
     | 
| 
       6 
     | 
    
         
            -
                  :find, :find_by, :find_by!, :take, :take!, :first, :first!, :last, :last!,
         
     | 
| 
      
 6 
     | 
    
         
            +
                  :find, :find_by, :find_by!, :take, :take!, :sole, :find_sole_by, :first, :first!, :last, :last!,
         
     | 
| 
       7 
7 
     | 
    
         
             
                  :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!,
         
     | 
| 
       8 
8 
     | 
    
         
             
                  :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!,
         
     | 
| 
       9 
9 
     | 
    
         
             
                  :exists?, :any?, :many?, :none?, :one?,
         
     | 
| 
         @@ -12,12 +12,13 @@ module ActiveRecord 
     | 
|
| 
       12 
12 
     | 
    
         
             
                  :create_or_find_by, :create_or_find_by!,
         
     | 
| 
       13 
13 
     | 
    
         
             
                  :destroy_all, :delete_all, :update_all, :touch_all, :destroy_by, :delete_by,
         
     | 
| 
       14 
14 
     | 
    
         
             
                  :find_each, :find_in_batches, :in_batches,
         
     | 
| 
       15 
     | 
    
         
            -
                  :select, :reselect, :order, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
         
     | 
| 
       16 
     | 
    
         
            -
                  :where, :rewhere, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,
         
     | 
| 
      
 15 
     | 
    
         
            +
                  :select, :reselect, :order, :regroup, :in_order_of, :reorder, :group, :limit, :offset, :joins, :left_joins, :left_outer_joins,
         
     | 
| 
      
 16 
     | 
    
         
            +
                  :where, :rewhere, :invert_where, :preload, :extract_associated, :eager_load, :includes, :from, :lock, :readonly,
         
     | 
| 
       17 
17 
     | 
    
         
             
                  :and, :or, :annotate, :optimizer_hints, :extending,
         
     | 
| 
       18 
18 
     | 
    
         
             
                  :having, :create_with, :distinct, :references, :none, :unscope, :merge, :except, :only,
         
     | 
| 
       19 
19 
     | 
    
         
             
                  :count, :average, :minimum, :maximum, :sum, :calculate,
         
     | 
| 
       20 
     | 
    
         
            -
                  :pluck, :pick, :ids, :strict_loading
         
     | 
| 
      
 20 
     | 
    
         
            +
                  :pluck, :pick, :ids, :async_ids, :strict_loading, :excluding, :without, :with,
         
     | 
| 
      
 21 
     | 
    
         
            +
                  :async_count, :async_average, :async_minimum, :async_maximum, :async_sum, :async_pluck, :async_pick,
         
     | 
| 
       21 
22 
     | 
    
         
             
                ].freeze # :nodoc:
         
     | 
| 
       22 
23 
     | 
    
         
             
                delegate(*QUERYING_METHODS, to: :all)
         
     | 
| 
       23 
24 
     | 
    
         | 
| 
         @@ -39,12 +40,29 @@ module ActiveRecord 
     | 
|
| 
       39 
40 
     | 
    
         
             
                #   Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id"
         
     | 
| 
       40 
41 
     | 
    
         
             
                #   # => [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "author"=>"Quentin"}>, ...]
         
     | 
| 
       41 
42 
     | 
    
         
             
                #
         
     | 
| 
       42 
     | 
    
         
            -
                # You can use the same string replacement techniques as you can with  
     | 
| 
      
 43 
     | 
    
         
            +
                # You can use the same string replacement techniques as you can with ActiveRecord::QueryMethods#where :
         
     | 
| 
       43 
44 
     | 
    
         
             
                #
         
     | 
| 
       44 
45 
     | 
    
         
             
                #   Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date]
         
     | 
| 
       45 
46 
     | 
    
         
             
                #   Post.find_by_sql ["SELECT body FROM comments WHERE author = :user_id OR approved_by = :user_id", { :user_id => user_id }]
         
     | 
| 
      
 47 
     | 
    
         
            +
                #
         
     | 
| 
      
 48 
     | 
    
         
            +
                # Note that building your own SQL query string from user input may expose your application to
         
     | 
| 
      
 49 
     | 
    
         
            +
                # injection attacks (https://guides.rubyonrails.org/security.html#sql-injection).
         
     | 
| 
       46 
50 
     | 
    
         
             
                def find_by_sql(sql, binds = [], preparable: nil, &block)
         
     | 
| 
       47 
     | 
    
         
            -
                   
     | 
| 
      
 51 
     | 
    
         
            +
                  _load_from_sql(_query_by_sql(sql, binds, preparable: preparable), &block)
         
     | 
| 
      
 52 
     | 
    
         
            +
                end
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
                # Same as <tt>#find_by_sql</tt> but perform the query asynchronously and returns an ActiveRecord::Promise.
         
     | 
| 
      
 55 
     | 
    
         
            +
                def async_find_by_sql(sql, binds = [], preparable: nil, &block)
         
     | 
| 
      
 56 
     | 
    
         
            +
                  _query_by_sql(sql, binds, preparable: preparable, async: true).then do |result|
         
     | 
| 
      
 57 
     | 
    
         
            +
                    _load_from_sql(result, &block)
         
     | 
| 
      
 58 
     | 
    
         
            +
                  end
         
     | 
| 
      
 59 
     | 
    
         
            +
                end
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
                def _query_by_sql(sql, binds = [], preparable: nil, async: false) # :nodoc:
         
     | 
| 
      
 62 
     | 
    
         
            +
                  connection.select_all(sanitize_sql(sql), "#{name} Load", binds, preparable: preparable, async: async)
         
     | 
| 
      
 63 
     | 
    
         
            +
                end
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
                def _load_from_sql(result_set, &block) # :nodoc:
         
     | 
| 
       48 
66 
     | 
    
         
             
                  column_types = result_set.column_types
         
     | 
| 
       49 
67 
     | 
    
         | 
| 
       50 
68 
     | 
    
         
             
                  unless column_types.empty?
         
     | 
| 
         @@ -83,5 +101,10 @@ module ActiveRecord 
     | 
|
| 
       83 
101 
     | 
    
         
             
                def count_by_sql(sql)
         
     | 
| 
       84 
102 
     | 
    
         
             
                  connection.select_value(sanitize_sql(sql), "#{name} Count").to_i
         
     | 
| 
       85 
103 
     | 
    
         
             
                end
         
     | 
| 
      
 104 
     | 
    
         
            +
             
     | 
| 
      
 105 
     | 
    
         
            +
                # Same as <tt>#count_by_sql</tt> but perform the query asynchronously and returns an ActiveRecord::Promise.
         
     | 
| 
      
 106 
     | 
    
         
            +
                def async_count_by_sql(sql)
         
     | 
| 
      
 107 
     | 
    
         
            +
                  connection.select_value(sanitize_sql(sql), "#{name} Count", async: true).then(&:to_i)
         
     | 
| 
      
 108 
     | 
    
         
            +
                end
         
     | 
| 
       86 
109 
     | 
    
         
             
              end
         
     | 
| 
       87 
110 
     | 
    
         
             
            end
         
     |