Skip to content

Commit

Permalink
Skip conventional sqlite:// URLs from safeguards
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timriley committed Jul 10, 2024
1 parent f458536 commit ec4b764
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/database_cleaner/safeguard.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "uri"

module DatabaseCleaner
class Safeguard
class Error < Exception
Expand Down Expand Up @@ -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
Expand All @@ -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?
Expand Down
18 changes: 17 additions & 1 deletion spec/database_cleaner/safeguard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ec4b764

Please sign in to comment.