diff options
Diffstat (limited to 'odb-tests/evolution/add-foreign-key')
-rw-r--r-- | odb-tests/evolution/add-foreign-key/buildfile | 64 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/driver.cxx | 178 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/model.hxx | 66 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/test1.hxx | 9 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/test2.hxx | 11 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/test3.hxx | 11 | ||||
-rw-r--r-- | odb-tests/evolution/add-foreign-key/testscript | 58 |
7 files changed, 397 insertions, 0 deletions
diff --git a/odb-tests/evolution/add-foreign-key/buildfile b/odb-tests/evolution/add-foreign-key/buildfile new file mode 100644 index 0000000..0fe5468 --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/buildfile @@ -0,0 +1,64 @@ +# file : evolution/add-foreign-key/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 libodb_db = libodb-$db%lib{odb-$db} +import libcommon = lib{common} + +hdrs = test1 test2 test3 + +exe{driver}: {hxx cxx}{* -*-odb} testscript + +# Introduce the metadata library target to make sure the libodb and libcommon +# libraries are resolved for the odb_compile ad hoc rule (see build/root.build +# for details). +# +libue{test-meta}: $libodb $libcommon + +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} $libodb_db + +# 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_add_fk_ \ + --schema-version-table evo_add_fk_sv \ + --generate-schema \ + --generate-query \ + --at-once \ + --changelog $out_base/model.xml \ + --fkeys-deferrable-mode not_deferrable + +<{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/add-foreign-key/driver.cxx b/odb-tests/evolution/add-foreign-key/driver.cxx new file mode 100644 index 0000000..735166f --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/driver.cxx @@ -0,0 +1,178 @@ +// file : evolution/add-foreign-key/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test adding a foreign key. +// + +#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; + +int +main (int argc, char* argv[]) +{ + try + { + unique_ptr<database> db (create_database (argc, argv, false)); + + db->schema_version_table (quote_name ("evo_add_fk_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 (); + } + + object o (1); + + { + transaction t (db->begin ()); + db->persist (o); + t.commit (); + } + break; + } + case 2: + { + using namespace v3; + +#ifdef DATABASE_SQLITE + // In SQLite we can only add foreign keys inline in the column + // definition. + // + db->connection ()->execute ("PRAGMA foreign_keys=OFF"); +#endif + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3); + t.commit (); + } + + // Both pointers are now NULL. + // + { + transaction t (db->begin ()); + unique_ptr<object> p (db->load<object> (1)); + + assert (p->o1 == 0 && p->o2 == 0); + + // Migration. The foreign key constraint is not yet there. + // + p->o1 = new object1 (1); + p->o2 = new object2 (1); + db->update (*p); + + t.commit (); + } + + // Migration. Add the missing objects. + // + object1 o1 (1); + object2 o2 (1); + + { + transaction t (db->begin ()); + db->persist (o1); + db->persist (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 ()); + unique_ptr<object> p (db->load<object> (1)); + assert (p->o1->id_ == 1); + assert (p->o2->id_ == 1); + t.commit (); + } + + // Now the foreign key constraint is there. + // + try + { + object o (2); + o.o1 = new object1 (2); + o.o2 = new object2 (2); + + transaction t (db->begin ()); + db->persist (o); + assert (false); + } + catch (const odb::exception& ) {} + + // As well as the NOT NULL. + // +#ifndef DATABASE_SQLITE + try + { + object o (3); + o.o2 = 0; + + transaction t (db->begin ()); + db->persist (o); + assert (false); + } + catch (const odb::exception& ) {} +#endif + 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/add-foreign-key/model.hxx b/odb-tests/evolution/add-foreign-key/model.hxx new file mode 100644 index 0000000..17cd85f --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/model.hxx @@ -0,0 +1,66 @@ +// file : evolution/add-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 <libcommon/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) +{ +#if MODEL_VERSION == 3 + #pragma db object + struct object1 + { + object1 (unsigned long id = 0): id_ (id) {} + + #pragma db id + unsigned long id_; + }; + + #pragma db object + struct object2 + { + object2 (unsigned long id = 0): id_ (id) {} + + #pragma db id + unsigned long id_; + }; +#endif + + #pragma db object + struct object + { + #pragma db id + unsigned long id_; + +#if MODEL_VERSION == 2 + object (unsigned long id = 0): id_ (id) {} +#else + object1* o1; + + // There is no support for changing a column to NOT NULL in SQLite. + // +#ifndef ODB_DATABASE_SQLITE + #pragma db not_null +#endif + object2* o2; + + object (unsigned long id = 0): id_ (id), o1 (0), o2 (0) {} + ~object () {delete o1; delete o2;} +#endif + }; +} + +#undef MODEL_NAMESPACE +#undef MODEL_NAMESPACE_IMPL diff --git a/odb-tests/evolution/add-foreign-key/test1.hxx b/odb-tests/evolution/add-foreign-key/test1.hxx new file mode 100644 index 0000000..05c78c3 --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/test1.hxx @@ -0,0 +1,9 @@ +// file : evolution/add-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/add-foreign-key/test2.hxx b/odb-tests/evolution/add-foreign-key/test2.hxx new file mode 100644 index 0000000..c57d9a1 --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/test2.hxx @@ -0,0 +1,11 @@ +// file : evolution/add-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/add-foreign-key/test3.hxx b/odb-tests/evolution/add-foreign-key/test3.hxx new file mode 100644 index 0000000..c844469 --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/test3.hxx @@ -0,0 +1,11 @@ +// file : evolution/add-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 diff --git a/odb-tests/evolution/add-foreign-key/testscript b/odb-tests/evolution/add-foreign-key/testscript new file mode 100644 index 0000000..9ea393f --- /dev/null +++ b/odb-tests/evolution/add-foreign-key/testscript @@ -0,0 +1,58 @@ +# file : evolution/add-foreign-key/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 +} |