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

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

Return to bug 10337