From d7370772a48b1bee07473c8ba080306c18e60933 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 3 Oct 2013 08:11:17 +0200 Subject: Add support for compile-time detection of unnecessary data migration functions --- odb/details/meta/static-assert.hxx | 33 +++++++++++++++++++ odb/schema-catalog.hxx | 66 +++++++++++++++++++++++++++++++++----- 2 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 odb/details/meta/static-assert.hxx (limited to 'odb') 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 + +#include // ODB_CXX11 + +#ifndef ODB_CXX11 + +namespace odb +{ + namespace details + { + namespace meta + { + template + struct static_assert_test; + + template <> + struct static_assert_test {}; + } + } +} + +#endif + +#include + +#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 // schema_version, odb::core #include +#include +#include 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 + static void + data_migration_function (data_migration_function_type f, + const std::string& name = "") + { + data_migration_function (id_common, f, name); + } + + // Database-specific data migration. + // + template + static void + data_migration_function (database& db, + data_migration_function_type f, + const std::string& name = "") + { + data_migration_function (db.id (), f, name); + } + + template + 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 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 (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 (id, v, f, name); } }; -- cgit v1.1