View | Details | Raw Unified | Return to bug 10337
Collapse All | Expand All

(-)a/t/db_dependent/000-init_db.t (-1 / +110 lines)
Line 0 Link Here
0
- 
1
use Modern::Perl;
2
use Test::More tests => 34;
3
4
use C4::Context;
5
use t::lib::Mocks;
6
7
t::lib::Mocks::mock_config('database', 'koha_ut');
8
9
10
my $koha_structure_file = C4::Context->config('intranetdir') . '/installer/data/mysql/kohastructure.sql';
11
my $sample_files_optional = C4::Context->config('intranetdir') . '/installer/data/mysql/en/optional/*.sql';
12
my $sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/mandatory/*.sql';
13
my $marc21_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/marc21/*/*.sql';
14
my $unimarc_sample_files_mandatory = C4::Context->config('intranetdir') . '/installer/data/mysql/en/marcflavour/unimarc/mandatory/*.sql';
15
my $sample_only_param_tables = C4::Context->config('intranetdir') . '/installer/data/mysql/sample_only_param_tables.sql';
16
my $sysprefs = C4::Context->config('intranetdir') . '/installer/data/mysql/sysprefs.sql';
17
18
my $version = get_version();
19
20
our $dbh = C4::Context->dbh;
21
our $user = C4::Context->config('user');
22
our $pass = C4::Context->config('pass');
23
our $dbname = C4::Context->config("database");
24
25
die q{The SQL database name should be 'koha_ut'}
26
    if $dbname ne 'koha_ut';
27
28
recreate_db( $dbname );
29
# Force C4::Context to recreate a new db handler
30
$dbh->disconnect;
31
$dbh = C4::Context->dbh;
32
initialize_data();
33
update_database();
34
35
36
# FIXME We can't insert unimarc AND marc21 files
37
#for my $f ( glob $unimarc_sample_files_mandatory ) {
38
#    execute_sqlfile( $f, "inserting $f");
39
#}
40
41
42
# FIXME This file crashes
43
#execute_sqlfile( $sample_only_param_tables, "inserting $sample_only_param_tables" );
44
45
sub recreate_db {
46
    my $dbname = shift;
47
    ok( $dbh->do(qq{
48
        DROP DATABASE $dbname
49
    }), "drop database $dbname" );
50
51
    is( $dbh->do(qq{
52
        CREATE DATABASE $dbname CHARACTER SET utf8 COLLATE utf8_bin
53
    }), 1, "create database $dbname" );
54
}
55
56
sub initialize_data {
57
    execute_sqlfile( $koha_structure_file, "inserting koha db structure" );
58
59
    for my $f ( glob $sample_files_mandatory ) {
60
        execute_sqlfile( $f, "inserting $f");
61
    }
62
63
64
    for my $f ( glob $sample_files_optional ) {
65
        execute_sqlfile( $f, "inserting $f");
66
    }
67
68
    for my $f ( glob $marc21_sample_files_mandatory ) {
69
        execute_sqlfile( $f, "inserting $f");
70
    }
71
    execute_sqlfile( $sysprefs, "inserting $sysprefs");
72
73
    # set marcflavour (MARC21)
74
    $dbh->do( q{
75
        INSERT INTO `systempreferences` (variable,value,explanation,options,type)
76
        VALUES ('marcflavour','MARC21','Define global MARC flavor (MARC21 or UNIMARC) used for character encoding','MARC21|UNIMARC','Choice')
77
    } );
78
79
    # set version
80
    $dbh->do( qq{
81
        INSERT INTO systempreferences(variable, value, options, explanation, type)
82
        VALUES ('Version', '$version', NULL, 'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller', NULL)
83
    } );
84
}
85
86
sub execute_sqlfile {
87
    my ( $filepath, $msg ) = @_;
88
    is( system( qq{/usr/bin/mysql -u $user -p'$pass' -D $dbname < $filepath} ), 0, $msg );
89
}
90
91
sub get_version {
92
    do C4::Context->config('intranetdir') . '/kohaversion.pl';
93
    my $version = kohaversion();
94
    $version =~ s/(\d)\.(\d{2})\.(\d{2})\.(\d{3})/$1.$2$3$4/;
95
    return $version;
96
}
97
98
sub update_database {
99
    my $src_path = C4::Context->config('intranetdir');
100
    my $update_db_path = $src_path . '/installer/data/mysql/updatedatabase.pl';
101
102
    my $file = `cat $update_db_path`;
103
    $file =~ s/exit;//;
104
    eval $file;
105
    if ($@) {
106
        fail("updatedatabase.pl process failed: $@");
107
    } else {
108
        pass("updatedatabase.pl process succeeded.");
109
    }
110
}

Return to bug 10337