From 299f9682a4a2df434d0e61e73780fd2555afa1b8 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 12 Jun 2018 22:02:08 +0300 Subject: Default version epoch to one --- bpkg/database.cxx | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'bpkg/database.cxx') diff --git a/bpkg/database.cxx b/bpkg/database.cxx index 3c55237..7d0e2ac 100644 --- a/bpkg/database.cxx +++ b/bpkg/database.cxx @@ -44,6 +44,52 @@ namespace bpkg } }; + // Register the data migration functions. + // + template + using migration_entry = odb::data_migration_entry; + + // Migrate tables that contain package version columns converting the + // default zero version epoch to one, unless the version is a stub. + // + // Note that we can't really distinguish the default zero epoch from an + // explicitly specified one, so will just update all of them, assuming that + // it is currently unlikely that the epoch was specified explicitly for any + // package version. + // + static const migration_entry<5> + migrate_epoch_entry ([] (odb::database& db) + { + // Delay the foreign key constraint checks until we are done with all the + // tables. + // + assert (transaction::has_current ()); + db.execute ("PRAGMA defer_foreign_keys = ON"); + + auto update = [&db] (const string& table, + const string& version_prefix = "version") + { + string ec (version_prefix + "_epoch"); + + db.execute ("UPDATE " + table + " SET " + ec + " = 1 " + + "WHERE " + ec + " = 0 AND NOT (" + + version_prefix + "_canonical_upstream = '' AND " + + version_prefix + "_canonical_release = '~')"); + }; + + update ("available_package"); + update ("available_package_locations"); + update ("available_package_dependencies"); + update ("available_package_dependency_alternatives"); + update ("available_package_dependency_alternatives", "dep_min_version"); + update ("available_package_dependency_alternatives", "dep_max_version"); + update ("selected_package"); + update ("selected_package_prerequisites", "min_version"); + update ("selected_package_prerequisites", "max_version"); + + db.execute ("PRAGMA defer_foreign_keys = OFF"); + }); + database open (const dir_path& d, tracer& tr, bool create) { -- cgit v1.1