Lines 18-26
Link Here
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
|
19 |
|
20 |
use C4::Context; |
20 |
use C4::Context; |
21 |
use C4::Log qw(cronlogaction); |
21 |
use C4::Log qw(cronlogaction); |
22 |
use Getopt::Long qw( GetOptions ); |
22 |
use Getopt::Long qw( GetOptions ); |
23 |
use Pod::Usage qw( pod2usage ); |
23 |
use Pod::Usage qw( pod2usage ); |
24 |
use Koha::Logger; |
24 |
use Koha::Logger; |
25 |
use Koha::DateUtils qw( dt_from_string ); |
25 |
use Koha::DateUtils qw( dt_from_string ); |
26 |
use Koha::Script -cron; |
26 |
use Koha::Script -cron; |
Lines 28-65
use Data::Dumper;
Link Here
|
28 |
use Koha::Database; |
28 |
use Koha::Database; |
29 |
|
29 |
|
30 |
my $verbose = 0; |
30 |
my $verbose = 0; |
31 |
my $doit = 0; |
31 |
my $doit = 0; |
32 |
|
32 |
|
33 |
GetOptions( |
33 |
GetOptions( |
34 |
'v|verbose' => \$verbose, |
34 |
'v|verbose' => \$verbose, |
35 |
'c|confirm' => \$doit, |
35 |
'c|confirm' => \$doit, |
36 |
); |
36 |
); |
37 |
|
37 |
|
38 |
my $schema = Koha::Database->new->schema; |
38 |
my $schema = Koha::Database->new->schema; |
|
|
39 |
|
39 |
# Loop over all the DBIx::Class classes |
40 |
# Loop over all the DBIx::Class classes |
40 |
for my $class ( sort values %{$schema->{class_mappings}} ) { |
41 |
for my $class ( sort values %{ $schema->{class_mappings} } ) { |
|
|
42 |
|
41 |
# Retrieve the resultset so we can access the columns info |
43 |
# Retrieve the resultset so we can access the columns info |
42 |
my $rs = $schema->resultset($class); |
44 |
my $rs = $schema->resultset($class); |
43 |
my $columns = $rs->result_source->columns_info; |
45 |
my $columns = $rs->result_source->columns_info; |
44 |
|
46 |
|
45 |
# Loop over the columns |
47 |
# Loop over the columns |
46 |
while ( my ( $column, $info ) = each %$columns ) { |
48 |
while ( my ( $column, $info ) = each %$columns ) { |
|
|
49 |
|
47 |
# Next if data type is not date/datetime/timestamp |
50 |
# Next if data type is not date/datetime/timestamp |
48 |
my $data_type = $info->{data_type}; |
51 |
my $data_type = $info->{data_type}; |
49 |
next unless grep { $data_type =~ m{^$_$} } qw( timestamp datetime date ); |
52 |
next unless grep { $data_type =~ m{^$_$} } qw( timestamp datetime date ); |
50 |
|
53 |
|
51 |
# Count the invalid dates |
54 |
# Count the invalid dates |
52 |
my $invalid_dates = $rs->search({ $column => '0000-00-00' })->count; |
55 |
my $invalid_dates = $rs->search( { $column => '0000-00-00' } )->count; |
53 |
|
56 |
|
54 |
next unless $invalid_dates; |
57 |
next unless $invalid_dates; |
55 |
|
58 |
|
56 |
if ($verbose) { |
59 |
if ($verbose) { |
57 |
say sprintf "Column %s.%s contains %s invalid dates", $rs->result_source->name, $column, $invalid_dates; |
60 |
say sprintf "Column %s.%s contains %s invalid dates", $rs->result_source->name, $column, $invalid_dates; |
58 |
} |
61 |
} |
59 |
|
62 |
|
60 |
if ($doit) { |
63 |
if ($doit) { |
61 |
$rs->search({ $column => '0000-00-00' })->update({ $column => undef }); |
64 |
$rs->search( { $column => '0000-00-00' } )->update( { $column => undef } ); |
62 |
say sprintf "Column %s.%s contains %s invalid dates that have been fixed", $rs->result_source->name, $column, $invalid_dates; |
65 |
say sprintf "Column %s.%s contains %s invalid dates that have been fixed", $rs->result_source->name, |
|
|
66 |
$column, $invalid_dates; |
63 |
} |
67 |
} |
64 |
|
68 |
|
65 |
} |
69 |
} |