Bug 24883: Move new sub from load_yaml.pl
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Thu, 19 Mar 2020 12:09:33 +0000 (13:09 +0100)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Fri, 27 Mar 2020 12:11:23 +0000 (12:11 +0000)
Use the sub from C4::Installer to avoid dup of code.

Note:
We are going to modify the script and so will do more stuffs.
We may want to rename it, maybe installer_utilities.pl,
misc/installer/yaml_utility.pl, any suggestions?

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

misc/load_yaml.pl

index e670f56..b12d88d 100755 (executable)
 
 use Modern::Perl;
 
-use YAML::Syck qw( LoadFile );
-use C4::Context;
 use Getopt::Long qw(:config no_ignore_case);
-use Data::Printer;
+use C4::Context;
+use C4::Installer;
 
 sub print_usage {
      ( my $basename = $0 ) =~ s|.*/||;
      print <<USAGE;
 
 $basename
- Load file in YAML format into database
+ Load file in YAML format into database.
 
 Usage:
 $0 [--file=FILE]
 $0 -h
-
- -f, --file=FILE         File to load.
  -h, --help              Show this help
+ -f, --file=FILE         File to load.
+ --load                  Load the file into the database
 
 USAGE
 }
 
 # Getting parameters
-my $file;
-my $help;
+my ( @files, $dump, $load, $help );
 
 GetOptions(
- 'file|f=s'     => \$file,
- 'help|h'       => \$help
+ 'help|h'        => \$help,
+ 'load'          => \$load,
+ 'file|f=s@'     => \@files,
 ) or print_usage, exit 1;
 
-if ($help or not $file) {
- print_usage;
- exit;
-}
-
-my $dbh  = C4::Context->dbh;
-my $yaml;
-eval {
-    $yaml = LoadFile( $file );                                    # Load YAML
-};
-if ($@){
-    die "Something went wrong loading file $file ($@)";
+if ($help or not @files or not $load) {
+    print_usage;
+    exit;
 }
 
-for my $table ( @{ $yaml->{'tables'} } ) {
-    my $table_name   = ( keys %$table )[0];                          # table name
-    my @rows         = @{ $table->{$table_name}->{rows} };           #
-    my @columns      = ( sort keys %{$rows[0]} );                    # column names
-    my $fields       = join ",", map{sprintf("`%s`", $_)} @columns;  # idem, joined
-    my $placeholders = join ",", map { "?" } @columns;               # '?,..,?' string
-    my $query        = "INSERT INTO $table_name ( $fields ) VALUES ( $placeholders )";
-    my $sth          = $dbh->prepare($query);
-    my @multiline    = @{ $table->{$table_name}->{'multiline'} };    # to check multiline values;
-    foreach my $row ( @rows ) {
-        my @values = map {
-                        my $col = $_;
-                        ( @multiline and grep { $_ eq $col } @multiline )
-                        ? join "\r\n", @{$row->{$col}}                # join multiline values
-                        : $row->{$col};
-                     } @columns;
-        $sth->execute( @values );
+my $installer = C4::Installer->new;
+if ( $load ) {
+    for my $f ( @files ) {
+        my $error = $installer->load_sql($f);
+        say $error if $error;
     }
 }
-for my $statement ( @{ $yaml->{'sql_statements'} } ) {               # extra SQL statements
-    $dbh->do($statement);
-}