Skip to content

Commit

Permalink
Add Rails 7.1 support
Browse files Browse the repository at this point in the history
ActiveRecord::InternalMetadata no longer inherits from ActiveRecord::Base: rails/rails@93ddf33

Reference: testdouble#9
  • Loading branch information
thewatts committed Oct 28, 2023
1 parent 23a5900 commit f25dd4d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 11 deletions.
24 changes: 24 additions & 0 deletions lib/test_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,28 @@ def self.insert_test_data_dump
InsertsTestData.new.call
nil
end

def self.metadata
@metadata ||= if ActiveRecord::InternalMetadata.respond_to?(:find_by)
ActiveRecord::InternalMetadata
else
ActiveRecord::InternalMetadata.new(ActiveRecord::Base.connection)
end
end

def self.find_metadata(key:)
if metadata.respond_to?(:find_by)
metadata.find_by(key: key)
else
metadata[key]
end
end

def self.create_metadata!(key:, value:)
if metadata.respond_to?(:create!)
create_metadata!(key: key, value: value)
else
metadata[key] = value
end
end
end
2 changes: 1 addition & 1 deletion lib/test_data/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Configuration
def non_test_data_tables
(@non_test_data_tables + [
ActiveRecord::Base.connection.schema_migration.table_name,
ActiveRecord::InternalMetadata.table_name
TestData.metadata.table_name
]).uniq
end

Expand Down
2 changes: 1 addition & 1 deletion lib/test_data/determines_databases_associated_dump_time.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestData
class DeterminesDatabasesAssociatedDumpTime
def call
if (last_dumped_at = ActiveRecord::InternalMetadata.find_by(key: "test_data:last_dumped_at")&.value)
if (last_dumped_at = TestData.find_metadata(key: "test_data:last_dumped_at").presence)
Time.parse(last_dumped_at)
end
rescue ActiveRecord::StatementInvalid
Expand Down
12 changes: 6 additions & 6 deletions lib/test_data/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ def record_ar_internal_metadata_that_test_data_is_loaded
if ar_internal_metadata_shows_test_data_is_loaded?
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?"
else
ActiveRecord::InternalMetadata.create!(key: "test_data:loaded", value: "true")
TestData.create_metadata!(key: "test_data:loaded", value: "true")
end
end

def ar_internal_metadata_shows_test_data_is_loaded?
ActiveRecord::InternalMetadata.find_by(key: "test_data:loaded")&.value == "true"
TestData.find_metadata(key: "test_data:loaded") == "true"
end

def ensure_after_truncate_save_point_is_active_if_data_is_truncated!
Expand All @@ -136,12 +136,12 @@ def record_ar_internal_metadata_that_test_data_is_truncated
if ar_internal_metadata_shows_test_data_is_truncated?
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?"
else
ActiveRecord::InternalMetadata.create!(key: "test_data:truncated", value: "true")
TestData.create_metadata!(key: "test_data:truncated", value: "true")
end
end

def ar_internal_metadata_shows_test_data_is_truncated?
ActiveRecord::InternalMetadata.find_by(key: "test_data:truncated")&.value == "true"
TestData.find_metadata(key: "test_data:truncated") == "true"
end

def ensure_custom_save_point_is_active_if_memo_exists!(name)
Expand All @@ -152,14 +152,14 @@ def ensure_custom_save_point_is_active_if_memo_exists!(name)
end

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

def record_ar_internal_metadata_of_custom_save_point(name)
if ar_internal_metadata_shows_custom_operation_was_persisted?(name)
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?"
else
ActiveRecord::InternalMetadata.create!(key: "test_data:#{name}", value: "true")
TestData.create_metadata!(key: "test_data:#{name}", value: "true")
end
end

Expand Down
4 changes: 1 addition & 3 deletions lib/test_data/records_dump_metadata.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module TestData
class RecordsDumpMetadata
def call
ActiveRecord::InternalMetadata
.find_or_initialize_by(key: "test_data:last_dumped_at")
.update!(value: Time.now.utc.inspect)
TestData.create_metadata!(key: "test_data:last_dumped_at", value: Time.now.utc.inspect)
end
end
end

0 comments on commit f25dd4d

Please sign in to comment.