From: Dylan William Hardison Date: Sun, 15 Dec 2019 17:13:34 +0000 (-0500) Subject: remove subclass loading and driver delegation from Schema->new. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cbbb15323f038d89a2c00aa8580f941f95d272a9;p=thirdparty%2Fbugzilla.git remove subclass loading and driver delegation from Schema->new. Bugzilla::DB::Schema->new() was both a normal constructor and also a class-loading factory method. It is simpler to just do the class loading at the call-site (in Bugzilla::DB::_bz_schema). It's not very likely extensions relied on this behavior so this should be a good change. --- diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 0fa9bf5038..7262cf888c 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -26,6 +26,7 @@ use Bugzilla::Error; use Bugzilla::DB::Schema; use Bugzilla::Version; +use Scalar::Util qw(blessed); use List::Util qw(max); use Storable qw(dclone); @@ -1155,12 +1156,21 @@ sub bz_set_next_serial_value { # Schema Information Methods ##################################################################### +sub _bz_schema_class { + my ($self) = @_; + my $class = blessed($self) // $self; + my @class_parts = split(/::/, $class); + splice(@class_parts, -1, 0, 'Schema'); + + return join('::', @class_parts); +} + sub _bz_schema { my ($self) = @_; return $self->{private_bz_schema} if exists $self->{private_bz_schema}; - my @module_parts = split('::', ref $self); - my $module_name = pop @module_parts; - $self->{private_bz_schema} = Bugzilla::DB::Schema->new($module_name); + my $schema_class = $self->_bz_schema_class; + eval "require $schema_class"; + $self->{private_bz_schema} = $schema_class->new(); return $self->{private_bz_schema}; } diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 2ab8a983a3..23d2cf0625 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -1758,9 +1758,7 @@ sub new { =item C Description: Public constructor method used to instantiate objects of this - class. However, it also can be used as a factory method to - instantiate database-specific subclasses when an optional - driver argument is supplied. + class. Parameters: $driver (optional) - Used to specify the type of database. This routine Cs if no subclass is found for the specified driver. @@ -1772,15 +1770,7 @@ sub new { my $this = shift; my $class = ref($this) || $this; - my $driver = shift; - - if ($driver) { - (my $subclass = $driver) =~ s/^(\S)/\U$1/; - $class .= '::' . $subclass; - eval "require $class;"; - die "The $class class could not be found ($subclass " . "not supported?): $@" - if ($@); - } + die "$class is an abstract base class. Instantiate a subclass instead." if ($class eq __PACKAGE__); @@ -2983,7 +2973,7 @@ sub deserialize_abstract { } } - return $class->new(undef, $thawed_hash); + return $class->new($thawed_hash); } ##################################################################### diff --git a/t/013dbschema.t b/t/013dbschema.t index 062a22992e..c2d827e4d3 100644 --- a/t/013dbschema.t +++ b/t/013dbschema.t @@ -19,6 +19,7 @@ use warnings; use lib qw(. t lib); use Bugzilla; use Bugzilla::DB::Schema; +use Bugzilla::DB::Schema::Mysql; # SQL reserved words @@ -57,7 +58,7 @@ our $schema; our @tables; BEGIN { - $schema = Bugzilla::DB::Schema->new("Mysql"); + $schema = Bugzilla::DB::Schema::Mysql->new; @tables = $schema->get_table_list(); }