summaryrefslogtreecommitdiff
path: root/odb-tests/evolution/data
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/evolution/data')
-rw-r--r--odb-tests/evolution/data/buildfile63
-rw-r--r--odb-tests/evolution/data/driver.cxx179
-rw-r--r--odb-tests/evolution/data/model.hxx45
-rw-r--r--odb-tests/evolution/data/test1.hxx9
-rw-r--r--odb-tests/evolution/data/test2.hxx11
-rw-r--r--odb-tests/evolution/data/test3.hxx11
-rw-r--r--odb-tests/evolution/data/testscript58
7 files changed, 376 insertions, 0 deletions
diff --git a/odb-tests/evolution/data/buildfile b/odb-tests/evolution/data/buildfile
new file mode 100644
index 0000000..d013cab
--- /dev/null
+++ b/odb-tests/evolution/data/buildfile
@@ -0,0 +1,63 @@
+# file : evolution/data/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_data_ \
+ --schema-version-table evo_data_sv \
+ --generate-schema \
+ --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
+ schema_versions = 002 003
+}
+
+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/data/driver.cxx b/odb-tests/evolution/data/driver.cxx
new file mode 100644
index 0000000..685f556
--- /dev/null
+++ b/odb-tests/evolution/data/driver.cxx
@@ -0,0 +1,179 @@
+// file : evolution/data/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test data migration support.
+//
+
+#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>
+
+#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;
+
+void
+migrate1 (database& db)
+{
+ using namespace v2;
+ using namespace v3;
+
+ unique_ptr<object1> o1 (db.load<object1> (1));
+ object2 o2 (1);
+ o2.num = o1->num;
+ db.persist (o2);
+}
+
+void
+migrate2 (database& db)
+{
+ using namespace v2;
+ using namespace v3;
+
+ unique_ptr<object1> o1 (db.load<object1> (2));
+ object2 o2 (2);
+ o2.num = o1->num;
+ db.persist (o2);
+}
+
+static const data_migration_entry<3, 1> migrate2_entry (&migrate2);
+
+int
+main (int argc, char* argv[])
+{
+ schema_catalog::data_migration_function<3, 1> (&migrate1);
+
+ schema_catalog::data_migration_function<3, 1> (
+ [] (database& db)
+ {
+ using namespace v2;
+ using namespace v3;
+
+ unique_ptr<object1> o1 (db.load<object1> (11));
+ object2 o2 (11);
+ o2.num = o1->num;
+ db.persist (o2);
+ });
+
+ try
+ {
+ unique_ptr<database> db (create_database (argc, argv, false));
+
+ db->schema_version_table (quote_name ("evo_data_sv"));
+
+ 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 ();
+ }
+
+ {
+ transaction t (db->begin ());
+
+ {
+ object1 o1 (1);
+ o1.num = 123;
+ db->persist (o1);
+ }
+
+ {
+ object1 o1 (2);
+ o1.num = 123;
+ db->persist (o1);
+ }
+
+ {
+ object1 o1 (11);
+ o1.num = 123;
+ db->persist (o1);
+ }
+
+ t.commit ();
+ }
+ break;
+ }
+ case 2:
+ {
+ {
+ transaction t (db->begin ());
+
+ if (embedded)
+ schema_catalog::migrate (*db);
+ else
+ schema_catalog::migrate_data (*db);
+
+ t.commit ();
+ }
+
+ break;
+ }
+ case 3:
+ {
+ using namespace v3;
+
+ {
+ transaction t (db->begin ());
+
+ {
+ unique_ptr<object2> o2 (db->load<object2> (1));
+ assert (o2->num == 123);
+ }
+
+ {
+ unique_ptr<object2> o2 (db->load<object2> (2));
+ assert (o2->num == 123);
+ }
+
+ {
+ unique_ptr<object2> o2 (db->load<object2> (11));
+ assert (o2->num == 123);
+ }
+
+ 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/data/model.hxx b/odb-tests/evolution/data/model.hxx
new file mode 100644
index 0000000..680bc55
--- /dev/null
+++ b/odb-tests/evolution/data/model.hxx
@@ -0,0 +1,45 @@
+// file : evolution/data/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/data/test1.hxx b/odb-tests/evolution/data/test1.hxx
new file mode 100644
index 0000000..976ed04
--- /dev/null
+++ b/odb-tests/evolution/data/test1.hxx
@@ -0,0 +1,9 @@
+// file : evolution/data/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/data/test2.hxx b/odb-tests/evolution/data/test2.hxx
new file mode 100644
index 0000000..bd72940
--- /dev/null
+++ b/odb-tests/evolution/data/test2.hxx
@@ -0,0 +1,11 @@
+// file : evolution/data/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/data/test3.hxx b/odb-tests/evolution/data/test3.hxx
new file mode 100644
index 0000000..0f47099
--- /dev/null
+++ b/odb-tests/evolution/data/test3.hxx
@@ -0,0 +1,11 @@
+// file : evolution/data/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/data/testscript b/odb-tests/evolution/data/testscript
new file mode 100644
index 0000000..dddc909
--- /dev/null
+++ b/odb-tests/evolution/data/testscript
@@ -0,0 +1,58 @@
+# file : evolution/data/testscript
+# license : GNU GPL v2; see accompanying LICENSE file
+
+.include ../../database-options.testscript
+.include ../../$db-schema.testscript
+
+test.arguments += $($(db)_options)
+
+: basics
+:
+if! $sqlite
+{
+ ss =; # Schema modification base file names.
+
+ # Drop everything.
+ #
+ for s: $schemas
+ ss =+ $s
+ end;
+
+ # Add base schema.
+ #
+ ss += test3-002-pre test3-002-post;
+
+ # Add migration.
+ #
+ ss += test3-003-pre test3-003-post;
+
+ # Run tests.
+ #
+ for s: $ss
+ f = $out_base/"$s".sql
+
+ if $mysql
+ cat $f | $create_schema_cmd
+ elif $pgsql
+ $create_schema_cmd -f $f
+ elif $oracle
+ $create_schema_cmd "@$f"
+ elif $mssql
+ $create_schema_cmd -i $f
+ end
+
+ if ($s == 'test3-002-post')
+ $* 1
+ elif ($s == 'test3-003-pre')
+ $* 2
+ elif ($s == 'test3-003-post')
+ $* 3
+ end
+ end
+}
+else
+{
+ $* 1 &odb-test.db;
+ $* 2;
+ $* 3
+}