diff --git a/lib/test_data.rb b/lib/test_data.rb index 3800809..0636d4c 100644 --- a/lib/test_data.rb +++ b/lib/test_data.rb @@ -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!) + metadata.create!(key: key, value: value) + else + metadata[key] = value + end + end end diff --git a/lib/test_data/config.rb b/lib/test_data/config.rb index 9522638..6884438 100644 --- a/lib/test_data/config.rb +++ b/lib/test_data/config.rb @@ -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 diff --git a/lib/test_data/determines_databases_associated_dump_time.rb b/lib/test_data/determines_databases_associated_dump_time.rb index 394ccc5..865762b 100644 --- a/lib/test_data/determines_databases_associated_dump_time.rb +++ b/lib/test_data/determines_databases_associated_dump_time.rb @@ -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 diff --git a/lib/test_data/manager.rb b/lib/test_data/manager.rb index e4917a7..4911a6c 100644 --- a/lib/test_data/manager.rb +++ b/lib/test_data/manager.rb @@ -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! @@ -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) @@ -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 diff --git a/lib/test_data/records_dump_metadata.rb b/lib/test_data/records_dump_metadata.rb index a98caad..0155d57 100644 --- a/lib/test_data/records_dump_metadata.rb +++ b/lib/test_data/records_dump_metadata.rb @@ -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