summaryrefslogtreecommitdiff
path: root/odb-tests/evolution/drop-foreign-key
diff options
context:
space:
mode:
Diffstat (limited to 'odb-tests/evolution/drop-foreign-key')
-rw-r--r--odb-tests/evolution/drop-foreign-key/driver.cxx145
-rw-r--r--odb-tests/evolution/drop-foreign-key/model.hxx50
-rw-r--r--odb-tests/evolution/drop-foreign-key/test1.hxx9
-rw-r--r--odb-tests/evolution/drop-foreign-key/test2.hxx11
-rw-r--r--odb-tests/evolution/drop-foreign-key/test3.hxx11
5 files changed, 226 insertions, 0 deletions
diff --git a/odb-tests/evolution/drop-foreign-key/driver.cxx b/odb-tests/evolution/drop-foreign-key/driver.cxx
new file mode 100644
index 0000000..f829562
--- /dev/null
+++ b/odb-tests/evolution/drop-foreign-key/driver.cxx
@@ -0,0 +1,145 @@
+// file : evolution/drop-foreign-key/driver.cxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+// Test dropping a foreign key.
+//
+
+#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));
+
+ // SQLite doesn't support dropping of foreign keys.
+ //
+#ifndef DATABASE_SQLITE
+ 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.o1 = new object (2);
+ o.o2 = new object (3);
+
+ {
+ transaction t (db->begin ());
+ db->persist (o.o1);
+ db->persist (o.o2);
+ db->persist (o);
+ t.commit ();
+ }
+
+ // The foreign key constraint is there.
+ //
+ try
+ {
+ object o (11);
+ o.o1 = new object (12);
+ o.o2 = new object (13);
+
+ transaction t (db->begin ());
+ db->persist (o);
+ assert (false);
+ }
+ catch (const odb::exception& ) {}
+ break;
+ }
+ case 2:
+ {
+ using namespace v3;
+
+ if (embedded)
+ {
+ transaction t (db->begin ());
+ schema_catalog::migrate_schema_pre (*db, 3);
+ t.commit ();
+ }
+
+ // The data is still there but the constraints are gone.
+ //
+ {
+ transaction t (db->begin ());
+ auto_ptr<object> p (db->load<object> (1));
+
+ assert (p->o1 == 2);
+ assert (p->o2 == 3);
+
+ db->erase<object> (p->o1);
+ db->erase<object> (p->o2);
+
+ 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));
+ assert (p->o1 == 2);
+ assert (p->o2 == 3);
+ t.commit ();
+ }
+ break;
+ }
+ default:
+ {
+ cerr << "unknown pass number '" << argv[argc - 1] << "'" << endl;
+ return 1;
+ }
+ }
+#endif // DATABASE_SQLITE
+ }
+ catch (const odb::exception& e)
+ {
+ cerr << e.what () << endl;
+ return 1;
+ }
+}
diff --git a/odb-tests/evolution/drop-foreign-key/model.hxx b/odb-tests/evolution/drop-foreign-key/model.hxx
new file mode 100644
index 0000000..eed8c46
--- /dev/null
+++ b/odb-tests/evolution/drop-foreign-key/model.hxx
@@ -0,0 +1,50 @@
+// file : evolution/drop-foreign-key/model.hxx
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef MODEL_VERSION
+# error model.hxx included directly
+#endif
+
+#include <string>
+
+#include <odb/core.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 object
+ {
+ #pragma db id
+ unsigned long id_;
+
+ // SQLite doesn't support dropping of foreign keys.
+ //
+#ifndef DATABASE_SQLITE
+#if MODEL_VERSION == 2
+ object* o1;
+ object* o2;
+
+ object (unsigned long id = 0): id_ (id), o1 (0), o2 (0) {}
+ ~object () {delete o1; delete o2;}
+#else
+ #pragma db null
+ unsigned long o1;
+
+ #pragma db null
+ unsigned long o2;
+
+ object (unsigned long id = 0): id_ (id) {}
+#endif
+#endif
+ };
+}
+
+#undef MODEL_NAMESPACE
+#undef MODEL_NAMESPACE_IMPL
diff --git a/odb-tests/evolution/drop-foreign-key/test1.hxx b/odb-tests/evolution/drop-foreign-key/test1.hxx
new file mode 100644
index 0000000..5476796
--- /dev/null
+++ b/odb-tests/evolution/drop-foreign-key/test1.hxx
@@ -0,0 +1,9 @@
+// file : evolution/drop-foreign-key/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/drop-foreign-key/test2.hxx b/odb-tests/evolution/drop-foreign-key/test2.hxx
new file mode 100644
index 0000000..f091a25
--- /dev/null
+++ b/odb-tests/evolution/drop-foreign-key/test2.hxx
@@ -0,0 +1,11 @@
+// file : evolution/drop-foreign-key/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/drop-foreign-key/test3.hxx b/odb-tests/evolution/drop-foreign-key/test3.hxx
new file mode 100644
index 0000000..beb8c42
--- /dev/null
+++ b/odb-tests/evolution/drop-foreign-key/test3.hxx
@@ -0,0 +1,11 @@
+// file : evolution/drop-foreign-key/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