Bugzilla – Attachment 117835 Details for
Bug 27880
Store each database migrations state in database
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 27880: Database migrations in separate files
Bug-27880-Database-migrations-in-separate-files.patch (text/plain), 33.59 KB, created by
Julian Maurice
on 2021-03-05 12:02:18 UTC
(
hide
)
Description:
Bug 27880: Database migrations in separate files
Filename:
MIME Type:
Creator:
Julian Maurice
Created:
2021-03-05 12:02:18 UTC
Size:
33.59 KB
patch
obsolete
>From bfd91b9cd7cca599a3ac51d6aca7b49cfefb0919 Mon Sep 17 00:00:00 2001 >From: Julian Maurice <julian.maurice@biblibre.com> >Date: Mon, 5 Oct 2020 10:03:14 +0200 >Subject: [PATCH] Bug 27880: Database migrations in separate files > >The goals of this patch are: >- stop the growth of updatedatabase.pl file (which is nearly 24k lines >long at the moment) >- reduce the manual work for RM/RMaints (copy/paste of code from >atomicupdate to updatedatabase.pl + increase of Koha version) >- provide a simple framework for a better control of the database update >process > >Database migrations are regular Perl scripts that return migration >metadata and code in a hashref. >Their filename is meaningful. It should always start with a timestamp >(in the form YYYYMMDDHHMMSS), which is used for executing migrations in >the correct order. It should also contain a short description. > >Once a migration has been successfully executed, its name is stored into >a new table `executed_migrations`. This is how we avoid executing a >migration twice. > >With this new system, the 'Version' system preference becomes useless, >as well as the 4th part of the Koha version. A good part of the patch is >about adapting the code to these changes. > >Test plan: >1. Apply patch and restart starman >2. Go to the staff interface, you should be redirected to the installer >3. Enter your credentials, click on "Continue to the next step" until > you arrive at the "Update database" step >4. You should now see the following message: > Pending migrations: > > 20201002135449-create-executed-migrations-table.pl > Click on "Update your database" > >5. Now you should see > Update report : > > Migration done: > 20201002135449-create-executed-migrations-table.pl (Create table executed_migrations) > > Everything went okay. Update done. > >6. Log into Koha >7. Verify that the About page shows the correct version >8. Verify that the 'Help' link in the top right corner redirects to the > correct version of the manual > >9. Now, do a fresh install and verify that the executed_migrations table > exists and is not empty at the end of the install process >--- > C4/Auth.pm | 35 +++-------- > C4/Context.pm | 16 ++++- > C4/Installer.pm | 28 --------- > Koha.pm | 13 ++-- > Koha/Manual.pm | 3 +- > Koha/Migrations.pm | 44 ++++++++++++++ > Koha/SharedContent.pm | 5 +- > Koha/Template/Plugin/Asset.pm | 2 +- > Koha/Template/Plugin/Koha.pm | 3 +- > .../data/mysql/atomicupdate/skeleton.perl | 13 ---- > installer/data/mysql/kohastructure.sql | 11 ++++ > ...135449-create-executed-migrations-table.pl | 15 +++++ > installer/data/mysql/updatedatabase.pl | 55 +++++++++-------- > installer/install.pl | 37 ++++-------- > installer/onboarding.pl | 2 +- > .../prog/en/modules/installer/step3.tt | 9 ++- > opac/maintenance.pl | 12 ++-- > plugins/plugins-home.pl | 6 +- > t/00-checkdatabase-version.t | 60 ------------------- > t/Context.t | 11 ---- > t/Koha_Template_Plugin_Koha.t | 8 +-- > t/db_dependent/Context.t | 13 ---- > .../Koha/REST/Plugin/PluginRoutes.t | 6 +- > 23 files changed, 163 insertions(+), 244 deletions(-) > create mode 100644 Koha/Migrations.pm > delete mode 100644 installer/data/mysql/atomicupdate/skeleton.perl > create mode 100644 installer/data/mysql/migrations/20201002135449-create-executed-migrations-table.pl > delete mode 100755 t/00-checkdatabase-version.t > >diff --git a/C4/Auth.pm b/C4/Auth.pm >index 8368dd82ff..1a0d0ceb8c 100644 >--- a/C4/Auth.pm >+++ b/C4/Auth.pm >@@ -31,6 +31,7 @@ use C4::Context; > use C4::Templates; # to get the template > use C4::Languages; > use C4::Search::History; >+use C4::Installer; > use Koha; > use Koha::Caches; > use Koha::AuthUtils qw(get_script_name hash_password); >@@ -48,6 +49,7 @@ use Encode qw( encode is_utf8); > use C4::Auth_with_shibboleth; > use Net::CIDR; > use C4::Log qw/logaction/; >+use Koha::Migrations; > > # use utf8; > use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug $ldap $cas $caslogout); >@@ -597,7 +599,6 @@ sub get_template_and_user { > OPACFinesTab => C4::Context->preference("OPACFinesTab"), > OpacTopissue => C4::Context->preference("OpacTopissue"), > RequestOnOpac => C4::Context->preference("RequestOnOpac"), >- 'Version' => C4::Context->preference('Version'), > hidelostitems => C4::Context->preference("hidelostitems"), > mylibraryfirst => ( C4::Context->preference("SearchMyLibraryFirst") && C4::Context->userenv ) ? C4::Context->userenv->{'branch'} : '', > opacbookbag => "" . C4::Context->preference("opacbookbag"), >@@ -733,7 +734,6 @@ has authenticated. > sub _version_check { > my $type = shift; > my $query = shift; >- my $version; > > # If version syspref is unavailable, it means Koha is being installed, > # and so we must redirect to OPAC maintenance page or to the WebInstaller >@@ -743,7 +743,7 @@ sub _version_check { > print $query->redirect("/cgi-bin/koha/maintenance.pl"); > safe_exit; > } >- unless ( $version = C4::Context->preference('Version') ) { # assignment, not comparison >+ unless ( C4::Installer::TableExists('systempreferences') ) { > if ( $type ne 'opac' ) { > warn "Install required, redirecting to Installer"; > print $query->redirect("/cgi-bin/koha/installer/install.pl"); >@@ -758,13 +758,8 @@ sub _version_check { > # there is no DB version, it's a fresh install, > # go to web installer > # there is a DB version, compare it to the code version >- my $kohaversion = Koha::version(); >- >- # remove the 3 last . to have a Perl number >- $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >- $debug and print STDERR "kohaversion : $kohaversion\n"; >- if ( $version < $kohaversion ) { >- my $warning = "Database update needed, redirecting to %s. Database is $version and Koha is $kohaversion"; >+ if (Koha::Migrations->pending_migrations > 0) { >+ my $warning = "Database update needed, redirecting to %s"; > if ( $type ne 'opac' ) { > warn sprintf( $warning, 'Installer' ); > print $query->redirect("/cgi-bin/koha/installer/install.pl?step=1&op=updatestructure"); >@@ -1489,15 +1484,7 @@ sub check_api_auth { > my $dbh = C4::Context->dbh; > my $timeout = _timeout_syspref(); > >- unless ( C4::Context->preference('Version') ) { >- >- # database has not been installed yet >- return ( "maintenance", undef, undef ); >- } >- my $kohaversion = Koha::version(); >- $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >- if ( C4::Context->preference('Version') < $kohaversion ) { >- >+ if ( Koha::Migrations->pending_migrations > 0 ) { > # database in need of version update; assume that > # no API should be called while databsae is in > # this condition. >@@ -1744,15 +1731,7 @@ sub check_cookie_auth { > my $dbh = C4::Context->dbh; > my $timeout = _timeout_syspref(); > >- unless ( C4::Context->preference('Version') ) { >- >- # database has not been installed yet >- return ( "maintenance", undef ); >- } >- my $kohaversion = Koha::version(); >- $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >- if ( C4::Context->preference('Version') < $kohaversion ) { >- >+ if ( Koha::Migrations->pending_migrations > 0 ) { > # database in need of version update; assume that > # no API should be called while databsae is in > # this condition. >diff --git a/C4/Context.pm b/C4/Context.pm >index 06bdf08e44..615ba760e4 100644 >--- a/C4/Context.pm >+++ b/C4/Context.pm >@@ -55,7 +55,6 @@ BEGIN { > <table> > <tr><th>Apache</th><td> $versions{apacheVersion}</td></tr> > <tr><th>Koha</th><td> $versions{kohaVersion}</td></tr> >- <tr><th>Koha DB</th><td> $versions{kohaDbVersion}</td></tr> > <tr><th>MySQL</th><td> $versions{mysqlVersion}</td></tr> > <tr><th>OS</th><td> $versions{osVersion}</td></tr> > <tr><th>Perl</th><td> $versions{perlVersion}</td></tr> >@@ -410,6 +409,12 @@ sub preference { > > $var = lc $var; > >+ if ($var eq 'version') { >+ my $version = Koha::version(); >+ $version =~ s/^(\d+)\.(\d+)\.(\d+)$/$1.$2$3/; >+ return $version; >+ } >+ > if ($use_syspref_cache) { > $syspref_cache = Koha::Caches->get_instance('syspref') unless $syspref_cache; > my $cached_var = $syspref_cache->get_from_cache("syspref_$var"); >@@ -907,7 +912,6 @@ Gets various version info, for core Koha packages, Currently called from carp ha > sub get_versions { > my %versions; > $versions{kohaVersion} = Koha::version(); >- $versions{kohaDbVersion} = C4::Context->preference('version'); > $versions{osVersion} = join(" ", POSIX::uname()); > $versions{perlVersion} = $]; > { >@@ -1103,7 +1107,13 @@ This method returns a boolean representing the install status of the Koha instan > > sub needs_install { > my ($self) = @_; >- return ($self->preference('Version')) ? 0 : 1; >+ eval { >+ my $dbh = $self->dbh; >+ local $dbh->{PrintError} = 0; >+ local $dbh->{RaiseError} = 1; >+ $dbh->do('SELECT * FROM systempreferences WHERE 1 = 0'); >+ }; >+ return $@ ? 1 : 0; > } > > __END__ >diff --git a/C4/Installer.pm b/C4/Installer.pm >index 05b8025dbe..bd536d998a 100644 >--- a/C4/Installer.pm >+++ b/C4/Installer.pm >@@ -46,7 +46,6 @@ C4::Installer > my $list; > #fill $list with list of sql files > my ($fwk_language, $error_list) = $installer->load_sql_in_order($all_languages, @$list); >- $installer->set_version_syspref(); > $installer->set_marcflavour_syspref('MARC21'); > > =head1 DESCRIPTION >@@ -443,33 +442,6 @@ sub set_marcflavour_syspref { > $request->execute; > } > >-=head2 set_version_syspref >- >- $installer->set_version_syspref(); >- >-Set or update the 'Version' system preference to the current >-Koha software version. >- >-=cut >- >-sub set_version_syspref { >- my $self = shift; >- >- my $kohaversion = Koha::version(); >- # remove the 3 last . to have a Perl number >- $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >- if (C4::Context->preference('Version')) { >- warn "UPDATE Version"; >- my $finish=$self->{'dbh'}->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'"); >- $finish->execute($kohaversion); >- } else { >- warn "INSERT Version"; >- my $finish=$self->{'dbh'}->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')"); >- $finish->execute($kohaversion); >- } >- C4::Context->clear_syspref_cache(); >-} >- > =head2 set_languages_syspref > > $installer->set_languages_syspref(); >diff --git a/Koha.pm b/Koha.pm >index 01f1db319c..6923b2577e 100644 >--- a/Koha.pm >+++ b/Koha.pm >@@ -22,14 +22,11 @@ use Modern::Perl; > > use vars qw{ $VERSION }; > >-#the kohaversion is divided in 4 parts : >-# - #1 : the major number. 3 atm >-# - #2 : the functional release. 00 atm >-# - #3 : the subnumber, moves only on a public release >-# - #4 : the developer version. The 4th number is the database subversion. >-# used by developers when the database changes. updatedatabase take care of the changes itself >-# and is automatically called by Auth.pm when needed. >-$VERSION = "20.12.00.021"; >+#the kohaversion is divided in 3 parts : >+# - #1 : the year of release >+# - #2 : the month of release (05 or 11 for stable releases, 06 or 12 for development branch) >+# - #3 : the maintenance release number >+$VERSION = "20.12.00"; > > sub version { > return $VERSION; >diff --git a/Koha/Manual.pm b/Koha/Manual.pm >index a69c079806..1abeca1d14 100644 >--- a/Koha/Manual.pm >+++ b/Koha/Manual.pm >@@ -3,9 +3,10 @@ package Koha::Manual; > use Modern::Perl; > > use C4::Context; >+use Koha; > > sub _get_help_version { >- my $help_version = C4::Context->preference("Version"); >+ my $help_version = Koha::version(); > if ( $help_version =~ m|^(\d+)\.(\d{2}).*$| ) { > my $version = $1; > my $major = $2; >diff --git a/Koha/Migrations.pm b/Koha/Migrations.pm >new file mode 100644 >index 0000000000..a4a6782d52 >--- /dev/null >+++ b/Koha/Migrations.pm >@@ -0,0 +1,44 @@ >+package Koha::Migrations; >+ >+use Modern::Perl; >+ >+use C4::Context; >+ >+sub pending_migrations { >+ my ($self) = @_; >+ >+ my $dbh = C4::Context->dbh; >+ >+ my @pending_migrations; >+ my $executed_migrations = {}; >+ eval { >+ $executed_migrations = $dbh->selectall_hashref('SELECT * FROM executed_migrations', 'name'); >+ }; >+ >+ opendir(my $dh, $self->migrations_dir); >+ foreach my $file (sort readdir $dh) { >+ next if $file !~ /\.pl$/; >+ next if (exists ($executed_migrations->{$file})); >+ push @pending_migrations, $file; >+ } >+ closedir($dh); >+ >+ return @pending_migrations; >+} >+ >+# Only needed for installer >+sub mark_all_migrations_as_executed { >+ my ($self) = @_; >+ >+ my $dbh = C4::Context->dbh; >+ >+ foreach my $migration ($self->pending_migrations) { >+ $dbh->do('INSERT INTO executed_migrations (name) VALUES (?)', {}, $migration); >+ } >+} >+ >+sub migrations_dir { >+ return C4::Context->config('intranetdir') . '/installer/data/mysql/migrations'; >+} >+ >+1; >diff --git a/Koha/SharedContent.pm b/Koha/SharedContent.pm >index 7082fe03a3..823d550ba0 100644 >--- a/Koha/SharedContent.pm >+++ b/Koha/SharedContent.pm >@@ -25,6 +25,7 @@ use LWP::UserAgent; > use Koha::Serials; > use Koha::Reports; > use C4::Context; >+use Koha; > > =head1 NAME > >@@ -144,11 +145,9 @@ sub prepare_entity_data { > $mana_email = C4::Context->preference('KohaAdminEmailAddress') > if ( ( not defined($mana_email) ) or ( $mana_email eq '' ) ); > >- my %versions = C4::Context::get_versions(); >- > my $mana_info = { > language => $lang, >- kohaversion => $versions{'kohaVersion'}, >+ kohaversion => Koha::version(), > exportemail => $mana_email > }; > >diff --git a/Koha/Template/Plugin/Asset.pm b/Koha/Template/Plugin/Asset.pm >index 08748b46e3..81814d0235 100644 >--- a/Koha/Template/Plugin/Asset.pm >+++ b/Koha/Template/Plugin/Asset.pm >@@ -142,7 +142,7 @@ sub url { > ); > > my $version = Koha::version; >- $version =~ s/([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)/$1.$2$3$4/; >+ $version =~ s/([0-9]+)\.([0-9]+)\.([0-9]+)/$1.$2${3}000/; > foreach my $dir (@dirs) { > my $abspath = File::Spec->catfile($root, $dir, $filename); > if (-e $abspath) { >diff --git a/Koha/Template/Plugin/Koha.pm b/Koha/Template/Plugin/Koha.pm >index 5ed58a95ad..9c94c3bdc7 100644 >--- a/Koha/Template/Plugin/Koha.pm >+++ b/Koha/Template/Plugin/Koha.pm >@@ -57,14 +57,13 @@ sub Preference { > > sub Version { > my $version_string = Koha::version(); >- my ( $major, $minor, $maintenance, $development ) = split( '\.', $version_string ); >+ my ( $major, $minor, $maintenance ) = split( '\.', $version_string ); > > return { > major => $major, > minor => $minor, > release => $major . "." . $minor, > maintenance => $major . "." . $minor . "." . $maintenance, >- development => ( $development ne '000' ) ? $development : undef, > }; > } > >diff --git a/installer/data/mysql/atomicupdate/skeleton.perl b/installer/data/mysql/atomicupdate/skeleton.perl >deleted file mode 100644 >index 813d7cbc5a..0000000000 >--- a/installer/data/mysql/atomicupdate/skeleton.perl >+++ /dev/null >@@ -1,13 +0,0 @@ >-$DBversion = 'XXX'; # will be replaced by the RM >-if( CheckVersion( $DBversion ) ) { >- # you can use $dbh here like: >- # $dbh->do( "ALTER TABLE biblio ADD COLUMN badtaste int" ); >- >- # or perform some test and warn >- # if( !column_exists( 'biblio', 'biblionumber' ) ) { >- # warn "There is something wrong"; >- # } >- >- # Always end with this (adjust the bug info) >- NewVersion( $DBversion, XXXXX, "Description"); >-} >diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql >index ae247e67c8..66c6b3d3f5 100644 >--- a/installer/data/mysql/kohastructure.sql >+++ b/installer/data/mysql/kohastructure.sql >@@ -2585,6 +2585,17 @@ CREATE TABLE `edifact_messages` ( > ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; > /*!40101 SET character_set_client = @saved_cs_client */; > >+-- >+-- Structure for table executed_migrations >+-- >+ >+DROP TABLE IF EXISTS `executed_migrations`; >+CREATE TABLE `executed_migrations` ( >+ `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, >+ `executed_at` timestamp NOT NULL DEFAULT current_timestamp(), >+ PRIMARY KEY (`name`) >+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; >+ > -- > -- Table structure for table `export_format` > -- >diff --git a/installer/data/mysql/migrations/20201002135449-create-executed-migrations-table.pl b/installer/data/mysql/migrations/20201002135449-create-executed-migrations-table.pl >new file mode 100644 >index 0000000000..1f896e8bfc >--- /dev/null >+++ b/installer/data/mysql/migrations/20201002135449-create-executed-migrations-table.pl >@@ -0,0 +1,15 @@ >+use Modern::Perl; >+ >+{ >+ description => 'Create table executed_migrations', >+ up => sub { >+ my ($dbh) = @_; >+ $dbh->do(q{ >+ CREATE TABLE IF NOT EXISTS `executed_migrations` ( >+ `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, >+ `executed_at` timestamp NOT NULL DEFAULT current_timestamp(), >+ PRIMARY KEY (`name`) >+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci >+ }); >+ }, >+} >diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl >index 5f4e6e1e13..9afee3f613 100755 >--- a/installer/data/mysql/updatedatabase.pl >+++ b/installer/data/mysql/updatedatabase.pl >@@ -40,6 +40,7 @@ use C4::Installer; > use Koha::Database; > use Koha; > use Koha::DateUtils; >+use Koha::Migrations; > > use MARC::Record; > use MARC::File::XML ( BinaryEncoding => 'utf8' ); >@@ -23634,21 +23635,29 @@ if( CheckVersion( $DBversion ) ) { > NewVersion( $DBversion, 22824, "Update syspref values for YesNo"); > } > >-# SEE bug 13068 >-# if there is anything in the atomicupdate, read and execute it. >-my $update_dir = C4::Context->config('intranetdir') . '/installer/data/mysql/atomicupdate/'; >-opendir( my $dirh, $update_dir ); >-foreach my $file ( sort readdir $dirh ) { >- next if $file !~ /\.(sql|perl)$/; #skip other files >- next if $file eq 'skeleton.perl'; # skip the skeleton file >- print "DEV atomic update: $file\n"; >- if ( $file =~ /\.sql$/ ) { >- my $installer = C4::Installer->new(); >- my $rv = $installer->load_sql( $update_dir . $file ) ? 0 : 1; >- } elsif ( $file =~ /\.perl$/ ) { >- my $code = read_file( $update_dir . $file ); >- eval $code; ## no critic (StringyEval) >- say "Atomic update generated errors: $@" if $@; >+my $insert_migration_sth = $dbh->prepare('INSERT INTO executed_migrations (name) VALUES (?)'); >+ >+my $migrations_dir = Koha::Migrations->migrations_dir; >+foreach my $file (Koha::Migrations->pending_migrations) { >+ my $migration = do $migrations_dir . '/' . $file; >+ unless ($migration) { >+ if ($@) { >+ warn "Could not compile $file: $@"; >+ } elsif ($!) { >+ warn "Could not do $file: $!"; >+ } else { >+ warn "Could not run $file"; >+ } >+ next; >+ } >+ >+ eval { >+ $migration->{up}->($dbh); >+ $insert_migration_sth->execute($file); >+ say sprintf('Migration done: %s (%s)', $file, $migration->{description}); >+ }; >+ if ($@) { >+ die sprintf('Migration failed %s (%s): %s', $file, $migration->{description}, $@); > } > } > >@@ -23691,8 +23700,9 @@ to a number, with just 1 . > > sub TransformToNum { > my $version = shift; >- # remove the 3 last . to have a Perl number >- $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >+ # keep only the first dot to have a Perl number >+ my ($major, @rest) = split /\./, $version; >+ $version = join('.', $major, join('', @rest)); > # three X's at the end indicate that you are testing patch with dbrev > # change it into 999 > # prevents error on a < comparison between strings (should be: lt) >@@ -23707,17 +23717,6 @@ set the DBversion in the systempreferences > =cut > > sub SetVersion { >- return if $_[0]=~ /XXX$/; >- #you are testing a patch with a db revision; do not change version >- my $kohaversion = TransformToNum($_[0]); >- if (C4::Context->preference('Version')) { >- my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'"); >- $finish->execute($kohaversion); >- } else { >- my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')"); >- $finish->execute($kohaversion); >- } >- C4::Context::clear_syspref_cache(); # invalidate cached preferences > } > > sub NewVersion { >diff --git a/installer/install.pl b/installer/install.pl >index be6ee27f8c..37a59affe1 100755 >--- a/installer/install.pl >+++ b/installer/install.pl >@@ -32,6 +32,7 @@ use C4::Installer; > use C4::Installer::PerlModules; > > use Koha; >+use Koha::Migrations; > > my $query = CGI->new; > my $step = $query->param('step'); >@@ -228,7 +229,7 @@ elsif ( $step && $step == 3 ) { > exit; > } > elsif ( $op && $op eq 'finish' ) { >- $installer->set_version_syspref(); >+ Koha::Migrations->mark_all_migrations_as_executed(); > > my $langchoice = $query->param('fwklanguage'); > $langchoice = $query->cookie('KohaOpacLanguage') unless ($langchoice); >@@ -384,11 +385,10 @@ elsif ( $step && $step == 3 ) { > > my $now = POSIX::strftime( "%Y-%m-%dT%H:%M:%S", localtime() ); > my $logdir = C4::Context->config('logdir'); >- my $dbversion = C4::Context->preference('Version'); > my $kohaversion = Koha::version; > $kohaversion =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; > >- my $filename_suffix = join '_', $now, $dbversion, $kohaversion; >+ my $filename_suffix = join '_', $now, $kohaversion; > my ( $logfilepath, $logfilepath_errors ) = ( > chk_log( $logdir, "updatedatabase_$filename_suffix" ), > chk_log( $logdir, "updatedatabase-error_$filename_suffix" ) >@@ -467,19 +467,14 @@ elsif ( $step && $step == 3 ) { > # we have tables, propose to select files to upload or updatedatabase > # > $template->param( "count" => $count, "default" => 1 ); >- # >- # 1st part of step 3 : check if there is a databaseversion systempreference >- # if there is, then we just need to upgrade >- # if there is none, then we need to install the database >- # >- if ( C4::Context->preference('Version') ) { >- my $dbversion = C4::Context->preference('Version'); >- $dbversion =~ /(.*)\.(..)(..)(...)/; >- $dbversion = "$1.$2.$3.$4"; >+ >+ # 1st part of step 3 : check if we need to install the database >+ # if not, then we just need to upgrade >+ unless ( C4::Context->needs_install() ) { > $template->param( > "upgrading" => 1, >- "dbversion" => $dbversion, > "kohaversion" => Koha::version(), >+ "pending_migrations" => [ Koha::Migrations->pending_migrations ], > ); > } > } >@@ -491,18 +486,10 @@ else { > # using opendir + language Hash > my $languages_loop = getTranslatedLanguages('intranet'); > $template->param( installer_languages_loop => $languages_loop ); >- if ($dbh) { >- my $rq = >- $dbh->prepare( >- "SELECT * from systempreferences WHERE variable='Version'"); >- if ( $rq->execute ) { >- my ($version) = $rq->fetchrow; >- if ($version) { >- print $query->redirect( >- "/cgi-bin/koha/installer/install.pl?step=3"); >- exit; >- } >- } >+ if (!C4::Context->needs_install) { >+ print $query->redirect( >+ "/cgi-bin/koha/installer/install.pl?step=3"); >+ exit; > } > } > output_html_with_http_headers $query, $cookie, $template->output; >diff --git a/installer/onboarding.pl b/installer/onboarding.pl >index 6680cd94d9..45d3abc02d 100755 >--- a/installer/onboarding.pl >+++ b/installer/onboarding.pl >@@ -35,7 +35,7 @@ use Koha::CirculationRules; > #Setting variables > my $input = CGI->new; > >-unless ( C4::Context->preference('Version') ) { >+if ( C4::Context->needs_install ) { > print $input->redirect("/cgi-bin/koha/installer/install.pl"); > exit; > } >diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step3.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step3.tt >index 28f14d177b..b5b497f62d 100644 >--- a/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step3.tt >+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/installer/step3.tt >@@ -233,7 +233,14 @@ > [% IF ( default ) %] > [% IF ( upgrading ) %] > <h2>Web installer › Update database</h2> >- <p>We are upgrading from Koha [% dbversion | html %] to [% kohaversion | html %]</p> >+ <p> >+ Pending migrations: >+ <ul> >+ [% FOREACH migration IN pending_migrations %] >+ <li>[% migration | html %]</li> >+ [% END %] >+ </ul> >+ </p> > <p><a href="install.pl?step=3&op=updatestructure" class="btn btn-primary">Update your database</a></p> > [% ELSE %] > <h2>Web installer › Install basic configuration settings</h2> >diff --git a/opac/maintenance.pl b/opac/maintenance.pl >index 7ec3ec8093..9679cdd840 100755 >--- a/opac/maintenance.pl >+++ b/opac/maintenance.pl >@@ -19,22 +19,18 @@ use Modern::Perl; > > use CGI qw ( -utf8 ); > use C4::Auth; >+use C4::Context; > use C4::Output; > use C4::Templates qw/gettemplate/; > > use Koha; >+use Koha::Migrations; > > my $query = CGI->new; > my $template = C4::Templates::gettemplate( 'maintenance.tt', 'opac', $query ); > >-my $koha_db_version = C4::Context->preference('Version'); >-my $kohaversion = Koha::version(); >-# Strip dots from version >-$kohaversion =~ s/\.//g if defined $kohaversion; >-$koha_db_version =~ s/\.//g if defined $koha_db_version; >- >-if ( !defined $koha_db_version || # DB not populated >- $kohaversion > $koha_db_version || # Update needed >+if ( C4::Context->needs_install() || # DB not populated >+ Koha::Migrations->pending_migrations > 0 || # Update needed > C4::Context->preference('OpacMaintenance') ) { # Maintenance mode enabled > output_html_with_http_headers $query, '', $template->output; > } >diff --git a/plugins/plugins-home.pl b/plugins/plugins-home.pl >index f9d72b885d..9a7b62857b 100755 >--- a/plugins/plugins-home.pl >+++ b/plugins/plugins-home.pl >@@ -47,8 +47,12 @@ my ( $template, $borrowernumber, $cookie ) = get_template_and_user( > > if ($plugins_enabled) { > >+ my $koha_version = Koha::version(); >+ if ($koha_version =~ /^(\d+)\.(\d+)\.(\d+)/) { >+ $koha_version = "$1.$2$3"; >+ } > $template->param( >- koha_version => C4::Context->preference("Version"), >+ koha_version => $koha_version, > method => $method, > ); > >diff --git a/t/00-checkdatabase-version.t b/t/00-checkdatabase-version.t >deleted file mode 100755 >index 9e94cbe50f..0000000000 >--- a/t/00-checkdatabase-version.t >+++ /dev/null >@@ -1,60 +0,0 @@ >-# Copyright 2010 Chris Cormack >-# >-# This file is part of Koha. >-# >-# 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 strict; >-use warnings; >- >-use Test::More; >-use File::Spec; >-use File::Find; >-use IO::File; >- >-my @files =('kohaversion.pl','installer/data/mysql/updatedatabase.pl'); >- >-foreach my $file (@files){ >- next unless -f $file; >- my @name_parts = File::Spec->splitpath($file); >- my %dirs = map { $_ => 1 } File::Spec->splitdir($name_parts[1]); >- next if exists $dirs{'.git'}; >- >- my $fh = IO::File->new($file, 'r'); >- my $xxx_found = 0; >- my $line = 0; >- while (<$fh>) { >- $line++; >- if (/XXX/i) { >- #two lines are an exception for updatedatabase (routine SetVersion and TransferToNum) >- next >- if $file =~ /updatedatabase/ >- && ( /s\/XXX\$\/999\/;/ >- || /\$_\[0\]=~ \/XXX\$\/;/ >- || /version contains XXX/ >- || /\$proposed_version =~ m\/XXX\// ); >- $xxx_found = 1; >- last; >- } >- } >- close $fh; >- if ($xxx_found) { >- fail("$file has no XXX in it"); >- diag("XXX found in line $line"); >- } else { >- pass("$file has no XXX in it"); >- } >-} >- >-done_testing(); >diff --git a/t/Context.t b/t/Context.t >index 8bff644cd5..31db8db4e6 100755 >--- a/t/Context.t >+++ b/t/Context.t >@@ -52,17 +52,6 @@ subtest 'yaml_preference() tests' => sub { > $context->unmock( 'preference' ); > }; > >-subtest 'needs_install() tests' => sub { >- >- plan tests => 2; >- >- t::lib::Mocks::mock_preference( 'Version', '3.0.0' ); >- is( C4::Context->needs_install, 0, 'Preference is defined, no need to install' ); >- >- t::lib::Mocks::mock_preference( 'Version', undef ); # the behaviour when ->preference fails to fetch >- is( C4::Context->needs_install, 1, "->preference(Version) is not defined, need to install" ); >-}; >- > my $context = Test::MockModule->new('C4::Context'); > my $userenv = {}; > >diff --git a/t/Koha_Template_Plugin_Koha.t b/t/Koha_Template_Plugin_Koha.t >index 13079a9753..282f7a96d9 100755 >--- a/t/Koha_Template_Plugin_Koha.t >+++ b/t/Koha_Template_Plugin_Koha.t >@@ -48,15 +48,13 @@ subtest "Koha::Template::Plugin::Koha::Version tests" => sub { > $major = $rnd->randregex('\d'); > $minor = $rnd->randregex('\d\d'); > $maintenance = $rnd->randregex('\d\d'); >- $development = $rnd->randregex('\d\d\d'); >- my $version = "$major.$minor.$maintenance.$development"; >+ my $version = "$major.$minor.$maintenance"; > my $res = Koha::Template::Plugin::Koha::Version( $version ); > is_deeply( $res, { > major => $major, > minor => $minor, > release => $major . "." . $minor, > maintenance => $major . "." . $minor . "." . $maintenance, >- development => $development > }, "Correct development version"); > > >@@ -64,15 +62,13 @@ subtest "Koha::Template::Plugin::Koha::Version tests" => sub { > $major = $rnd->randregex('\d'); > $minor = $rnd->randregex('\d\d'); > $maintenance = $rnd->randregex('\d\d'); >- $development = "000"; >- $version = "$major.$minor.$maintenance.$development"; >+ $version = "$major.$minor.$maintenance"; > $res = Koha::Template::Plugin::Koha::Version( $version ); > is_deeply( $res, { > major => $major, > minor => $minor, > release => $major . "." . $minor, > maintenance => $major . "." . $minor . "." . $maintenance, >- development => undef > }, "Correct maintenance version"); > > }; >diff --git a/t/db_dependent/Context.t b/t/db_dependent/Context.t >index 532f6cae70..86d75205a1 100755 >--- a/t/db_dependent/Context.t >+++ b/t/db_dependent/Context.t >@@ -134,16 +134,3 @@ my $yesno_pref = Koha::Config::SysPrefs->find('AutoEmailOpacUser'); > is( $yesno_pref->value(), 0, 'set_preference should have set the value to 0, instead of an empty string' ); > > done_testing(); >- >-sub TransformVersionToNum { >- my $version = shift; >- >- # remove the 3 last . to have a Perl number >- $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/; >- >- # three X's at the end indicate that you are testing patch with dbrev >- # change it into 999 >- # prevents error on a < comparison between strings (should be: lt) >- $version =~ s/XXX$/999/; >- return $version; >-} >diff --git a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t >index a0fc2dc51f..b61881b896 100755 >--- a/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t >+++ b/t/db_dependent/Koha/REST/Plugin/PluginRoutes.t >@@ -214,8 +214,8 @@ subtest 'needs_install use case tests' => sub { > my $plugins = Koha::Plugins->new; > $plugins->InstallPlugins; > >- # mock Version before initializing the API class >- t::lib::Mocks::mock_preference('Version', undef); >+ my $c4_context = Test::MockModule->new('C4::Context'); >+ $c4_context->mock('needs_install', sub { 1 }); > # initialize Koha::REST::V1 after mocking > > my $t = Test::Mojo->new('Koha::REST::V1'); >@@ -228,7 +228,7 @@ subtest 'needs_install use case tests' => sub { > 'Plugin enabled, route not defined as C4::Context->needs_install is true' > ); > >- t::lib::Mocks::mock_preference('Version', '3.0.0'); >+ $c4_context->mock('needs_install', sub { 0 }); > > $schema->resultset('PluginData')->delete; > $plugins->InstallPlugins; >-- >2.20.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 27880
:
117835
|
127998
|
153006
|
153007