Bug 10337: Add a script to populate devs' DBs with sample data
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 17 Oct 2016 16:50:50 +0000 (17:50 +0100)
committerKyle M Hall <kyle@bywatersolutions.com>
Fri, 21 Oct 2016 14:08:06 +0000 (14:08 +0000)
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>
Koha/Database.pm
misc/devel/populate_db.pl [new file with mode: 0644]

index a8f4eab..6f72559 100644 (file)
@@ -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 (file)
index 0000000..9289c14
--- /dev/null
@@ -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 <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.";
+    }
+}