From 3635e8f82660ced571af592088a369db698101a3 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 17 Oct 2016 17:50:50 +0100 Subject: [PATCH] Bug 10337: Add a script to populate devs' DBs with sample data Executing the installer process and inserting all the sample data take a lot of clics and time. The idea of this script is to provide a quick way to insert all the sample data easily to get a working Koha install asap. Test plan: - Set your database config to a non-existent DB - Execute perl misc/devel/populate_db.pl You will get an error - Create an empty DB - Execute perl misc/devel/populate_db.pl It will insert all the MARC21 sample data - Execute perl misc/devel/populate_db.pl You will get an error because the DB is not empty (systempreferences and borrowers tables) Signed-off-by: Chris Cormack Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall --- Koha/Database.pm | 4 ++ misc/devel/populate_db.pl | 123 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 misc/devel/populate_db.pl diff --git a/Koha/Database.pm b/Koha/Database.pm index a8f4eab9a8..6f72559b6b 100644 --- a/Koha/Database.pm +++ b/Koha/Database.pm @@ -90,6 +90,10 @@ sub _new_schema { my $dbh = $schema->storage->dbh; eval { $dbh->{RaiseError} = 1; + if ( $ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} ) { + $dbh->{RaiseError} = 0; + $dbh->{PrintError} = 0; + } $dbh->do(q| SELECT * FROM systempreferences WHERE 1 = 0 | ); diff --git a/misc/devel/populate_db.pl b/misc/devel/populate_db.pl new file mode 100644 index 0000000000..9289c14af3 --- /dev/null +++ b/misc/devel/populate_db.pl @@ -0,0 +1,123 @@ +#!/usr/bin/perl + +# This file is part of Koha. +# +# Copyright 2016 Koha Development Team +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use C4::Installer; +use C4::Context; +use t::lib::Mocks; + +$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 1; +my $dbh = C4::Context->dbh; # At the beginning to die if DB does not exist. + +my ( $prefs_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM systempreferences|); +my ( $patrons_count ) = $dbh->selectrow_array(q|SELECT COUNT(*) FROM borrowers|); +if ( $prefs_count or $patrons_count ) { + die "Database is not empty!"; +} +$dbh->disconnect; +$ENV{KOHA_DB_DO_NOT_RAISE_OR_PRINT_ERROR} = 0; + +our $root = C4::Context->config('intranetdir'); +our $data_dir = "$root/installer/data/mysql"; +our $installer = C4::Installer->new; +my $lang = 'en'; +my $koha_structure_file = "$data_dir/kohastructure.sql"; +my @sample_files_mandatory = ( + glob("$data_dir/mandatory/*.sql"), + "$data_dir/audio_alerts.sql", + "$data_dir/sysprefs.sql", + "$data_dir/userflags.sql", + "$data_dir/userpermissions.sql", +); +my @sample_lang_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/mandatory/*.sql" ); +my @sample_lang_files_optional = ( glob $root . "/installer/data/mysql/$lang/optional/*.sql" ); +my @marc21_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/marc21/*/*.sql" ); +my @unimarc_sample_files_mandatory = ( glob $root . "/installer/data/mysql/$lang/marcflavour/unimarc/*/*.sql" ); + +my $version = get_version(); + +initialize_data(); +update_database(); + +sub initialize_data { + say "Inserting koha db structure..."; + my $error = $installer->load_db_schema; + die $error if $error; + + for my $f (@sample_files_mandatory) { + execute_sqlfile($f); + } + + for my $f (@sample_lang_files_mandatory) { + execute_sqlfile($f); + } + + for my $f (@sample_lang_files_optional) { + execute_sqlfile($f); + } + + for my $f (@marc21_sample_files_mandatory) { + execute_sqlfile($f); + } + + # set marcflavour (MARC21) + my $dbh = C4::Context->dbh; + $dbh->do( + q{ + INSERT INTO `systempreferences` (variable,value,explanation,options,type) + VALUES ('marcflavour','MARC21','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice') + } + ); + + # set version + $dbh->do( + qq{ + INSERT INTO systempreferences(variable, value, options, explanation, type) + VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL) + } + ); +} + +sub execute_sqlfile { + my ($filepath) = @_; + say "Inserting $filepath..."; + my $error = $installer->load_sql($filepath); + die $error if $error; +} + +sub get_version { + do $root . '/kohaversion.pl'; + my $version = kohaversion(); + $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/; + return $version; +} + +sub update_database { + my $update_db_path = $root . '/installer/data/mysql/updatedatabase.pl'; + + my $file = `cat $update_db_path`; + $file =~ s/exit;//; + eval $file; + if ($@) { + die "updatedatabase.pl process failed: $@"; + } else { + say "updatedatabase.pl process succeeded."; + } +} -- 2.39.5