Bugzilla – Attachment 101013 Details for
Bug 24883
Add `misc/load_yaml.pl` utility script to allow manual loading of yaml data files
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 24883: Move new sub from load_yaml.pl
Bug-24883-Move-new-sub-from-loadyamlpl.patch (text/plain), 3.49 KB, created by
Jonathan Druart
on 2020-03-19 12:24:48 UTC
(
hide
)
Description:
Bug 24883: Move new sub from load_yaml.pl
Filename:
MIME Type:
Creator:
Jonathan Druart
Created:
2020-03-19 12:24:48 UTC
Size:
3.49 KB
patch
obsolete
>From e6c85ebb6ca685717b01ca00ba3da26cfbc95fe2 Mon Sep 17 00:00:00 2001 >From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> >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. >Also add an option to convert the yml file to SQL queries > >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 | 64 +++++++++++++++++------------------------------ > 1 file changed, 23 insertions(+), 41 deletions(-) > >diff --git a/misc/load_yaml.pl b/misc/load_yaml.pl >index e670f56660..9879b5eb01 100755 >--- a/misc/load_yaml.pl >+++ b/misc/load_yaml.pl >@@ -19,70 +19,52 @@ > > 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, or convert it to SQL queries > > 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 >+ --convert Convert to SQL > > USAGE > } > > # Getting parameters >-my $file; >-my $help; >+my ( @files, $dump, $load, $convert, $help ); > > GetOptions( >- 'file|f=s' => \$file, >- 'help|h' => \$help >+ 'help|h' => \$help, >+ 'load' => \$load, >+ 'convert' => \$convert, >+ '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 ($convert or $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); >+if ( $convert ) { >+ for my $f ( @files ) { >+ say $installer->yml2sql($f); >+ } > } >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 24883
:
100831
|
101009
|
101010
|
101011
|
101012
|
101013
|
101292
|
101293
|
101294
|
101332
|
101333
|
101901
|
101902
|
101903
|
101904
|
101905
|
101950
|
101951
|
101952
|
101953
|
101954