From d78ba3e97cbe0d6c641120d94d47e25b198febfb Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 25 Apr 2013 07:35:45 +0200 Subject: Add support for schema version table --- odb/database.hxx | 46 ++++++++++++++++++++++++++++++++++++++++++++++ odb/database.ixx | 38 ++++++++++++++++++++++++++++++++++++++ odb/schema-catalog.cxx | 24 ++++++++++++++++++++++++ odb/schema-catalog.hxx | 24 +++++++++++++++++------- 4 files changed, 125 insertions(+), 7 deletions(-) diff --git a/odb/database.hxx b/odb/database.hxx index 6876850..79fa622 100644 --- a/odb/database.hxx +++ b/odb/database.hxx @@ -317,6 +317,47 @@ namespace odb tracer_type* tracer () const; + // Database schema version. + // + public: + typedef odb::schema_version schema_version_type; + + schema_version_type + schema_version (const std::string& schema_name = "") const; + + bool + schema_migration (const std::string& schema_name = "") const; + + // Set schema version and migration state manually. + // + void + schema_version (schema_version_type, + bool migration, + const std::string& schema_name = ""); + + // Set default schema version table for all schema names. The table + // name should already be quoted if necessary. + // + void + schema_version_table (const std::string& table_name); + + // Set schema version table for a specific schema. + // + void + schema_version_table (const std::string& table_name, + const std::string& schema_name); + + protected: + struct schema_version_info + { + schema_version_type version; + bool migration; + std::string version_table; + }; + + virtual const schema_version_info& + load_schema_version (const std::string& schema_name) const = 0; + // Database id. // public: @@ -393,9 +434,14 @@ namespace odb std::map query_factory_map; + typedef std::map schema_version_map; + database_id id_; tracer_type* tracer_; query_factory_map query_factory_map_; + + std::string schema_version_table_; + mutable schema_version_map schema_version_map_; }; } diff --git a/odb/database.ixx b/odb/database.ixx index d58e5e7..b7c1917 100644 --- a/odb/database.ixx +++ b/odb/database.ixx @@ -21,6 +21,44 @@ namespace odb return id_; } + inline database::schema_version_type database:: + schema_version (const std::string& name) const + { + schema_version_map::const_iterator i (schema_version_map_.find (name)); + return i != schema_version_map_.end () && i->second.version != 0 + ? i->second.version + : load_schema_version (name).version; + } + + inline bool database:: + schema_migration (const std::string& name) const + { + schema_version_map::const_iterator i (schema_version_map_.find (name)); + return i != schema_version_map_.end () && i->second.version != 0 + ? i->second.migration + : load_schema_version (name).migration; + } + + inline void database:: + schema_version (schema_version_type v, bool m, const std::string& name) + { + schema_version_info& svi (schema_version_map_[name]); + svi.version = v; + svi.migration = m; + } + + inline void database:: + schema_version_table (const std::string& tname) + { + schema_version_table_ = tname; + } + + inline void database:: + schema_version_table (const std::string& tname, const std::string& sname) + { + schema_version_map_[sname].version_table = tname; + } + inline connection_ptr database:: connection () { diff --git a/odb/schema-catalog.cxx b/odb/schema-catalog.cxx index 9599afe..137931f 100644 --- a/odb/schema-catalog.cxx +++ b/odb/schema-catalog.cxx @@ -147,6 +147,30 @@ namespace odb if (!pre || m != migrate_both) break; } + + // Update the schema version on the database instance. + // + db.schema_version (v, m == migrate_pre, name); + } + + void schema_catalog:: + migrate (database& db, schema_version v, const std::string& name) + { + schema_version latest (latest_version (db, name)); + + if (v == 0) + v = latest; + else if (v > latest) + throw unknown_schema_version (v); + + for (schema_version i (next_version (db, 0, name)); + i != 0 && i <= v; + i = next_version (db, i, name)) + { + migrate_schema_pre (db, i, name); + // migrate_data (db, i, name); + migrate_schema_post (db, i, name); + } } schema_version schema_catalog:: diff --git a/odb/schema-catalog.hxx b/odb/schema-catalog.hxx index a186de4..07e625b 100644 --- a/odb/schema-catalog.hxx +++ b/odb/schema-catalog.hxx @@ -52,28 +52,38 @@ namespace odb migrate_schema_impl (db, v, name, migrate_both); } + // Migrate both schema and data to the specified version. If version + // is not specified, then migrate to the latest version. + // + static void + migrate (database& db, schema_version v = 0, const std::string& name = ""); + // Return 0 if current is greater or equal to the latest version. + // If current is not specified, get the current version from the + // database. // - schema_version + static schema_version next_version (const database& db, - schema_version current /*= 0*/, + schema_version current = 0, const std::string& name = "") { - return next_version (db.id (), current, name); + return next_version (db.id (), + current == 0 ? db.schema_version () : current, + name); } - schema_version + static schema_version next_version (database_id, - schema_version current /*= 0*/, + schema_version current, const std::string& name = ""); - schema_version + static schema_version latest_version (const database& db, const std::string& name = "") { return latest_version (db.id (), name); } - schema_version + static schema_version latest_version (database_id, const std::string& name = ""); // Test for presence of a schema with a specific name. -- cgit v1.1