@@ -19,30 +19,75 @@ def target_version
1919 end
2020
2121 def patch
22+ # Rails 7.0 intruduced new on-load hooks for sqlite3 and postgresql adapters
23+ # The load hook for mysql2 adapter was introduced in Rails 7.1
24+ #
25+ # If the adapter is not loaded when the :active_record load hook is called,
26+ # we need to add a load hook for the adapter
2227 ActiveSupport . on_load :active_record do
23- instrumentation_module = if ::ActiveRecord . gem_version >= Gem ::Version . new ( '7.1' )
24- Instrumentation ::InternalExecQueryAdapterPatch
25- else
26- Instrumentation ::ExecQueryAdapterPatch
27- end
28-
2928 if defined? ( ::ActiveRecord ::ConnectionAdapters ::SQLite3Adapter )
30- ::ActiveRecord ::ConnectionAdapters ::SQLite3Adapter . prepend ( instrumentation_module )
29+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_sqlite3_adapter
30+ else
31+ ActiveSupport . on_load :active_record_sqlite3adapter do
32+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_sqlite3_adapter
33+ end
3134 end
3235
3336 if defined? ( ::ActiveRecord ::ConnectionAdapters ::Mysql2Adapter )
34- ::ActiveRecord ::ConnectionAdapters ::Mysql2Adapter . prepend ( instrumentation_module )
37+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_mysql2_adapter
38+ else
39+ ActiveSupport . on_load :active_record_mysql2adapter do
40+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_mysql2_adapter
41+ end
3542 end
3643
3744 if defined? ( ::ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter )
38- unless defined? ( ::ActiveRecord ::ConnectionAdapters ::JdbcAdapter )
39- instrumentation_module = Instrumentation ::ExecuteAndClearAdapterPatch
45+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_postgresql_adapter
46+ else
47+ ActiveSupport . on_load :active_record_postgresqladapter do
48+ ::Datadog ::AppSec ::Contrib ::ActiveRecord ::Patcher . patch_postgresql_adapter
4049 end
41-
42- ::ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . prepend ( instrumentation_module )
4350 end
4451 end
4552 end
53+
54+ def patch_sqlite3_adapter
55+ instrumentation_module = if ::ActiveRecord . gem_version >= Gem ::Version . new ( '7.1' )
56+ Instrumentation ::InternalExecQueryAdapterPatch
57+ elsif ::ActiveRecord . gem_version . segments . first == 4
58+ Instrumentation ::Rails4ExecQueryAdapterPatch
59+ else
60+ Instrumentation ::ExecQueryAdapterPatch
61+ end
62+
63+ ::ActiveRecord ::ConnectionAdapters ::SQLite3Adapter . prepend ( instrumentation_module )
64+ end
65+
66+ def patch_mysql2_adapter
67+ instrumentation_module = if ::ActiveRecord . gem_version >= Gem ::Version . new ( '7.1' )
68+ Instrumentation ::InternalExecQueryAdapterPatch
69+ elsif ::ActiveRecord . gem_version . segments . first == 4
70+ Instrumentation ::Rails4ExecQueryAdapterPatch
71+ else
72+ Instrumentation ::ExecQueryAdapterPatch
73+ end
74+
75+ ::ActiveRecord ::ConnectionAdapters ::Mysql2Adapter . prepend ( instrumentation_module )
76+ end
77+
78+ def patch_postgresql_adapter
79+ jdbc_defined = defined? ( ::ActiveRecord ::ConnectionAdapters ::JdbcAdapter )
80+
81+ instrumentation_module = if jdbc_defined && ::ActiveRecord . gem_version >= Gem ::Version . new ( '7.1' )
82+ Instrumentation ::InternalExecQueryAdapterPatch
83+ elsif jdbc_defined && ::ActiveRecord . gem_version . segments . first == 4
84+ Instrumentation ::Rails4ExecuteAndClearAdapterPatch
85+ else
86+ Instrumentation ::ExecuteAndClearAdapterPatch
87+ end
88+
89+ ::ActiveRecord ::ConnectionAdapters ::PostgreSQLAdapter . prepend ( instrumentation_module )
90+ end
4691 end
4792 end
4893 end
0 commit comments