View | Details | Raw Unified | Return to bug 31143
Collapse All | Expand All

(-)a/misc/cronjobs/fix_invalid_dates.pl (+66 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use C4::Context;
21
use C4::Log qw(cronlogaction);
22
use Getopt::Long qw( GetOptions );
23
use Pod::Usage qw( pod2usage );
24
use Koha::Logger;
25
use Koha::DateUtils qw( dt_from_string );
26
use Koha::Script -cron;
27
use Data::Dumper;
28
use Koha::Database;
29
30
my $verbose = 0;
31
my $doit = 0;
32
33
GetOptions(
34
    'v|verbose'       => \$verbose,
35
    'c|confirm'       => \$doit,
36
);
37
38
my $schema = Koha::Database->new->schema;
39
# Loop over all the DBIx::Class classes
40
for my $class ( sort values %{$schema->{class_mappings}} ) {
41
    # Retrieve the resultset so we can access the columns info
42
    my $rs = $schema->resultset($class);
43
    my $columns = $rs->result_source->columns_info;
44
45
    # Loop over the columns
46
    while ( my ( $column, $info ) = each %$columns ) {
47
        # Next if data type is not date/datetime/timestamp
48
        my $data_type = $info->{data_type};
49
        next unless grep { $data_type =~ m{^$_$} } qw( timestamp datetime date );
50
51
        # Count the invalid dates
52
        my $invalid_dates = $rs->search({ $column => '0000-00-00' })->count;
53
54
        next unless $invalid_dates;
55
56
    if ($verbose) {
57
            say sprintf "Column %s.%s contains %s invalid dates", $rs->result_source->name, $column, $invalid_dates;
58
        }
59
60
        if ($doit) {
61
            $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;
63
        }
64
65
    }
66
}
(-)a/misc/maintenance/search_for_data_inconsistencies.pl (-1 / +32 lines)
Lines 326-331 use C4::Biblio qw( GetMarcFromKohaField ); Link Here
326
    }
326
    }
327
}
327
}
328
328
329
{
330
    use Koha::Database;
331
    my $schema = Koha::Database->new->schema;
332
    
333
    # Loop over all the DBIx::Class classes
334
    for my $class ( sort values %{$schema->{class_mappings}} ) {
335
        # Retrieve the resultset so we can access the columns info
336
        my $rs = $schema->resultset($class);
337
        my $columns = $rs->result_source->columns_info;
338
339
        # Loop over the columns
340
        while ( my ( $column, $info ) = each %$columns ) {
341
            # Next if data type is not date/datetime/timestamp
342
            my $data_type = $info->{data_type};
343
            next unless grep { $data_type =~ m{^$_$} } qw( timestamp datetime date );
344
345
            # Count the invalid dates
346
            my $invalid_dates = $rs->search({ $column => '0000-00-00' })->count;
347
348
            next unless $invalid_dates;
349
350
            new_section("Column " . $rs->result_source->name . "." . $column . " contains $invalid_dates invalid dates");
351
352
            if ($invalid_dates > 0) {
353
                new_hint("You may change the dates with script: misc/cronjobs/fix_invalid_dates.pl (-c -v)");
354
            }
355
356
        }
357
    }
358
}
359
329
{
360
{
330
    my @loop_borrowers_relationships;
361
    my @loop_borrowers_relationships;
331
    my @guarantor_ids = Koha::Patron::Relationships->_resultset->get_column('guarantor_id')->all();
362
    my @guarantor_ids = Koha::Patron::Relationships->_resultset->get_column('guarantor_id')->all();
Lines 450-454 Catch data inconsistencies in Koha database Link Here
450
  * Item types defined in items or biblioitems must be defined in the itemtypes table
481
  * Item types defined in items or biblioitems must be defined in the itemtypes table
451
* Invalid MARCXML in bibliographic records
482
* Invalid MARCXML in bibliographic records
452
* Patrons with invalid category types due to lower and upper age limits
483
* Patrons with invalid category types due to lower and upper age limits
484
* Any date fields in the database (timestamp, datetime, date) set to 0000-00-00 
453
485
454
=cut
486
=cut
455
- 

Return to bug 31143