diff options
Diffstat (limited to 'odb-tests/evolution/combined')
-rw-r--r-- | odb-tests/evolution/combined/driver.cxx | 157 | ||||
-rw-r--r-- | odb-tests/evolution/combined/model.hxx | 174 | ||||
-rw-r--r-- | odb-tests/evolution/combined/test1.hxx | 9 | ||||
-rw-r--r-- | odb-tests/evolution/combined/test2.hxx | 11 | ||||
-rw-r--r-- | odb-tests/evolution/combined/test3.hxx | 11 |
5 files changed, 362 insertions, 0 deletions
diff --git a/odb-tests/evolution/combined/driver.cxx b/odb-tests/evolution/combined/driver.cxx new file mode 100644 index 0000000..88453c3 --- /dev/null +++ b/odb-tests/evolution/combined/driver.cxx @@ -0,0 +1,157 @@ +// file : evolution/combined/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Combined schema evolution test. +// + +#include <memory> // std::auto_ptr +#include <cassert> +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> +#include <odb/schema-catalog.hxx> + +#include <common/common.hxx> + +#include "test2.hxx" +#include "test3.hxx" +#include "test2-odb.hxx" +#include "test3-odb.hxx" + +using namespace std; +using namespace odb::core; + +int +main (int argc, char* argv[]) +{ + try + { + auto_ptr<database> db (create_database (argc, argv, false)); + bool embedded (schema_catalog::exists (*db)); + + // 1 - base version + // 2 - migration + // 3 - current version + // + unsigned short pass (*argv[argc - 1] - '0'); + + switch (pass) + { + case 1: + { + using namespace v2; + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::drop_schema (*db); + schema_catalog::create_schema (*db, "", false); + schema_catalog::migrate_schema (*db, 2); + t.commit (); + } + + object o ("1"); + o.dui = 1; + o.anui = 1; + o.dnui = 1; + o.dc = 1; + o.dt.push_back (1); + o.aui = 1; + +#ifndef DATABASE_SQLITE + o.dfk = new object1 (1); + o.acn = 1; + o.acnn.reset (); + o.afk = 1; +#endif + + { + transaction t (db->begin ()); +#ifndef DATABASE_SQLITE + db->persist (o.dfk); +#endif + db->persist (o); + t.commit (); + } + break; + } + case 2: + { + using namespace v3; + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3); + t.commit (); + } + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> ("1")); + + assert (p->ac1 == 999); + assert (!p->ac2); + +#ifndef DATABASE_SQLITE + assert (!p->ac3); +#endif + // Migrate. + // + p->at.push_back ("abc"); + +#ifndef DATABASE_SQLITE + p->dfk = 999; + p->ac3 = 1; + p->acn.reset (); + p->acnn = 1; +#endif + db->update (*p); + + t.commit (); + } + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_post (*db, 3); + t.commit (); + } + break; + } + case 3: + { + using namespace v3; + + { + transaction t (db->begin ()); + auto_ptr<object> p (db->load<object> ("1")); + + // Check post-migration. + // + assert (p->at[0] == "abc"); + +#ifndef DATABASE_SQLITE + assert (p->dfk == 999); + assert (*p->ac3 == 1); + assert (!p->acn); + assert (*p->acnn == 1); +#endif + t.commit (); + } + break; + } + default: + { + cerr << "unknown pass number '" << argv[argc - 1] << "'" << endl; + return 1; + } + } + } + catch (const odb::exception& e) + { + cerr << e.what () << endl; + return 1; + } +} diff --git a/odb-tests/evolution/combined/model.hxx b/odb-tests/evolution/combined/model.hxx new file mode 100644 index 0000000..e924943 --- /dev/null +++ b/odb-tests/evolution/combined/model.hxx @@ -0,0 +1,174 @@ +// file : evolution/combined/model.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef MODEL_VERSION +# error model.hxx included directly +#endif + +#include <string> +#include <vector> + +#include <odb/core.hxx> +#include <odb/nullable.hxx> + +#include <common/config.hxx> // DATABASE_XXX + +#pragma db model version(1, MODEL_VERSION) + +#define MODEL_NAMESPACE_IMPL(V) v##V +#define MODEL_NAMESPACE(V) MODEL_NAMESPACE_IMPL(V) + +namespace MODEL_NAMESPACE(MODEL_VERSION) +{ + #pragma db object + struct object1 + { + object1 (unsigned long id = 0): id_ (id) {} + + #pragma db id + unsigned long id_; + }; + + #pragma db object + struct object + { + #pragma db id + std::string id_; + + // + // Pre pass 1. + // + + // Drop unique index. + // +#if MODEL_VERSION == 2 + #pragma db unique +#endif + unsigned long dui; + + // Alter table drop foreign key. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 2 + object1* dfk; +#else + #pragma db null + unsigned long dfk; +#endif +#endif + + // Add table. + // +#if MODEL_VERSION == 3 + std::vector<std::string> at; +#endif + + // Add column. + // +#if MODEL_VERSION == 3 + #pragma db default(999) + unsigned long ac1; + + odb::nullable<unsigned long> ac2; + + // Initially added as NULL, converted to NOT NULL. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE + #pragma db not_null + odb::nullable<unsigned long> ac3; +#endif +#endif + + // Alter column NULL. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 2 + unsigned long acn; +#else + odb::nullable<unsigned long> acn; +#endif +#endif + + // + // Pre pass 2. + // + + // Add non-unique indexes. + // +#if MODEL_VERSION == 3 + #pragma db index +#endif + unsigned long anui; + + // + // Post pass 1. + // + + // Drop non-unique indexes. + // +#if MODEL_VERSION == 2 + #pragma db index +#endif + unsigned long dnui; + + // + // Post pass 2. + // + + // Drop table. + // +#if MODEL_VERSION == 2 + std::vector<unsigned long> dt; +#endif + + // Drop column. Logical drop (set NULL) in SQLite. + // +#if MODEL_VERSION == 2 + unsigned long dc; +#endif + + // Alter column NOT NULL. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 3 + #pragma db not_null +#endif + odb::nullable<unsigned long> acnn; +#endif + + // Alter table add foreign key. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 2 + #pragma db null + unsigned long afk; +#else + object1* afk; +#endif +#endif + + // Add unique index. + // +#if MODEL_VERSION == 3 + #pragma db unique +#endif + unsigned long aui; + + public: +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 2 + + object (std::string id = ""): id_ (id), dfk (0) {} + ~object () {delete dfk;} +#else + object (std::string id = ""): id_ (id), afk (0) {} + ~object () {delete afk;} +#endif +#else + object (std::string id = ""): id_ (id) {} +#endif + }; +} + +#undef MODEL_NAMESPACE +#undef MODEL_NAMESPACE_IMPL diff --git a/odb-tests/evolution/combined/test1.hxx b/odb-tests/evolution/combined/test1.hxx new file mode 100644 index 0000000..1a18aff --- /dev/null +++ b/odb-tests/evolution/combined/test1.hxx @@ -0,0 +1,9 @@ +// file : evolution/combined/test1.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST1_HXX +#define TEST1_HXX + +#pragma db model version(1, 1) + +#endif // TEST1_HXX diff --git a/odb-tests/evolution/combined/test2.hxx b/odb-tests/evolution/combined/test2.hxx new file mode 100644 index 0000000..8eb7ac8 --- /dev/null +++ b/odb-tests/evolution/combined/test2.hxx @@ -0,0 +1,11 @@ +// file : evolution/combined/test2.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST2_HXX +#define TEST2_HXX + +#define MODEL_VERSION 2 +#include "model.hxx" +#undef MODEL_VERSION + +#endif // TEST2_HXX diff --git a/odb-tests/evolution/combined/test3.hxx b/odb-tests/evolution/combined/test3.hxx new file mode 100644 index 0000000..05a052f --- /dev/null +++ b/odb-tests/evolution/combined/test3.hxx @@ -0,0 +1,11 @@ +// file : evolution/combined/test3.hxx +// license : GNU GPL v2; see accompanying LICENSE file + +#ifndef TEST3_HXX +#define TEST3_HXX + +#define MODEL_VERSION 3 +#include "model.hxx" +#undef MODEL_VERSION + +#endif // TEST3_HXX |