| 1 | package Jifty::Plugin::SetupWizard::Action::TestDatabaseConnectivity; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use base 'Jifty::Action'; | ||
| 5 | |||
| 6 | use Jifty::Param::Schema; | ||
| 7 | use Jifty::Action schema { | ||
| 8 | param driver => | ||
| 9 | is mandatory, | ||
| 10 | type is 'text', | ||
| 11 | default is defer { Jifty->config->framework('Database')->{Driver} }; | ||
| 12 | |||
| 13 | param database => | ||
| 14 | is mandatory, | ||
| 15 | type is 'text', | ||
| 16 | default is defer { Jifty->config->framework('Database')->{Database} }; | ||
| 17 | |||
| 18 | param host => | ||
| 19 | type is 'text', | ||
| 20 | default is defer { Jifty->config->framework('Database')->{Host} }; | ||
| 21 | |||
| 22 | param port => | ||
| 23 | type is 'integer', | ||
| 24 | default is defer { Jifty->config->framework('Database')->{Port} }; | ||
| 25 | |||
| 26 | param user => | ||
| 27 | type is 'text', | ||
| 28 | default is defer { Jifty->config->framework('Database')->{User} }; | ||
| 29 | |||
| 30 | param password => | ||
| 31 | type is 'password', | ||
| 32 | default is defer { Jifty->config->framework('Database')->{Password} }; | ||
| 33 | |||
| 34 | param requiressl => | ||
| 35 | type is 'boolean', | ||
| 36 | default is defer { Jifty->config->framework('Database')->{RequireSSL} }; | ||
| 37 | }; | ||
| 38 | |||
| 39 | sub take_action { | ||
| 40 | my $self = shift; | ||
| 41 | |||
| 42 | # Remove empty arguments (empty port confuses DBI) | ||
| 43 | # Maybe should go in Jifty::DBI. it does handle undef.. | ||
| 44 | my %args = %{ $self->argument_values }; | ||
| 45 | for my $key (keys %args) { | ||
| 46 | delete $args{$key} if !defined($args{$key}) || !length($args{$key}); | ||
| 47 | } | ||
| 48 | |||
| 49 | my $handle = Jifty::DBI::Handle->new; | ||
| 50 | my $ok = eval { | ||
| 51 | local $SIG{__DIE__}; | ||
| 52 | |||
| 53 | # Connect returns undef if there's already a connection, so we | ||
| 54 | # only report failure to connect if an exception was thrown | ||
| 55 | $handle->connect(%args); | ||
| 56 | |||
| 57 | 1; | ||
| 58 | }; | ||
| 59 | my $error = $@; | ||
| 60 | |||
| 61 | # database will be created, so not worth complaining about this | ||
| 62 | $ok = 1 if $error =~ /Connection failed: Unknown database '/ | ||
| 63 | || $error =~ /Connection failed: .* database ".*" does not exist/; | ||
| 64 | |||
| 65 | if (!$ok) { | ||
| 66 | $error ||= _("No handle created"); | ||
| 67 | warn $error; | ||
| 68 | $error =~ s/ at .* line \d+$//; | ||
| 69 | return $self->result->error($error); | ||
| 70 | } | ||
| 71 | |||
| 72 | $self->result->message(_('Connection successful')); | ||
| 73 | } | ||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | __END__ |