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

(-)a/C4/Update/Database.pm (-1 lines)
Lines 51-57 our $dbh = C4::Context->dbh; Link Here
51
51
52
sub get_versions_path {
52
sub get_versions_path {
53
    return C4::Context->config('intranetdir') . '/installer/data/mysql/versions';
53
    return C4::Context->config('intranetdir') . '/installer/data/mysql/versions';
54
55
}
54
}
56
55
57
=head2 get_filepath
56
=head2 get_filepath
(-)a/t/db_dependent/UpdateDatabase.t (+101 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
use Modern::Perl;
4
use Test::More;
5
use Test::MockModule;
6
use C4::Context;
7
use Data::Dumper;
8
9
BEGIN {
10
    use_ok('C4::Update::Database');
11
}
12
13
clean_db();
14
15
my $already_applied_before = C4::Update::Database::list_versions_already_applied;
16
my $nb_applied_before = scalar @$already_applied_before;
17
my $nb_status_ok_before = grep {$_->{status}} @$already_applied_before;
18
19
my $updatedatabasemodule = new Test::MockModule('C4::Update::Database');
20
$updatedatabasemodule->mock('get_versions_path', sub {C4::Context->config('intranetdir') . '/t/db_dependent/data/update_database/versions'});
21
22
my $availables = C4::Update::Database::list_versions_available;
23
my $already_applied = C4::Update::Database::list_versions_already_applied;
24
25
my $report;
26
27
is ( scalar @$availables, 4, "There are 4 new available updates" );
28
29
$report = C4::Update::Database::execute_version( "3.99.01.001" );
30
is ( $report->{'3.99.01.001'}, 'OK', "There is no error for the 1st version" );
31
32
$already_applied = C4::Update::Database::list_versions_already_applied;
33
is ( scalar @$already_applied, $nb_applied_before + 1, "There is 1 already applied version" );
34
35
$report = C4::Update::Database::execute_version( "3.99.01.001" );
36
is ( $report->{"3.99.01.001"}{error}, 'ALREADY_EXISTS', "There is an 'already exist' error" );
37
is ( $report->{"3.99.01.001"}{old_version}, '3.99.01.001', "This version had already executed in version 3.99.01.001" );
38
39
40
my $queries = C4::Update::Database::get_queries( C4::Update::Database::get_filepath( "3.99.01.002" ) );
41
my $expected = {
42
    queries => [
43
        'CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL)'
44
    ],
45
    'comments' => [
46
        'This is a comment',
47
        'This is aanother comment'
48
    ]
49
};
50
is_deeply ( $queries, $expected, "The version 002 contains 1 query and 2 comments" );
51
52
$report = C4::Update::Database::execute_version( "3.99.01.002" );
53
is ( $report->{'3.99.01.002'}, 'OK', "There is no error for the 2nd version" );
54
55
$report = C4::Update::Database::execute_version( "3.99.01.003" );
56
$expected = {
57
    '3.99.01.003' => [
58
        q{Error : 1050 => Table 'UpdateDatabase_testFOO' already exists}
59
    ]
60
};
61
is_deeply ( $report, $expected, "There is an error for the 3rd version" );
62
63
$report = C4::Update::Database::execute_version( "3.99.01.004" );
64
is ( $report->{'3.99.01.004'}, 'OK', "There is no error for the 4th version" );
65
66
67
$already_applied = C4::Update::Database::list_versions_already_applied;
68
is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 3, "There are 3 new versions with a status OK" );
69
70
C4::Update::Database::mark_as_ok( "3.99.01.003" );
71
$already_applied = C4::Update::Database::list_versions_already_applied;
72
is ( grep( {$_->{status}} @$already_applied ), $nb_status_ok_before + 4, "There are 4 new versions with a status OK" );
73
74
75
clean_db();
76
77
sub clean_db {
78
    my $dbh = C4::Context->dbh;
79
    local $dbh->{PrintError} = 0;
80
    local $dbh->{RaiseError} = 0;
81
    $dbh->do( q{
82
        DELETE FROM updatedb_error WHERE version LIKE "3.99.01.%";
83
    });
84
    $dbh->do( q{
85
        DELETE FROM updatedb_query WHERE version LIKE "3.99.01.%";
86
    });
87
    $dbh->do( q{
88
        DELETE FROM updatedb_report WHERE version LIKE "3.99.01.%";
89
    });
90
    $dbh->do( q{
91
        DROP TABLE UpdateDatabase_testFOO;
92
    });
93
    $dbh->do( q{
94
        DROP TABLE UpdateDatabase_testFOOBIS;
95
    });
96
    $dbh->do( q{
97
        DELETE FROM systempreferences WHERE variable = "UpdateDatabase::testsyspref1" OR variable = "UpdateDatabase::testsyspref2"
98
    });
99
}
100
101
done_testing;
(-)a/t/db_dependent/data/update_database/versions/3.99.01.001.sql (+3 lines)
Line 0 Link Here
1
-- This is a comment
2
DELIMITER ;
3
CREATE TABLE UpdateDatabase_testFOO ( `version` varchar(32) DEFAULT NULL);
(-)a/t/db_dependent/data/update_database/versions/3.99.01.002.sql (+4 lines)
Line 0 Link Here
1
-- This is a comment
2
-- This is aanother comment
3
DELIMITER ;
4
CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL);
(-)a/t/db_dependent/data/update_database/versions/3.99.01.003.sql (+3 lines)
Line 0 Link Here
1
-- table FOO again
2
DELIMITER ;
3
CREATE TABLE UpdateDatabase_testFOO ( `version` varchar(32) DEFAULT NULL);
(-)a/t/db_dependent/data/update_database/versions/3.99.01.004.pl (-1 / +44 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# You write good Perl, so you start with Modern::Perl, of course
4
use Modern::Perl;
5
6
# then you load Packages that could be usefull
7
use C4::Context;
8
# Loading this package is usefull if you need to check if a table exist (TableExists)
9
use C4::Update::Database;
10
11
# you *must* have the sub _get_queries
12
# it returns an array of all SQL that have to be executed
13
# this array will be stored "forever" in your Koha database
14
# thus, you will be able to know which SQL has been executed
15
# at the time of upgrade. Very handy, because since then
16
# your database configuration may have changed and you'll wonder
17
# what has really be executed, not what would be executed today !
18
19
# put in an array the SQL to execute
20
# put in an array the comments
21
sub _get_queries {
22
    my @queries;
23
    my @comments;
24
    push @comments, "Add sample feature";
25
    unless ( C4::Update::Database::TableExists('testtable') ) {
26
        push @queries, qq{
27
                CREATE TABLE `UpdateDatabase_testtable` (
28
                  `id` int(11) NOT NULL AUTO_INCREMENT,
29
                  `source` text DEFAULT NULL,
30
                  `text` mediumtext NOT NULL,
31
                  `timestamp` datetime NOT NULL,
32
                  PRIMARY KEY (`id`)
33
                ) ENGINE=InnoDB DEFAULT CHARSET=utf8
34
            };
35
        push @comments, qq { * Added the table UpdateDatabase_testtable that did not exist};
36
    }
37
    push @queries, qq{INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('UpdateDatabase::testsyspref1',0,'Enable or disable display of Quote of the Day on the OPAC home page',NULL,'YesNo')};
38
    push @queries, qq{INSERT IGNORE INTO `systempreferences` (variable,value,explanation,options,type) VALUES('UpdateDatabase::testsyspref2',0,'Enable or disable display of Quote of the Day on the OPAC home page',NULL,'YesNo')};
39
    push @comments , qq{ * Added 2 sysprefs};
40
41
# return queries and comments
42
    return { queries => \@queries, comments => \@comments };
43
}
44
1;

Return to bug 7167