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

(-)a/C4/Installer.pm (-14 / +46 lines)
Lines 526-531 sub set_languages_syspref { Link Here
526
    C4::Context->clear_syspref_cache();
526
    C4::Context->clear_syspref_cache();
527
}
527
}
528
528
529
=head2 get_sql_statements_from_yml
530
531
  my @sql_statements = $installer->get_sql_statements_from_yml($filename[, $split]);
532
533
Load the YML file passed in parameter and return the list of statements found.
534
535
If $split is passed, each row will be inserted one by one.
536
537
=cut
538
539
sub get_sql_statements_from_yml {
540
    my ( $self, $filename, $split ) = @_;
541
    my $yaml = YAML::XS::LoadFile($filename);    # Load YAML
542
    my @statements;
543
    for my $table ( @{ $yaml->{'tables'} } ) {
544
        my $query_info   = process_yml_table($table);
545
        my $query        = $query_info->{query};
546
        my $placeholders = $query_info->{placeholders};
547
        my $values       = $query_info->{values};
548
549
        if ($split) {
550
            $query .= join ', ', ($placeholders);
551
            for my $value (@$values) {
552
                push @statements, [ $query, $value ];
553
            }
554
        } else {
555
556
            # Doing only 1 INSERT query for the whole table
557
            my @all_rows_values = map { @$_ } @$values;
558
            $query .= join ', ', ($placeholders) x scalar @$values;
559
            push @statements, [ $query, \@all_rows_values ];
560
        }
561
    }
562
563
    # extra SQL statements
564
    if ( $yaml->{sql_statements} ) {
565
        push @statements, [ map { [ $_, undef, undef ] } @{ $yaml->{'sql_statements'} } ];
566
    }
567
    return @statements;
568
}
569
529
=head2 process_yml_table
570
=head2 process_yml_table
530
571
531
  my $query_info   = $installer->process_yml_table($table);
572
  my $query_info   = $installer->process_yml_table($table);
Lines 592-611 sub load_sql { Link Here
592
            };
633
            };
593
        } else {                        # YAML files
634
        } else {                        # YAML files
594
            eval {
635
            eval {
595
                my $yaml = YAML::XS::LoadFile($filename);    # Load YAML
636
                my @statements = $self->get_sql_statements_from_yml($filename);
596
                for my $table ( @{ $yaml->{'tables'} } ) {
637
                for my $statement (@statements) {
597
                    my $query_info   = process_yml_table($table);
638
                    my $sql       = $statement->[0];
598
                    my $query        = $query_info->{query};
639
                    my $variables = $statement->[1];
599
                    my $placeholders = $query_info->{placeholders};
640
                    $dbh->do( $sql, undef, @$variables );
600
                    my $values       = $query_info->{values};
601
602
                    # Doing only 1 INSERT query for the whole table
603
                    my @all_rows_values = map { @$_ } @$values;
604
                    $query .= join ', ', ($placeholders) x scalar @$values;
605
                    $dbh->do( $query, undef, @all_rows_values );
606
                }
607
                for my $statement ( @{ $yaml->{'sql_statements'} } ) {    # extra SQL statements
608
                    $dbh->do($statement);
609
                }
641
                }
610
            };
642
            };
611
        }
643
        }
(-)a/misc/maintenance/load_installer_data.pl (-1 / +70 lines)
Line 0 Link Here
0
- 
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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Koha::Script;
21
use Getopt::Long;
22
use Try::Tiny;
23
use C4::Context;
24
use C4::Installer;
25
26
my ( $filepath, $help, $table, $lang, $all, $force );
27
GetOptions(
28
    'help|h'     => \$help,
29
    'filepath=s' => \$filepath,
30
    'table=s'    => \$table,
31
    'lang=s'     => \$lang,
32
    'force'      => \$force,
33
);
34
35
my $dbh       = C4::Context->dbh;
36
my $installer = C4::Installer->new;
37
38
my $table_mapping = {
39
    authorised_values    => 'auth_values',
40
    account_credit_types => 'account_credit_types',
41
    account_debit_types  => 'account_debit_types',
42
    letter               => 'sample_notices',
43
};
44
45
unless ($filepath) {
46
    die "table and lang are mandatory" unless $table || $lang;
47
48
    my $filename = $table;
49
    if ( exists $table_mapping->{$table} ) {
50
        $filename = $table_mapping->{$table};
51
    }
52
    $filepath = sprintf 'installer/data/mysql/%s/mandatory/%s.yml', $lang, $filename;
53
}
54
55
die "File does not exist ($filepath)" unless -f $filepath;
56
57
my $inserted       = 0;
58
my @sql_statements = $installer->get_sql_statements_from_yml( $filepath, 1 );
59
for my $statement (@sql_statements) {
60
    my $sql = $statement->[0];
61
    if ($force) {
62
        $sql =~ s/INSERT INTO/REPLACE INTO/;
63
    }
64
    my $variables = $statement->[1];
65
    try {
66
        $inserted += $dbh->do( $sql, undef, @$variables );
67
    } catch {
68
    };
69
}
70
say "$inserted rows inserted";

Return to bug 24712