From 4ad96226725fd136a439c45e1a0e957d253dfbab Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 19 Mar 2020 13:09:33 +0100 Subject: [PATCH] Bug 24883: Move new sub from load_yaml.pl 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? --- misc/load_yaml.pl | 59 ++++++++++++++--------------------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/misc/load_yaml.pl b/misc/load_yaml.pl index e670f56660..b12d88d886 100755 --- a/misc/load_yaml.pl +++ b/misc/load_yaml.pl @@ -19,70 +19,45 @@ 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 < \$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); -} -- 2.20.1