diff options
Diffstat (limited to 'odb-tests/evolution/alter-column')
-rw-r--r-- | odb-tests/evolution/alter-column/buildfile | 63 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/driver.cxx | 164 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/model.hxx | 56 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/test1.hxx | 9 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/test2.hxx | 11 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/test3.hxx | 11 | ||||
-rw-r--r-- | odb-tests/evolution/alter-column/testscript | 59 |
7 files changed, 373 insertions, 0 deletions
diff --git a/odb-tests/evolution/alter-column/buildfile b/odb-tests/evolution/alter-column/buildfile new file mode 100644 index 0000000..08b0ed7 --- /dev/null +++ b/odb-tests/evolution/alter-column/buildfile @@ -0,0 +1,63 @@ +# file : evolution/alter-column/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_alter_c_ \ + --schema-version-table evo_alter_c_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/alter-column/driver.cxx b/odb-tests/evolution/alter-column/driver.cxx new file mode 100644 index 0000000..cf7cbc3 --- /dev/null +++ b/odb-tests/evolution/alter-column/driver.cxx @@ -0,0 +1,164 @@ +// file : evolution/alter-column/driver.cxx +// license : GNU GPL v2; see accompanying LICENSE file + +// Test altering a column. +// + +#include <memory> // std::unique_ptr +#include <iostream> + +#include <odb/database.hxx> +#include <odb/transaction.hxx> +#include <odb/schema-catalog.hxx> + +#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_alter_c_sv")); + + // SQLite doesn't support altering of columns. + // +#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.num = 123; + + { + transaction t (db->begin ()); + 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 (); + } + + // NULL is already in effect; NOT NULL is not yet. + // + { + transaction t (db->begin ()); + unique_ptr<object> p (db->load<object> (1)); + + assert (!p->str); + assert (p->num && *p->num == 123); + assert (!p->num1); + + // Migration. + // + p->str = "abc"; + p->num.reset (); + p->num1 = 123; + 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 ()); + unique_ptr<object> p (db->load<object> (1)); + + assert (p->str && *p->str == "abc"); + assert (!p->num); + + t.commit (); + } + + // NOT NULL is now in effect. + // + try + { + object o2 (2); + o2.num1 = 234; // str is NULL + + transaction t (db->begin ()); + db->persist (o2); + assert (false); + } + catch (const odb::exception& ) {} + + try + { + object o3 (3); + o3.str = "bcd"; // num1 is NULL + + transaction t (db->begin ()); + db->persist (o3); + assert (false); + } + catch (const odb::exception& ) {} + + 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/alter-column/model.hxx b/odb-tests/evolution/alter-column/model.hxx new file mode 100644 index 0000000..8378887 --- /dev/null +++ b/odb-tests/evolution/alter-column/model.hxx @@ -0,0 +1,56 @@ +// file : evolution/alter-column/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 <odb/nullable.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) +{ + #pragma db object + struct object + { + object (unsigned long id = 0): id_ (id) {} + + #pragma db id + unsigned long id_; + + // SQLite doesn't support altering of columns. + // +#ifndef DATABASE_SQLITE +#if MODEL_VERSION == 2 + odb::nullable<std::string> str; + + unsigned long num; +#else + // Use nullable to be able to access during migration. + // + #pragma db not_null + odb::nullable<std::string> str; + + odb::nullable<unsigned long> num; + + // Test adding NOT NULL column. It should be added NULL in pre + // and then converted to NOT NULL in post. + // + #pragma db not_null + odb::nullable<unsigned long> num1; +#endif +#endif + }; +} + +#undef MODEL_NAMESPACE +#undef MODEL_NAMESPACE_IMPL diff --git a/odb-tests/evolution/alter-column/test1.hxx b/odb-tests/evolution/alter-column/test1.hxx new file mode 100644 index 0000000..9353558 --- /dev/null +++ b/odb-tests/evolution/alter-column/test1.hxx @@ -0,0 +1,9 @@ +// file : evolution/alter-column/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/alter-column/test2.hxx b/odb-tests/evolution/alter-column/test2.hxx new file mode 100644 index 0000000..e2dbed3 --- /dev/null +++ b/odb-tests/evolution/alter-column/test2.hxx @@ -0,0 +1,11 @@ +// file : evolution/alter-column/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/alter-column/test3.hxx b/odb-tests/evolution/alter-column/test3.hxx new file mode 100644 index 0000000..ac08e2f --- /dev/null +++ b/odb-tests/evolution/alter-column/test3.hxx @@ -0,0 +1,11 @@ +// file : evolution/alter-column/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/alter-column/testscript b/odb-tests/evolution/alter-column/testscript new file mode 100644 index 0000000..ff8f9a6 --- /dev/null +++ b/odb-tests/evolution/alter-column/testscript @@ -0,0 +1,59 @@ +# file : evolution/alter-column/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 +{ + # Note that SQLite doesn't support altering of columns and so the driver is + # trivial. We, however, still run it once for good measure. + # + $* 1 +} |