summaryrefslogtreecommitdiff
path: root/odb-tests/evolution/embedded
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/evolution/embedded')
-rw-r--r--odb-tests/evolution/embedded/buildfile59
-rw-r--r--odb-tests/evolution/embedded/driver.cxx185
-rw-r--r--odb-tests/evolution/embedded/model.hxx45
-rw-r--r--odb-tests/evolution/embedded/test1.hxx9
-rw-r--r--odb-tests/evolution/embedded/test2.hxx11
-rw-r--r--odb-tests/evolution/embedded/test3.hxx11
-rw-r--r--odb-tests/evolution/embedded/testscript21
7 files changed, 341 insertions, 0 deletions
diff --git a/odb-tests/evolution/embedded/buildfile b/odb-tests/evolution/embedded/buildfile
new file mode 100644
index 0000000..095e476
--- /dev/null
+++ b/odb-tests/evolution/embedded/buildfile
@@ -0,0 +1,59 @@
+# file : evolution/embedded/buildfile
+# license : GNU GPL v2; see accompanying LICENSE file
+
+if ($build.meta_operation != 'dist')
+ assert (!$multi) "multi-database mode is not supported by this test"
+
+db = ($databases[0])
+
+import libodb = libodb%lib{odb}
+
+import libs = libodb-$db%lib{odb-$db}
+import libs += lib{common}
+
+hdrs = test1 test2 test3
+
+exe{driver}: {hxx cxx}{* -*-odb} testscript
+
+# Introduce the metadata library target to make sure the libodb library is
+# resolved for the odb_compile ad hoc rule (see build/root.build for details).
+#
+libue{test-meta}: $libodb
+
+for h: $hdrs
+ exe{driver}: {hxx ixx cxx}{$h-odb}: hxx{$h} libue{test-meta} hxx{model}
+
+# Make sure testN.hxx are compiled serially since they share the changelog.
+#
+# @@ TODO: make order-only when supported by build2.
+#
+{hxx ixx cxx}{test3-odb}: {hxx ixx cxx}{test2-odb}: {hxx ixx cxx}{test1-odb}
+
+exe{driver}: libue{test-meta} $libs
+
+# Specify the ODB custom options to be used by the odb_compile ad hoc rule
+# (see build/root.build for details).
+#
+odb_options = --table-prefix evo_embedded_ \
+ --schema-version-table evo_embedded_sv \
+ --generate-schema \
+ --schema-format embedded \
+ --generate-query \
+ --at-once \
+ --changelog $out_base/model.xml
+
+<{hxx ixx cxx}{test1-odb}>: odb_options += --init-changelog
+<{hxx ixx cxx}{test2-odb}>: odb_options += --omit-create --suppress-migration
+<{hxx ixx cxx}{test3-odb}>: odb_options += --omit-create
+
+cxx.poptions =+ "-I$out_base" "-I$src_base"
+
+# Testscript's run-time prerequisites.
+#
+exe{driver}: ../../alias{database-client}: include = adhoc
+
+testscript@./:
+{
+ db = $db
+ schemas = $hdrs
+}
diff --git a/odb-tests/evolution/embedded/driver.cxx b/odb-tests/evolution/embedded/driver.cxx
new file mode 100644
index 0000000..2bdb2d1
--- /dev/null
+++ b/odb-tests/evolution/embedded/driver.cxx
@@ -0,0 +1,185 @@
+// file : evolution/embedded/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test embedded schema migration.
+//
+
+#include <memory> // std::unique_ptr
+#include <iostream>
+
+#include <odb/database.hxx>
+#include <odb/transaction.hxx>
+#include <odb/schema-catalog.hxx>
+
+#include <libcommon/config.hxx> // DATABASE_XXX
+#include <libcommon/common.hxx>
+
+#ifdef DATABASE_PGSQL
+# include <odb/pgsql/connection.hxx>
+#endif
+
+#include "test2.hxx"
+#include "test3.hxx"
+#include "test2-odb.hxx"
+#include "test3-odb.hxx"
+
+#undef NDEBUG
+#include <cassert>
+
+using namespace std;
+using namespace odb::core;
+
+int
+main (int argc, char* argv[])
+{
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv, false));
+
+ db->schema_version_table (quote_name ("evo_embedded_sv"));
+
+ // 1 - base version
+ // 2 - migration
+ // 3 - current version
+ //
+ unsigned short pass (*argv[argc - 1] - '0');
+
+ switch (pass)
+ {
+ case 1:
+ {
+ using namespace v2;
+
+ {
+ transaction t (db->begin ());
+ schema_catalog::drop_schema (*db);
+ t.commit ();
+ }
+
+ // PostgreSQL cannot continue a transaction after a query failed. We
+ // have a workaround but only for 9.4+.
+ //
+#ifdef DATABASE_PGSQL
+ {
+ odb::connection_ptr c (db->connection ());
+ int v (static_cast<odb::pgsql::connection&> (*c).server_version ());
+ if (v < 90400)
+ assert (db->schema_version () == 0);
+ }
+#endif
+
+ {
+ transaction t (db->begin ());
+ assert (db->schema_version () == 0);
+
+ schema_catalog::create_schema (*db, "", false);
+
+ assert (db->schema_version () == 1 && !db->schema_migration ());
+
+ schema_catalog::migrate_schema (*db, 2);
+ t.commit ();
+ }
+
+ assert (db->schema_version () == 2 && !db->schema_migration ());
+
+ {
+ transaction t (db->begin ());
+ object1 o1 (1);
+ o1.num = 123;
+ db->persist (o1);
+ t.commit ();
+ }
+ break;
+ }
+ case 2:
+ {
+ using namespace v2;
+ using namespace v3;
+
+ // Check version information correctness.
+ //
+ assert (schema_catalog::base_version (*db) == 1);
+ assert (schema_catalog::current_version (*db) == 3);
+ assert (schema_catalog::next_version (*db, 0) == 3);
+ assert (schema_catalog::next_version (*db, 1) == 2);
+ assert (schema_catalog::next_version (*db) == 3);
+ assert (schema_catalog::next_version (*db, 3) == 4);
+
+ {
+ assert (db->schema_version () == 2 && !db->schema_migration ());
+
+ transaction t (db->begin ());
+ schema_catalog::migrate_schema_pre (*db, 3);
+ t.commit ();
+ }
+
+ assert (db->schema_version () == 3 && db->schema_migration ());
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object1> o1 (db->load<object1> (1));
+ object2 o2 (1);
+ o2.num = o1->num;
+ db->persist (o2);
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ schema_catalog::migrate_schema_post (*db, 3);
+ t.commit ();
+
+ assert (db->schema_version () == 3 && !db->schema_migration ());
+ }
+ break;
+ }
+ case 3:
+ {
+ using namespace v3;
+
+ // In transaction.
+ //
+ {
+ transaction t (db->begin ());
+ assert (db->schema_version () == 3 && !db->schema_migration ());
+ t.commit ();
+ }
+
+ {
+ transaction t (db->begin ());
+ unique_ptr<object2> o2 (db->load<object2> (1));
+ assert (o2->num == 123);
+ t.commit ();
+ }
+
+ // Test the case where there is still no version table.
+ //
+ db->schema_version_migration (0, false);
+
+ {
+ transaction t (db->begin ());
+
+#ifdef DATABASE_ORACLE
+ db->execute ("DROP TABLE \"evo_embedded_sv\"");
+#else
+ db->execute ("DROP TABLE evo_embedded_sv");
+#endif
+ t.commit ();
+ }
+
+ assert (db->schema_version () == 0);
+ 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/embedded/model.hxx b/odb-tests/evolution/embedded/model.hxx
new file mode 100644
index 0000000..f3aa7a4
--- /dev/null
+++ b/odb-tests/evolution/embedded/model.hxx
@@ -0,0 +1,45 @@
+// file : evolution/embedded/model.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef MODEL_VERSION
+# error model.hxx included directly
+#endif
+
+#include <odb/core.hxx>
+
+#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)
+{
+#if MODEL_VERSION == 2
+ #pragma db object
+ struct object1
+ {
+ object1 (unsigned long id = 0): id_ (id) {}
+
+ #pragma db id
+ unsigned long id_;
+
+ int num;
+ };
+#endif
+
+#if MODEL_VERSION == 3
+ #pragma db object
+ struct object2
+ {
+ object2 (unsigned long id = 0): id_ (id) {}
+
+ #pragma db id
+ unsigned long id_;
+
+ int num;
+ };
+#endif
+}
+
+#undef MODEL_NAMESPACE
+#undef MODEL_NAMESPACE_IMPL
diff --git a/odb-tests/evolution/embedded/test1.hxx b/odb-tests/evolution/embedded/test1.hxx
new file mode 100644
index 0000000..32903a1
--- /dev/null
+++ b/odb-tests/evolution/embedded/test1.hxx
@@ -0,0 +1,9 @@
+// file : evolution/embedded/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/embedded/test2.hxx b/odb-tests/evolution/embedded/test2.hxx
new file mode 100644
index 0000000..fce8760
--- /dev/null
+++ b/odb-tests/evolution/embedded/test2.hxx
@@ -0,0 +1,11 @@
+// file : evolution/embedded/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/embedded/test3.hxx b/odb-tests/evolution/embedded/test3.hxx
new file mode 100644
index 0000000..d49ecc5
--- /dev/null
+++ b/odb-tests/evolution/embedded/test3.hxx
@@ -0,0 +1,11 @@
+// file : evolution/embedded/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
diff --git a/odb-tests/evolution/embedded/testscript b/odb-tests/evolution/embedded/testscript
new file mode 100644
index 0000000..3ddf983
--- /dev/null
+++ b/odb-tests/evolution/embedded/testscript
@@ -0,0 +1,21 @@
+# file : evolution/embedded/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+
+test.arguments += $($(db)_options)
+
+: basics
+:
+if! $sqlite
+{
+ $* 1;
+ $* 2;
+ $* 3
+}
+else
+{
+ $* 1 &odb-test.db;
+ $* 2;
+ $* 3
+}