diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2016-01-18 07:35:12 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2016-01-23 17:47:47 +0200 |
commit | 1dc38cf49b6c7a8b661a9cc675ded94c8ab33c36 (patch) | |
tree | 5a216148adb9d842a5a15c032a671182faa9ba06 /brep/database-lock | |
parent | fe6182a8c89675f92e72c881d707e21cdf56f376 (diff) |
Implement brep-migrate utility
Diffstat (limited to 'brep/database-lock')
-rw-r--r-- | brep/database-lock | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/brep/database-lock b/brep/database-lock new file mode 100644 index 0000000..72036a1 --- /dev/null +++ b/brep/database-lock @@ -0,0 +1,43 @@ +// file : brep/database-lock -*- C++ -*- +// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#ifndef BREP_DATABASE_LOCK +#define BREP_DATABASE_LOCK + +#include <memory> // unique_ptr +#include <exception> + +#include <odb/pgsql/forward.hxx> // database, transaction +#include <odb/pgsql/connection.hxx> + +namespace brep +{ + struct database_locked: std::exception + { + virtual char const* + what () const throw () {return "database locked";} + }; + + // Try to "lock" the PostgreSQL database in the constructor and release the + // lock in the destructor. Throw database_locked if the database is already + // locked by someone else. May also throw odb::pgsql::database_exception. + // + // This mechanism is used by the brep loader and schema migration tool to + // make sure they don't step on each others toes. + // + // Note: movable but not copyable. + // + class database_lock + { + public: + explicit + database_lock (odb::pgsql::database&); + + private: + odb::pgsql::connection_ptr connection_; + std::unique_ptr<odb::pgsql::transaction> transaction_; + }; +} + +#endif // BREP_DATABASE_LOCK |