From ec4b7645f6f4c22a400e3279c362af9c104b2037 Mon Sep 17 00:00:00 2001 From: Tim Riley Date: Wed, 10 Jul 2024 20:46:52 +1000 Subject: [PATCH] Skip conventional sqlite:// URLs from safeguards MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Update Safeguard::RemoteDatabaseUrl to skip URLs like "sqlite://path/to/file.sqlite”, which is now the conventional format for URLs to SQLite databases when using libraries like Sequel. --- lib/database_cleaner/safeguard.rb | 7 +++++-- spec/database_cleaner/safeguard_spec.rb | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/lib/database_cleaner/safeguard.rb b/lib/database_cleaner/safeguard.rb index 26646791..ec81afa7 100644 --- a/lib/database_cleaner/safeguard.rb +++ b/lib/database_cleaner/safeguard.rb @@ -1,3 +1,5 @@ +require "uri" + module DatabaseCleaner class Safeguard class Error < Exception @@ -42,7 +44,8 @@ class RemoteDatabaseUrl LOCAL = %w(localhost 127.0.0.1) def run - raise Error::RemoteDatabaseUrl if !skip? && given? + return if skip? + raise Error::RemoteDatabaseUrl if given? end private @@ -54,7 +57,7 @@ def given? def remote?(url) return false unless url parsed = URI.parse(url) - return false if parsed.scheme == 'sqlite3:' + return false if parsed.scheme == 'sqlite' || parsed.scheme == 'sqlite3' host = parsed.host return false if host.nil? || host.empty? diff --git a/spec/database_cleaner/safeguard_spec.rb b/spec/database_cleaner/safeguard_spec.rb index 8c9be543..b4459a96 100644 --- a/spec/database_cleaner/safeguard_spec.rb +++ b/spec/database_cleaner/safeguard_spec.rb @@ -45,7 +45,23 @@ module DatabaseCleaner end end - describe 'to a sqlite db' do + describe 'to a sqlite url' do + let(:database_url) { 'sqlite://tmp/db.sqlite3' } + + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end + + describe 'to a sqlite3 url' do + let(:database_url) { 'sqlite3://tmp/db.sqlite3' } + + it 'does not raise' do + expect { cleaner.start }.to_not raise_error + end + end + + describe 'to a sqlite3 url with no slashes after the scheme' do let(:database_url) { 'sqlite3:tmp/db.sqlite3' } it 'does not raise' do