From 78a3f9d2223a7c6477036c8bd6fb8a6f188f344f Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 28 Sep 2013 13:15:48 +0200 Subject: Rework migration API in schema_catalog Specifically: - Rename latest_version() to current_version(). - Change next_version() to return one past current instead of 0 if passed current. - migrate() will now do schema creation if current database version is 0 (no schema). --- odb/schema-catalog.cxx | 54 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 15 deletions(-) (limited to 'odb/schema-catalog.cxx') diff --git a/odb/schema-catalog.cxx b/odb/schema-catalog.cxx index 15cdcb9..bec13a7 100644 --- a/odb/schema-catalog.cxx +++ b/odb/schema-catalog.cxx @@ -250,15 +250,30 @@ namespace odb void schema_catalog:: migrate (database& db, schema_version v, const string& name) { - schema_version latest (latest_version (db, name)); + schema_version cur (current_version (db, name)); if (v == 0) - v = latest; - else if (v > latest) + v = cur; + else if (v > cur) throw unknown_schema_version (v); - for (schema_version i (next_version (db, 0, name)); - i != 0 && i <= v; + schema_version i (db.schema_version (name)); + + // If there is no schema, then "migrate" by creating it. + // + if (i == 0) + { + // Schema creation can only "migrate" straight to current. + // + if (v != cur) + throw unknown_schema_version (v); + + create_schema (db, name, false); + return; + } + + for (i = next_version (db, i, name); + i <= v; i = next_version (db, i, name)) { migrate_schema_pre (db, i, name); @@ -268,7 +283,7 @@ namespace odb } schema_version schema_catalog:: - next_version (database_id id, schema_version current, const string& name) + current_version (database_id id, const string& name) { const schema_catalog_impl& c (*schema_catalog_init::catalog); schema_map::const_iterator i (c.schema.find (key (id, name))); @@ -277,22 +292,31 @@ namespace odb throw unknown_schema (name); const version_map& vm (i->second.migrate); - version_map::const_iterator j (vm.upper_bound (current)); - return j != vm.end () ? j->first : 0; + assert (!vm.empty ()); + return vm.rbegin ()->first; } schema_version schema_catalog:: - latest_version (database_id id, const string& name) + next_version (database_id id, schema_version v, const string& name) { - const schema_catalog_impl& c (*schema_catalog_init::catalog); - schema_map::const_iterator i (c.schema.find (key (id, name))); + const schema_catalog_impl& sc (*schema_catalog_init::catalog); + schema_map::const_iterator i (sc.schema.find (key (id, name))); - if (i == c.schema.end ()) + if (i == sc.schema.end ()) throw unknown_schema (name); - const version_map& vm (i->second.migrate); - assert (!vm.empty ()); - return vm.rbegin ()->first; + const version_map& vm (i->second.migrate); // Cannot be empty. + + schema_version b (vm.begin ()->first); + schema_version c (vm.rbegin ()->first); + + if (v == 0) + return c; // "Migration" to the current via schema creation. + else if (v < b) + throw unknown_schema_version (v); // Unsupported migration. + + version_map::const_iterator j (vm.upper_bound (v)); + return j != vm.end () ? j->first : c + 1; } // schema_catalog_init -- cgit v1.1