From b0c2c30dd0b83ea67fb2c8375ae2061dad0d1770 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 9 Apr 2013 16:17:27 +0200 Subject: Add support for embedded schema migration --- evolution/combined/driver.cxx | 54 +++++++++++++++++++++++++++++++++++-------- evolution/combined/makefile | 9 ++++---- evolution/combined/model.hxx | 33 +++++++++++++++++++++----- 3 files changed, 77 insertions(+), 19 deletions(-) (limited to 'evolution/combined') diff --git a/evolution/combined/driver.cxx b/evolution/combined/driver.cxx index c87b14e..9cb2853 100644 --- a/evolution/combined/driver.cxx +++ b/evolution/combined/driver.cxx @@ -11,6 +11,7 @@ #include #include +#include #include @@ -27,7 +28,8 @@ main (int argc, char* argv[]) { try { - auto_ptr db (create_database (argc, argv)); + auto_ptr db (create_database (argc, argv, false)); + bool embedded (schema_catalog::exists (*db, "test2")); // 1 - base version // 2 - migration @@ -41,21 +43,35 @@ 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"); o.dui = 1; - o.dfk = new object1 (1); - o.acn = 1; o.anui = 1; o.dnui = 1; o.dt.push_back (1); + o.aui = 1; + +#ifndef DATABASE_SQLITE + o.dfk = new object1 (1); + o.acn = 1; o.dc = 1; o.acnn.reset (); o.afk = 1; - o.aui = 1; +#endif { transaction t (db->begin ()); +#ifndef DATABASE_SQLITE db->persist (o.dfk); +#endif db->persist (o); t.commit (); } @@ -65,26 +81,44 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + { transaction t (db->begin ()); auto_ptr p (db->load ("1")); assert (p->ac1 == 999); assert (!p->ac2); - assert (!p->ac3); +#ifndef DATABASE_SQLITE + assert (!p->ac3); +#endif // Migrate. // - p->dfk = 999; p->at.push_back ("abc"); + +#ifndef DATABASE_SQLITE + p->dfk = 999; p->ac3 = 1; p->acn.reset (); p->acnn = 1; - +#endif db->update (*p); t.commit (); } + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_post (*db, 3, "test2"); + t.commit (); + } break; } case 3: @@ -97,12 +131,14 @@ main (int argc, char* argv[]) // Check post-migration. // - assert (p->dfk == 999); assert (p->at[0] == "abc"); + +#ifndef DATABASE_SQLITE + assert (p->dfk == 999); assert (*p->ac3 == 1); assert (!p->acn); assert (*p->acnn == 1); - +#endif t.commit (); } break; diff --git a/evolution/combined/makefile b/evolution/combined/makefile index 493f022..a489287 100644 --- a/evolution/combined/makefile +++ b/evolution/combined/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) @@ -113,7 +114,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/combined/model.hxx b/evolution/combined/model.hxx index 2aede90..2cdc9d7 100644 --- a/evolution/combined/model.hxx +++ b/evolution/combined/model.hxx @@ -12,6 +12,8 @@ #include #include +#include // DATABASE_XXX + #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -45,14 +47,16 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #endif unsigned long dui; - // Alter table drop foreign key. + // Alter table drop foreign key. Not supported by SQLite. // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 object1* dfk; #else #pragma db null unsigned long dfk; #endif +#endif // Add table. // @@ -68,17 +72,23 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) odb::nullable ac2; - #pragma db not_null // Initially added as NULL, converted to NOT NULL. + // Initially added as NULL, converted to NOT NULL. Not supported by SQLite. + // +#ifndef DATABASE_SQLITE + #pragma db not_null odb::nullable ac3; #endif +#endif - // Alter column NULL. + // Alter column NULL. Not supported by SQLite. // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 unsigned long acn; #else odb::nullable acn; #endif +#endif // // Pre pass 2. @@ -112,27 +122,33 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) std::vector dt; #endif - // Drop column. + // Drop column. Not supported by SQLite. // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 unsigned long dc; #endif +#endif - // Alter column NOT NULL. + // Alter column NOT NULL. Not supported by SQLite. // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 3 #pragma db not_null #endif odb::nullable acnn; +#endif - // Alter table add foreign key. + // Alter table add foreign key. Not supported by SQLite. // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 #pragma db null unsigned long afk; #else object1* afk; #endif +#endif // Add unique index. // @@ -142,13 +158,18 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) unsigned long aui; public: +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 + object (std::string id = ""): id_ (id), dfk (0) {} ~object () {delete dfk;} #else object (std::string id = ""): id_ (id), afk (0) {} ~object () {delete afk;} #endif +#else + object (std::string id = ""): id_ (id) {} +#endif }; } -- cgit v1.1