aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-09-28 13:15:48 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-09-28 14:26:16 +0200
commit78a3f9d2223a7c6477036c8bd6fb8a6f188f344f (patch)
treeeb848fc7f6d305c1a43be2097c617628ce072911
parentb4825d95472f8d9e2b9892a56c585a035b6230ac (diff)
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).
-rw-r--r--odb/schema-catalog.cxx54
-rw-r--r--odb/schema-catalog.hxx36
2 files changed, 57 insertions, 33 deletions
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
diff --git a/odb/schema-catalog.hxx b/odb/schema-catalog.hxx
index a58a2dc..c02de4a 100644
--- a/odb/schema-catalog.hxx
+++ b/odb/schema-catalog.hxx
@@ -63,8 +63,8 @@ namespace odb
// Data migration.
//
public:
- // If version is 0, then use the current version and also check whether
- // we are in migration. Returns the number of calls made.
+ // If version is 0, then use the current database version and also
+ // check whether we are in migration. Returns the number of calls made.
//
static std::size_t
migrate_data (database&,
@@ -108,7 +108,7 @@ namespace odb
//
public:
// Migrate both schema and data to the specified version. If version
- // is not specified, then migrate to the latest version.
+ // is not specified, then migrate to the current model version.
//
static void
migrate (database&, schema_version = 0, const std::string& name = "");
@@ -116,33 +116,33 @@ namespace odb
// Schema version information.
//
public:
- // Return 0 if current is greater or equal to the latest version.
- // If current is not specified, get the current version from the
- // database.
+ // Return the current model version.
//
static schema_version
- next_version (const database& db,
- schema_version current = 0,
- const std::string& name = "")
+ current_version (const database& db, const std::string& name = "")
{
- return next_version (db.id (),
- current == 0 ? db.schema_version () : current,
- name);
+ return current_version (db.id (), name);
}
static schema_version
- next_version (database_id,
- schema_version current,
- const std::string& name = "");
+ current_version (database_id, const std::string& name = "");
+ // Return current model version + 1 (that is, one past current) if
+ // the passed version is equal to or greater than current. If the
+ // version is not specified, then use the current database version.
+ //
static schema_version
- latest_version (const database& db, const std::string& name = "")
+ next_version (const database& db,
+ schema_version v = 0,
+ const std::string& name = "")
{
- return latest_version (db.id (), name);
+ return next_version (db.id (), v == 0 ? db.schema_version () : v, name);
}
static schema_version
- latest_version (database_id, const std::string& name = "");
+ next_version (database_id,
+ schema_version,
+ const std::string& name = "");
// Schema existence.
//