Koha/misc/devel/populate_db.pl
Jonathan Druart 3635e8f826 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 <chrisc@catalyst.net.nz>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
2016-10-21 14:08:06 +00:00

123 lines
3.8 KiB
Perl

#!/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 <http://www.gnu.org/licenses>.
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.";
}
}