Skip to content

Commit fed9521

Browse files
committed
remove the ability to pass a class name for a sqlite extension
1 parent a8b1655 commit fed9521

File tree

2 files changed

+35
-66
lines changed

2 files changed

+35
-66
lines changed

lib/sqlite3/database.rb

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,6 @@ module SQLite3
7373
# Note that the constructor will implicitly call #enable_load_extension if the +extensions:+
7474
# keyword argument is present.
7575
#
76-
# Finally, it's also possible to load an extension by passing the _name_ of a constant that
77-
# implements the +_ExtensionSpecifier+ interface:
78-
#
79-
# db = SQLite3::Database.new(":memory:", extensions: ["SQLean::Crypto"])
80-
#
81-
# Handling the name of a constant like this is what enables injection of an extension in a Rails
82-
# application's `config/database.yml` file, like this:
83-
#
84-
# development:
85-
# adapter: sqlite3
86-
# database: storage/development.sqlite3
87-
# extensions:
88-
# - SQLean::Crypto
89-
#
9076
class Database
9177
attr_reader :collations
9278

@@ -183,7 +169,7 @@ def initialize file, options = {}, zvfs = nil
183169
@readonly = mode & Constants::Open::READONLY != 0
184170
@default_transaction_mode = options[:default_transaction_mode] || :deferred
185171

186-
marshal_extensions(options[:extensions])
172+
initialize_extensions(options[:extensions])
187173

188174
ForkSafety.track(self)
189175

@@ -738,26 +724,14 @@ def load_extension(extension_specifier)
738724
load_extension_internal(extension_specifier)
739725
end
740726

741-
def marshal_extensions(extensions) # :nodoc:
727+
def initialize_extensions(extensions) # :nodoc:
742728
return if extensions.nil?
743729
raise TypeError, "extensions must be an Array" unless extensions.is_a?(Array)
744730
return if extensions.empty?
745731

746732
enable_load_extension(true)
747733

748734
extensions.each do |extension|
749-
# marshall the extension into an object if it's the name of a constant that responds to
750-
# `#to_path`
751-
if extension.is_a?(String)
752-
begin
753-
extension_spec = Object.const_get(extension)
754-
if extension_spec.respond_to?(:to_path)
755-
extension = extension_spec
756-
end
757-
rescue NameError
758-
end
759-
end
760-
761735
load_extension(extension)
762736
end
763737
end

test/test_database.rb

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ def test_load_extension_error_with_nonexistent_path
671671
skip("extensions are not enabled") unless db.respond_to?(:load_extension)
672672
db.enable_load_extension(true)
673673

674-
assert_raises(SQLite3::Exception) { db.load_extension("/path/to/extension") }
675-
assert_raises(SQLite3::Exception) { db.load_extension(Pathname.new("foo")) }
674+
assert_raises(SQLite3::Exception) { db.load_extension("/nonexistent/path") }
675+
assert_raises(SQLite3::Exception) { db.load_extension(Pathname.new("nonexistent")) }
676676
end
677677

678678
def test_load_extension_error_with_invalid_argument
@@ -697,81 +697,76 @@ def test_load_extension_with_an_extension_descriptor
697697
assert_equal(["/path/to/extension"], db.load_extension_internal_path)
698698
end
699699

700-
def test_marshal_extensions_with_extensions_calls_enable_load_extension
700+
def test_initialize_extensions_with_extensions_calls_enable_load_extension
701701
mock_database_load_extension_internal(db)
702702
class << db
703-
attr_reader :enable_load_extension_called
703+
attr_accessor :enable_load_extension_called
704704

705705
def enable_load_extension(...)
706706
@enable_load_extension_called = true
707707
end
708708
end
709709

710-
db.marshal_extensions(nil)
710+
db.initialize_extensions(nil)
711711
refute(db.enable_load_extension_called)
712712

713-
db.marshal_extensions([])
713+
db.initialize_extensions([])
714714
refute(db.enable_load_extension_called)
715715

716-
db.marshal_extensions([FakeExtensionSpecifier])
716+
db.initialize_extensions(["/path/to/extension"])
717717
assert(db.enable_load_extension_called)
718-
end
719718

