Skip to content

Commit f25dd4d

Browse files
committed
Add Rails 7.1 support
ActiveRecord::InternalMetadata no longer inherits from ActiveRecord::Base: rails/rails@93ddf33 Reference: #9
1 parent 23a5900 commit f25dd4d

5 files changed

Lines changed: 33 additions & 11 deletions

File tree

lib/test_data.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,28 @@ def self.insert_test_data_dump
7070
InsertsTestData.new.call
7171
nil
7272
end
73+
74+
def self.metadata
75+
@metadata ||= if ActiveRecord::InternalMetadata.respond_to?(:find_by)
76+
ActiveRecord::InternalMetadata
77+
else
78+
ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection)
79+
end
80+
end
81+
82+
def self.find_metadata(key:)
83+
if metadata.respond_to?(:find_by)
84+
metadata.find_by(key: key)
85+
else
86+
metadata[key]
87+
end
88+
end
89+
90+
def self.create_metadata!(key:, value:)
91+
if metadata.respond_to?(:create!)
92+
create_metadata!(key: key, value: value)
93+
else
94+
metadata[key] = value
95+
end
96+
end
7397
end

lib/test_data/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Configuration
2222
def non_test_data_tables
2323
(@non_test_data_tables + [
2424
ActiveRecord::Base.connection.schema_migration.table_name,
25-
ActiveRecord::InternalMetadata.table_name
25+
TestData.metadata.table_name
2626
]).uniq
2727
end
2828

lib/test_data/determines_databases_associated_dump_time.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module TestData
22
class DeterminesDatabasesAssociatedDumpTime
33
def call
4-
if (last_dumped_at = ActiveRecord::InternalMetadata.find_by(key: "test_data:last_dumped_at")&.value)
4+
if (last_dumped_at = TestData.find_metadata(key: "test_data:last_dumped_at").presence)
55
Time.parse(last_dumped_at)
66
end
77
rescue ActiveRecord::StatementInvalid

lib/test_data/manager.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ def record_ar_internal_metadata_that_test_data_is_loaded
117117
if ar_internal_metadata_shows_test_data_is_loaded?
118118
TestData.log.warn "Attempted to record that test data is loaded in ar_internal_metadata, but record already existed. Perhaps a previous test run committed your test data?"
119119
else
120-
ActiveRecord::InternalMetadata.create!(key: "test_data:loaded", value: "true")
120+
TestData.create_metadata!(key: "test_data:loaded", value: "true")
121121
end
122122
end
123123

124124
def ar_internal_metadata_shows_test_data_is_loaded?
125-
ActiveRecord::InternalMetadata.find_by(key: "test_data:loaded")&.value == "true"
125+
TestData.find_metadata(key: "test_data:loaded") == "true"
126126
end
127127

128128
def ensure_after_truncate_save_point_is_active_if_data_is_truncated!
@@ -136,12 +136,12 @@ def record_ar_internal_metadata_that_test_data_is_truncated
136136
if ar_internal_metadata_shows_test_data_is_truncated?
137137
TestData.log.warn "Attempted to record that test data is truncated in ar_internal_metadata, but record already existed. Perhaps a previous test run committed the truncation of your test data?"
138138
else
139-
ActiveRecord::InternalMetadata.create!(key: "test_data:truncated", value: "true")
139+
TestData.create_metadata!(key: "test_data:truncated", value: "true")
140140
end
141141
end
142142

143143
def ar_internal_metadata_shows_test_data_is_truncated?
144-
ActiveRecord::InternalMetadata.find_by(key: "test_data:truncated")&.value == "true"
144+
TestData.find_metadata(key: "test_data:truncated") == "true"
145145
end
146146

147147
def ensure_custom_save_point_is_active_if_memo_exists!(name)
@@ -152,14 +152,14 @@ def ensure_custom_save_point_is_active_if_memo_exists!(name)
152152
end
153153

154154
def ar_internal_metadata_shows_custom_operation_was_persisted?(name)
155-
ActiveRecord::InternalMetadata.find_by(key: "test_data:#{name}")&.value == "true"
155+
TestData.find_metadata(key: "test_data:#{name}") == "true"
156156
end
157157

158158
def record_ar_internal_metadata_of_custom_save_point(name)
159159
if ar_internal_metadata_shows_custom_operation_was_persisted?(name)
160160
TestData.log.warn "Attempted to record that test_data had loaded #{name} in ar_internal_metadata, but record already existed. Perhaps a previous test run committed it?"
161161
else
162-
ActiveRecord::InternalMetadata.create!(key: "test_data:#{name}", value: "true")
162+
TestData.create_metadata!(key: "test_data:#{name}", value: "true")
163163
end
164164
end
165165

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
module TestData
22
class RecordsDumpMetadata
33
def call
4-
ActiveRecord::InternalMetadata
5-
.find_or_initialize_by(key: "test_data:last_dumped_at")
6-
.update!(value: Time.now.utc.inspect)
4+
TestData.create_metadata!(key: "test_data:last_dumped_at", value: Time.now.utc.inspect)
75
end
86
end
97
end

0 commit comments

Comments
 (0)