diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-10-03 08:11:17 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-10-03 08:11:17 +0200 |
commit | d7370772a48b1bee07473c8ba080306c18e60933 (patch) | |
tree | bb1157f3d12f250bd835c9e26eba1fd1479891ab | |
parent | 78a3f9d2223a7c6477036c8bd6fb8a6f188f344f (diff) |
Add support for compile-time detection of unnecessary data migration functions
-rw-r--r-- | odb/details/meta/static-assert.hxx | 33 | ||||
-rw-r--r-- | odb/schema-catalog.hxx | 66 |
2 files changed, 91 insertions, 8 deletions
diff --git a/odb/details/meta/static-assert.hxx b/odb/details/meta/static-assert.hxx new file mode 100644 index 0000000..6bb84f2 --- /dev/null +++ b/odb/details/meta/static-assert.hxx @@ -0,0 +1,33 @@ +// file : odb/details/meta/static-assert.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef ODB_DETAILS_META_STATIC_ASSERT_HXX +#define ODB_DETAILS_META_STATIC_ASSERT_HXX + +#include <odb/pre.hxx> + +#include <odb/details/config.hxx> // ODB_CXX11 + +#ifndef ODB_CXX11 + +namespace odb +{ + namespace details + { + namespace meta + { + template <bool> + struct static_assert_test; + + template <> + struct static_assert_test<true> {}; + } + } +} + +#endif + +#include <odb/post.hxx> + +#endif // ODB_DETAILS_META_STATIC_ASSERT_HXX diff --git a/odb/schema-catalog.hxx b/odb/schema-catalog.hxx index c02de4a..968cbdc 100644 --- a/odb/schema-catalog.hxx +++ b/odb/schema-catalog.hxx @@ -19,6 +19,8 @@ #include <odb/forward.hxx> // schema_version, odb::core #include <odb/details/export.hxx> +#include <odb/details/unused.hxx> +#include <odb/details/meta/static-assert.hxx> namespace odb { @@ -77,8 +79,60 @@ namespace odb typedef void (*data_migration_function_type) (database&); #endif + // The following three variants of the registration functions make + // sure that the version is greater that the base model version. + // This helps with identifying and removing data migration function + // that are no longer required. + // // Data migration functions are called in the order of registration. // + template <schema_version v, schema_version base> + static void + data_migration_function (data_migration_function_type f, + const std::string& name = "") + { + data_migration_function<v, base> (id_common, f, name); + } + + // Database-specific data migration. + // + template <schema_version v, schema_version base> + static void + data_migration_function (database& db, + data_migration_function_type f, + const std::string& name = "") + { + data_migration_function<v, base> (db.id (), f, name); + } + + template <schema_version v, schema_version base> + static void + data_migration_function (database_id id, + data_migration_function_type f, + const std::string& name = "") + { + // If the data migration version is below the base model version + // then it will never be called. + // +#ifdef ODB_CXX11 + static_assert (v > base || base == 0, + "data migration function is no longer necessary"); +#else + // Poor man's static_assert. + // + typedef details::meta::static_assert_test<(v > base || base == 0)> + data_migration_function_is_no_longer_necessary; + + char sa [sizeof (data_migration_function_is_no_longer_necessary)]; + ODB_POTENTIALLY_UNUSED (sa); +#endif + + data_migration_function (id, v, f, name); + } + + // The same as above but take the version as an argument and do + // not check whether it is greater than the base model version. + // static void data_migration_function (schema_version v, data_migration_function_type f, @@ -87,8 +141,6 @@ namespace odb data_migration_function (id_common, v, f, name); } - // Database-specific data migration. - // static void data_migration_function (database& db, schema_version v, @@ -175,23 +227,21 @@ namespace odb // Static data migration function registration. // + template <schema_version v, schema_version base> struct LIBODB_EXPORT data_migration_entry { typedef schema_catalog::data_migration_function_type function_type; - data_migration_entry (schema_version v, - function_type f, - const std::string& name = "") + data_migration_entry (function_type f, const std::string& name = "") { - schema_catalog::data_migration_function (v, f, name); + schema_catalog::data_migration_function<v, base> (f, name); } data_migration_entry (database_id id, - schema_version v, function_type f, const std::string& name = "") { - schema_catalog::data_migration_function (id, v, f, name); + schema_catalog::data_migration_function<v, base> (id, v, f, name); } }; |