720-
def test_marshal_extensions_object_is_an_extension_specifier
721-
mock_database_load_extension_internal(db)
719+
db.enable_load_extension_called = false # reset
722720

723-
db.marshal_extensions([Pathname.new("/path/to/extension")])
724-
assert_equal(["/path/to/extension"], db.load_extension_internal_path)
721+
db.initialize_extensions([FakeExtensionSpecifier])
722+
assert(db.enable_load_extension_called)
723+
end
725724

726-
db.load_extension_internal_path.clear # reset
725+
def test_initialize_extensions_object_is_an_extension_specifier
726+
mock_database_load_extension_internal(db)
727727

728-
db.marshal_extensions([FakeExtensionSpecifier])
728+
db.initialize_extensions([Pathname.new("/path/to/extension")])
729729
assert_equal(["/path/to/extension"], db.load_extension_internal_path)
730730

731731
db.load_extension_internal_path.clear # reset
732732

733-
db.marshal_extensions(["SQLite3::FakeExtensionSpecifier"])
733+
db.initialize_extensions([FakeExtensionSpecifier])
734734
assert_equal(["/path/to/extension"], db.load_extension_internal_path)
735-
736-
db.load_extension_internal_path.clear # reset
737-
738-
db.marshal_extensions(["CannotBeResolved"])
739-
assert_equal(["CannotBeResolved"], db.load_extension_internal_path)
740735
end
741736

742-
def test_marshal_extensions_object_not_an_extension_specifier
737+
def test_initialize_extensions_object_not_an_extension_specifier
743738
mock_database_load_extension_internal(db)
744739

745-
db.marshal_extensions(["CannotBeResolved"])
746-
assert_equal(["CannotBeResolved"], db.load_extension_internal_path)
740+
db.initialize_extensions(["/path/to/extension"])
741+
assert_equal(["/path/to/extension"], db.load_extension_internal_path)
747742

748-
assert_raises(TypeError) { db.marshal_extensions([Class.new]) }
743+
assert_raises(TypeError) { db.initialize_extensions([Class.new]) }
749744

750-
assert_raises(TypeError) { db.marshal_extensions(FakeExtensionSpecifier) }
745+
assert_raises(TypeError) { db.initialize_extensions(FakeExtensionSpecifier) }
751746
end
752747

753-
def test_initialize_with_extensions_calls_marshal_extensions
754-
# ephemeral class to capture arguments passed to marshal_extensions
748+
def test_initialize_with_extensions_calls_initialize_extensions
749+
# ephemeral class to capture arguments passed to initialize_extensions
755750
klass = Class.new(SQLite3::Database) do
756-
attr :marshal_extensions_called, :marshal_extensions_arg
751+
attr :initialize_extensions_called, :initialize_extensions_arg
757752

758-
def marshal_extensions(extensions)
759-
@marshal_extensions_called = true
760-
@marshal_extensions_arg = extensions
753+
def initialize_extensions(extensions)
754+
@initialize_extensions_called = true
755+
@initialize_extensions_arg = extensions
761756
end
762757
end
763758

764759
db = klass.new(":memory:")
765-
assert(db.marshal_extensions_called)
766-
assert_nil(db.marshal_extensions_arg)
760+
assert(db.initialize_extensions_called)
761+
assert_nil(db.initialize_extensions_arg)
767762

768763
db = klass.new(":memory:", extensions: [])
769-
assert(db.marshal_extensions_called)
770-
assert_empty(db.marshal_extensions_arg)
764+
assert(db.initialize_extensions_called)
765+
assert_empty(db.initialize_extensions_arg)
771766

772-
db = klass.new(":memory:", extensions: ["path/to/ext1", "path/to/ext2", "ClassName"])
773-
assert(db.marshal_extensions_called)
774-
assert_equal(["path/to/ext1", "path/to/ext2", "ClassName"], db.marshal_extensions_arg)
767+
db = klass.new(":memory:", extensions: ["path/to/ext1", "path/to/ext2", FakeExtensionSpecifier])
768+
assert(db.initialize_extensions_called)
769+
assert_equal(["path/to/ext1", "path/to/ext2", FakeExtensionSpecifier], db.initialize_extensions_arg)
775770
end
776771

777772
def test_raw_float_infinity

0 commit comments

Comments
 (0)