Skip to content

Commit

Permalink
add extra config to main connect method
Browse files Browse the repository at this point in the history
We mostly use connect_from_config() to connect to the database. Now we
have the ability to add additional configuration to this method, for
example to add an `on_connect_do` subref to track reconnections to the
database.
  • Loading branch information
preaction committed Apr 20, 2018
1 parent 4c193dc commit fec3d0e
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/CPAN/Testers/Schema.pm
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ __PACKAGE__->upgrade_directory( dist_dir( 'CPAN-Testers-Schema' ) );

=method connect_from_config
my $schema = CPAN::Testers::Schema->connect_from_config;
my $schema = CPAN::Testers::Schema->connect_from_config( %extra_conf );
Connect to the MySQL database using a local MySQL configuration file
in C<$HOME/.cpanstats.cnf>. This configuration file should look like:
Expand All @@ -55,10 +55,14 @@ in C<$HOME/.cpanstats.cnf>. This configuration file should look like:
See L<DBD::mysql/mysql_read_default_file>.
C<%extra_conf> will be added to the L<DBIx::Class::Schema/connect>
method in the C<%dbi_attributes> hashref (see
L<DBIx::Class::Storage::DBI/connect_info>).
=cut

# Convenience connect method
sub connect_from_config ( $class ) {
sub connect_from_config ( $class, %config ) {
my $schema = $class->connect(
"DBI:mysql:mysql_read_default_file=$ENV{HOME}/.cpanstats.cnf;".
"mysql_read_default_group=application;mysql_enable_utf8=1",
Expand All @@ -70,6 +74,7 @@ sub connect_from_config ( $class ) {
mysql_enable_utf8 => 1,
quote_char => '`',
name_sep => '.',
%config,
},
);
return $schema;
Expand Down

0 comments on commit fec3d0e

Please sign in to comment.