aboutsummaryrefslogtreecommitdiff
path: root/evolution/add-foreign-key
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2013-04-09 16:17:27 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2013-04-10 18:46:47 +0200
commitb0c2c30dd0b83ea67fb2c8375ae2061dad0d1770 (patch)
treeb1fbada48dab47c2477c6016a45cfac957670346 /evolution/add-foreign-key
parent7d1e16930e37c4109f439f5ebe1e789b9619a57e (diff)
Add support for embedded schema migration
Diffstat (limited to 'evolution/add-foreign-key')
-rw-r--r--evolution/add-foreign-key/driver.cxx36
-rw-r--r--evolution/add-foreign-key/makefile9
-rw-r--r--evolution/add-foreign-key/model.hxx4
3 files changed, 44 insertions, 5 deletions
diff --git a/evolution/add-foreign-key/driver.cxx b/evolution/add-foreign-key/driver.cxx
index 0d62889..26c1f57 100644
--- a/evolution/add-foreign-key/driver.cxx
+++ b/evolution/add-foreign-key/driver.cxx
@@ -11,7 +11,9 @@
#include <odb/database.hxx>
#include <odb/transaction.hxx>
+#include <odb/schema-catalog.hxx>
+#include <common/config.hxx> // DATABASE_XXX
#include <common/common.hxx>
#include "test1.hxx"
@@ -27,7 +29,8 @@ main (int argc, char* argv[])
{
try
{
- auto_ptr<database> db (create_database (argc, argv));
+ auto_ptr<database> db (create_database (argc, argv, false));
+ bool embedded (schema_catalog::exists (*db, "test2"));
// 1 - base version
// 2 - migration
@@ -41,6 +44,15 @@ main (int argc, char* argv[])
{
using namespace v2;
+ if (embedded)
+ {
+ transaction t (db->begin ());
+ schema_catalog::create_schema (*db, "test2");
+ schema_catalog::create_schema (*db, "test1");
+ schema_catalog::migrate_schema (*db, 2, "test2");
+ t.commit ();
+ }
+
object o (1);
{
@@ -54,6 +66,20 @@ main (int argc, char* argv[])
{
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, "test2");
+ t.commit ();
+ }
+
// Both pointers are now NULL.
//
{
@@ -83,6 +109,12 @@ main (int argc, char* argv[])
t.commit ();
}
+ if (embedded)
+ {
+ transaction t (db->begin ());
+ schema_catalog::migrate_schema_post (*db, 3, "test2");
+ t.commit ();
+ }
break;
}
case 3:
@@ -113,6 +145,7 @@ main (int argc, char* argv[])
// As well as the NOT NULL.
//
+#ifndef DATABASE_SQLITE
try
{
object o (3);
@@ -123,6 +156,7 @@ main (int argc, char* argv[])
assert (false);
}
catch (const odb::exception& ) {}
+#endif
break;
}
default:
diff --git a/evolution/add-foreign-key/makefile b/evolution/add-foreign-key/makefile
index d06a50e..165cf37 100644
--- a/evolution/add-foreign-key/makefile
+++ b/evolution/add-foreign-key/makefile
@@ -29,15 +29,16 @@ $(driver): $(cxx_obj) $(common.l)
$(cxx_obj) $(cxx_od): cpp_options := -I$(out_base) -I$(src_base)
$(cxx_obj) $(cxx_od): $(common.l.cpp-options)
-genf1 := test1-odb.hxx test1-odb.ixx test1-odb.cxx test1.sql model.xml
+genf1 := test1-odb.hxx test1-odb.ixx test1-odb.cxx model.xml
gen1 := $(addprefix $(out_base)/,$(genf1))
-genf2 := test2-odb.hxx test2-odb.ixx test2-odb.cxx test2.sql \
-test2-002-pre.sql test2-002-post.sql test2-003-pre.sql test2-003-post.sql
+genf2 := test2-odb.hxx test2-odb.ixx test2-odb.cxx
gen2 := $(addprefix $(out_base)/,$(genf2))
genf := $(genf1) $(genf2)
gen := $(gen1) $(gen2)
+gens := test1.sql test2.sql test2-002-pre.sql test2-002-post.sql \
+test2-003-pre.sql test2-003-post.sql
$(gen): $(odb)
$(gen): odb := $(odb)
@@ -114,7 +115,7 @@ $(clean): \
ifeq ($(out_base),$(src_base))
$(driver): | $(out_base)/.gitignore
-$(out_base)/.gitignore: files := driver $(genf)
+$(out_base)/.gitignore: files := driver $(genf) $(gens)
$(clean): $(out_base)/.gitignore.clean
$(call include,$(bld_root)/git/gitignore.make)
diff --git a/evolution/add-foreign-key/model.hxx b/evolution/add-foreign-key/model.hxx
index e73e47a..6b116e9 100644
--- a/evolution/add-foreign-key/model.hxx
+++ b/evolution/add-foreign-key/model.hxx
@@ -48,7 +48,11 @@ namespace MODEL_NAMESPACE(MODEL_VERSION)
#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) {}