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

(-)a/C4/Installer.pm (-1 / +25 lines)
Lines 30-36 use vars qw(@ISA @EXPORT); Link Here
30
BEGIN {
30
BEGIN {
31
    require Exporter;
31
    require Exporter;
32
    @ISA = qw( Exporter );
32
    @ISA = qw( Exporter );
33
    push @EXPORT, qw( foreign_key_exists index_exists column_exists );
33
    push @EXPORT, qw( foreign_key_exists index_exists column_exists default_exists );
34
};
34
};
35
35
36
=head1 NAME
36
=head1 NAME
Lines 525-530 sub index_exists { Link Here
525
    return $exists;
525
    return $exists;
526
}
526
}
527
527
528
=head2 default_exists
529
530
  my $default_check = default_exists($table_name,$field_name);
531
532
Determine if a particular field has a default value.
533
This is useful to prevent errors when there is no default value to drop.
534
535
=cut
536
528
sub column_exists {
537
sub column_exists {
529
    my ( $table_name, $column_name ) = @_;
538
    my ( $table_name, $column_name ) = @_;
530
    my $dbh = C4::Context->dbh;
539
    my $dbh = C4::Context->dbh;
Lines 537-542 sub column_exists { Link Here
537
    return $exists;
546
    return $exists;
538
}
547
}
539
548
549
550
sub default_exists {
551
    my ( $table_name, $column_name ) = @_;
552
    my $dbh = C4::Context->dbh;
553
    my @results = $dbh->selectall_array(qq{SHOW CREATE TABLE $table_name;});
554
    my $table_definition = $results[0][1]; # only 1 result, and the 2nd entry is the definition.
555
    my @lines = split /\n/, $table_definition;
556
    my @default_lines = grep { /DEFAULT/ } @lines;
557
    my $return_value = 0;
558
    if ( grep { /\`$column_name\`/ } @default_lines ) {
559
        $return_value = 1;
560
    }
561
    return $return_value;
562
}
563
540
=head1 AUTHOR
564
=head1 AUTHOR
541
565
542
C4::Installer is a refactoring of logic originally from installer/installer.pl, which was
566
C4::Installer is a refactoring of logic originally from installer/installer.pl, which was
(-)a/t/db_dependent/Installer.t (-2 / +4 lines)
Lines 22-28 Link Here
22
# Add more tests here!!!
22
# Add more tests here!!!
23
23
24
use Modern::Perl;
24
use Modern::Perl;
25
use Test::More tests => 15;
25
use Test::More tests => 17;
26
use Koha::Database;
26
use Koha::Database;
27
27
28
BEGIN {
28
BEGIN {
Lines 64-66 ok( ! index_exists( 'borrowers', 'xxx'), 'Constraint xxx does not exist' ); Link Here
64
64
65
ok( foreign_key_exists( 'borrowers', 'borrowers_ibfk_1' ), 'FK borrowers_ibfk_1 exists' );
65
ok( foreign_key_exists( 'borrowers', 'borrowers_ibfk_1' ), 'FK borrowers_ibfk_1 exists' );
66
ok( ! foreign_key_exists( 'borrowers', 'xxx' ), 'FK xxxx does not exist' );
66
ok( ! foreign_key_exists( 'borrowers', 'xxx' ), 'FK xxxx does not exist' );
67
- 
67
68
ok( default_exists( 'stockrotationrotas', 'cyclical' ), "Stock Rotation's cyclical field has a default" );
69
ok( ! default_exists( 'stockrotationrotas', 'rota_id' ), "Stock Rotation's rota_id field has no default" );

Return to bug 21682