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

(-)a/C4/Update/Database.pm (-3 / +13 lines)
Lines 48-54 my $list; Link Here
48
48
49
my $dbh = C4::Context->dbh;
49
my $dbh = C4::Context->dbh;
50
50
51
=head2
51
=head2 get_versions_path
52
53
    return the path to the version files
54
55
=cut
56
57
sub get_versions_path {
58
    return $VERSIONS_PATH;
59
}
60
61
=head2 get_filepath
52
62
53
  my $file = get_filepath($version);
63
  my $file = get_filepath($version);
54
  this sub will return the full path of a given DB update number
64
  this sub will return the full path of a given DB update number
Lines 57-63 my $dbh = C4::Context->dbh; Link Here
57
67
58
sub get_filepath {
68
sub get_filepath {
59
    my ( $version ) = @_;
69
    my ( $version ) = @_;
60
    my @files = File::Find::Rule->file->name( "$version.sql", "$version.pl" ) ->in( ( $VERSIONS_PATH ) );
70
    my @files = File::Find::Rule->file->name( "$version.sql", "$version.pl" ) ->in( ( get_versions_path() ) );
61
71
62
    if ( scalar @files != 1 ) {
72
    if ( scalar @files != 1 ) {
63
        die "This version ($version) returned has ".scalar @files." corresponding, need only 1";
73
        die "This version ($version) returned has ".scalar @files." corresponding, need only 1";
Lines 168-174 sub execute_version { Link Here
168
sub list_versions_available {
178
sub list_versions_available {
169
    my @versions;
179
    my @versions;
170
180
171
    my @files = File::Find::Rule->file->name( "*.sql", "*.pl" ) ->in( ( $VERSIONS_PATH ) );
181
    my @files = File::Find::Rule->file->name( "*.sql", "*.pl" ) ->in( ( get_versions_path() ) );
172
182
173
    for my $f ( @files ) {
183
    for my $f ( @files ) {
174
        my @file_infos = fileparse( $f, qr/\.[^.]*/ );
184
        my @file_infos = fileparse( $f, qr/\.[^.]*/ );
(-)a/t/db_dependent/UpdateDatabase.t (+99 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 $updatedatabasemodule = new Test::MockModule('C4::Update::Database');
16
$updatedatabasemodule->mock('get_versions_path', sub {C4::Context->config('intranetdir') . '/t/db_dependent/data/update_database/versions'});
17
18
my $availables = C4::Update::Database::list_versions_available;
19
my $already_applied = C4::Update::Database::list_versions_already_applied;
20
21
my $report;
22
23
is ( C4::Update::Database::is_uptodate(), 0, "The database is not up-to-date" );
24
25
is ( scalar @$availables, 4, "There are 4 available updates" );
26
27
$report = C4::Update::Database::execute_version( "3.99.01.001" );
28
is ( $report->{'3.99.01.001'}, 'OK', "There is no error for the 1st version" );
29
30
$already_applied = C4::Update::Database::list_versions_already_applied;
31
is ( scalar @$already_applied, 1, "There is 1 already applied version" );
32
33
$report = C4::Update::Database::execute_version( "3.99.01.001" );
34
is ( $report->{"3.99.01.001"}{error}, 'ALREADY_EXISTS', "There is an 'already exist' error" );
35
is ( $report->{"3.99.01.001"}{old_version}, '3.99.01.001', "This version had already executed in version 3.99.01.001" );
36
37
38
my $queries = C4::Update::Database::get_queries( C4::Update::Database::get_filepath( "3.99.01.002" ) );
39
my $expected = {
40
    queries => [
41
        'CREATE TABLE UpdateDatabase_testFOOBIS ( `version` varchar(32) DEFAULT NULL)'
42
    ],
43
    'comments' => [
44
        'This is a comment',
45
        'This is aanother comment'
46
    ]
47
};
48
is_deeply ( $queries, $expected, "The version 002 contains 1 query and 2 comments" );
49
50
$report = C4::Update::Database::execute_version( "3.99.01.002" );
51
is ( $report->{'3.99.01.002'}, 'OK', "There is no error for the 2nd version" );
52
53
$report = C4::Update::Database::execute_version( "3.99.01.003" );
54
$expected = {
55
    '3.99.01.003' => [
56
        q{Error : 1050 => Table 'UpdateDatabase_testFOO' already exists}
57
    ]
58
};
59
is_deeply ( $report, $expected, "There is an error for the 3rd version" );
60
61
$report = C4::Update::Database::execute_version( "3.99.01.004" );
62
is ( $report->{'3.99.01.004'}, 'OK', "There is no error for the 4th version" );
63
64
65
$already_applied = C4::Update::Database::list_versions_already_applied;
66
is ( grep( {$_->{status}} @$already_applied ), 3, "There are 3 versions with a status OK" );
67
68
C4::Update::Database::mark_as_ok( "3.99.01.003" );
69
$already_applied = C4::Update::Database::list_versions_already_applied;
70
is ( grep( {$_->{status}} @$already_applied ), 4, "There are 4 versions with a status OK" );
71
72
73
clean_db();
74
75
sub clean_db {
76
    my $dbh = C4::Context->dbh;
77
    local $dbh->{PrintError} = 0;
78
    local $dbh->{RaiseError} = 0;
79
    $dbh->do( q{
80
        DELETE FROM updatedb_error;
81
    });
82
    $dbh->do( q{
83
        DELETE FROM updatedb_query;
84
    });
85
    $dbh->do( q{
86
        DELETE FROM updatedb_report;
87
    });
88
    $dbh->do( q{
89
        DROP TABLE UpdateDatabase_testFOO;
90
    });
91
    $dbh->do( q{
92
        DROP TABLE UpdateDatabase_testFOOBIS;
93
    });
94
    $dbh->do( q{
95
        DELETE FROM systempreferences WHERE variable = "UpdateDatabase::testsyspref1" OR variable = "UpdateDatabase::testsyspref2"
96
    });
97
}
98
99
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