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/add-column/driver.cxx | 27 +++++++++++++++++- evolution/add-column/makefile | 9 +++--- evolution/add-foreign-key/driver.cxx | 36 ++++++++++++++++++++++- evolution/add-foreign-key/makefile | 9 +++--- evolution/add-foreign-key/model.hxx | 4 +++ evolution/add-index/driver.cxx | 26 ++++++++++++++++- evolution/add-index/makefile | 9 +++--- evolution/add-table/driver.cxx | 26 ++++++++++++++++- evolution/add-table/makefile | 9 +++--- evolution/alter-column/driver.cxx | 32 ++++++++++++++++++++- evolution/alter-column/makefile | 9 +++--- evolution/alter-column/model.hxx | 6 ++++ evolution/combined/driver.cxx | 54 +++++++++++++++++++++++++++++------ evolution/combined/makefile | 9 +++--- evolution/combined/model.hxx | 33 +++++++++++++++++---- evolution/drop-column/driver.cxx | 32 ++++++++++++++++++++- evolution/drop-column/makefile | 9 +++--- evolution/drop-column/model.hxx | 6 ++++ evolution/drop-foreign-key/driver.cxx | 32 ++++++++++++++++++++- evolution/drop-foreign-key/makefile | 9 +++--- evolution/drop-foreign-key/model.hxx | 6 ++++ evolution/drop-index/driver.cxx | 26 ++++++++++++++++- evolution/drop-index/makefile | 9 +++--- evolution/drop-table/driver.cxx | 26 ++++++++++++++++- evolution/drop-table/makefile | 9 +++--- evolution/template/driver.cxx | 27 +++++++++++++++++- evolution/template/makefile | 9 +++--- 27 files changed, 429 insertions(+), 69 deletions(-) (limited to 'evolution') diff --git a/evolution/add-column/driver.cxx b/evolution/add-column/driver.cxx index 85a10f4..3503010 100644 --- a/evolution/add-column/driver.cxx +++ b/evolution/add-column/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,6 +43,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 +65,13 @@ 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)); @@ -69,6 +87,13 @@ 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: diff --git a/evolution/add-column/makefile b/evolution/add-column/makefile index f6dd0f3..5e64a45 100644 --- a/evolution/add-column/makefile +++ b/evolution/add-column/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/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 #include +#include +#include // DATABASE_XXX #include #include "test1.hxx" @@ -27,7 +29,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,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) {} diff --git a/evolution/add-index/driver.cxx b/evolution/add-index/driver.cxx index e4a6e17..ba9f837 100644 --- a/evolution/add-index/driver.cxx +++ b/evolution/add-index/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,6 +43,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 o0 (0); o0.num = 123; @@ -65,6 +76,13 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + object o3 (3); o3.num = 234; @@ -97,6 +115,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: diff --git a/evolution/add-index/makefile b/evolution/add-index/makefile index 32e2e04..1fe6f0a 100644 --- a/evolution/add-index/makefile +++ b/evolution/add-index/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/add-table/driver.cxx b/evolution/add-table/driver.cxx index 7e9b2ad..d64e991 100644 --- a/evolution/add-table/driver.cxx +++ b/evolution/add-table/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,6 +43,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); o.str = "abc"; @@ -55,6 +66,13 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + object1 o1; o1.nums.push_back (1); o1.nums.push_back (2); @@ -67,6 +85,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: diff --git a/evolution/add-table/makefile b/evolution/add-table/makefile index 76cb897..a645e7d 100644 --- a/evolution/add-table/makefile +++ b/evolution/add-table/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/alter-column/driver.cxx b/evolution/alter-column/driver.cxx index 193289f..976f539 100644 --- a/evolution/alter-column/driver.cxx +++ b/evolution/alter-column/driver.cxx @@ -11,6 +11,7 @@ #include #include +#include #include @@ -27,7 +28,12 @@ main (int argc, char* argv[]) { try { - auto_ptr db (create_database (argc, argv)); + auto_ptr db (create_database (argc, argv, false)); + + // SQLite doesn't support altering of columns. + // +#ifndef DATABASE_SQLITE + bool embedded (schema_catalog::exists (*db, "test2")); // 1 - base version // 2 - migration @@ -41,6 +47,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); o.num = 123; @@ -55,6 +70,13 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + // NULL is already in effect; NOT NULL is not yet. // { @@ -74,6 +96,13 @@ 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: @@ -122,6 +151,7 @@ main (int argc, char* argv[]) return 1; } } +#endif // DATABASE_SQLITE } catch (const odb::exception& e) { diff --git a/evolution/alter-column/makefile b/evolution/alter-column/makefile index fffbef8..de0d3e0 100644 --- a/evolution/alter-column/makefile +++ b/evolution/alter-column/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/alter-column/model.hxx b/evolution/alter-column/model.hxx index 0b31e1c..a9a3aeb 100644 --- a/evolution/alter-column/model.hxx +++ b/evolution/alter-column/model.hxx @@ -11,6 +11,8 @@ #include #include +#include // DATABASE_XXX + #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -26,6 +28,9 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #pragma db id unsigned long id_; + // SQLite doesn't support altering of columns. + // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 odb::nullable str; @@ -44,6 +49,7 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #pragma db not_null odb::nullable num1; #endif +#endif }; } 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 }; } diff --git a/evolution/drop-column/driver.cxx b/evolution/drop-column/driver.cxx index 02d453c..2fddf3a 100644 --- a/evolution/drop-column/driver.cxx +++ b/evolution/drop-column/driver.cxx @@ -11,6 +11,7 @@ #include #include +#include #include @@ -27,7 +28,12 @@ main (int argc, char* argv[]) { try { - auto_ptr db (create_database (argc, argv)); + auto_ptr db (create_database (argc, argv, false)); + + // SQLite doesn't support dropping of columns. + // +#ifndef DATABASE_SQLITE + bool embedded (schema_catalog::exists (*db, "test2")); // 1 - base version // 2 - migration @@ -41,6 +47,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); o.str = "abc"; o.num = 123; @@ -56,6 +71,13 @@ main (int argc, char* argv[]) { using namespace v2; // @@ soft delete + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + // Things are still there. // { @@ -67,6 +89,13 @@ 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: @@ -88,6 +117,7 @@ main (int argc, char* argv[]) return 1; } } +#endif // DATABASE_SQLITE } catch (const odb::exception& e) { diff --git a/evolution/drop-column/makefile b/evolution/drop-column/makefile index af51386..6bc42fd 100644 --- a/evolution/drop-column/makefile +++ b/evolution/drop-column/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/drop-column/model.hxx b/evolution/drop-column/model.hxx index 0c54252..42a8f54 100644 --- a/evolution/drop-column/model.hxx +++ b/evolution/drop-column/model.hxx @@ -11,6 +11,8 @@ #include #include +#include // DATABASE_XXX + #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -26,10 +28,14 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #pragma db id unsigned long id_; + // SQLite doesn't support dropping of columns. + // +#ifndef DATABASE_SQLITE #if MODEL_VERSION == 2 std::string str; unsigned long num; #endif +#endif }; } diff --git a/evolution/drop-foreign-key/driver.cxx b/evolution/drop-foreign-key/driver.cxx index a63886b..8fe332b 100644 --- a/evolution/drop-foreign-key/driver.cxx +++ b/evolution/drop-foreign-key/driver.cxx @@ -11,6 +11,7 @@ #include #include +#include #include @@ -27,7 +28,12 @@ main (int argc, char* argv[]) { try { - auto_ptr db (create_database (argc, argv)); + auto_ptr db (create_database (argc, argv, false)); + + // SQLite doesn't support dropping of foreign keys. + // +#ifndef DATABASE_SQLITE + bool embedded (schema_catalog::exists (*db, "test2")); // 1 - base version // 2 - migration @@ -41,6 +47,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); o.o1 = new object (2); o.o2 = new object (3); @@ -72,6 +87,13 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + // The data is still there but the constraints are gone. // { @@ -86,6 +108,13 @@ 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: @@ -107,6 +136,7 @@ main (int argc, char* argv[]) return 1; } } +#endif // DATABASE_SQLITE } catch (const odb::exception& e) { diff --git a/evolution/drop-foreign-key/makefile b/evolution/drop-foreign-key/makefile index 97509a6..d83546b 100644 --- a/evolution/drop-foreign-key/makefile +++ b/evolution/drop-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/drop-foreign-key/model.hxx b/evolution/drop-foreign-key/model.hxx index 4f41329..b58aa92 100644 --- a/evolution/drop-foreign-key/model.hxx +++ b/evolution/drop-foreign-key/model.hxx @@ -10,6 +10,8 @@ #include +#include // DATABASE_XXX + #pragma db model version(1, MODEL_VERSION) #define MODEL_NAMESPACE_IMPL(V) v##V @@ -23,6 +25,9 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) #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; @@ -38,6 +43,7 @@ namespace MODEL_NAMESPACE(MODEL_VERSION) object (unsigned long id = 0): id_ (id) {} #endif +#endif }; } diff --git a/evolution/drop-index/driver.cxx b/evolution/drop-index/driver.cxx index 0b85164..f0740ef 100644 --- a/evolution/drop-index/driver.cxx +++ b/evolution/drop-index/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,6 +43,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 o0 (0); o0.num = 123; @@ -73,6 +84,13 @@ main (int argc, char* argv[]) { using namespace v3; + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + // Duplicates are now ok. // object o2 (2); @@ -84,6 +102,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: diff --git a/evolution/drop-index/makefile b/evolution/drop-index/makefile index e96b66d..fc45fdb 100644 --- a/evolution/drop-index/makefile +++ b/evolution/drop-index/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/drop-table/driver.cxx b/evolution/drop-table/driver.cxx index 2a48ed5..6ab29f0 100644 --- a/evolution/drop-table/driver.cxx +++ b/evolution/drop-table/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,6 +43,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 (); + } + object1 o1; o1.o = new object (1); o1.o->str = "abc"; @@ -60,6 +71,13 @@ main (int argc, char* argv[]) { using namespace v2; // @@ v3; soft immediate drop + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } + // Both object and object1 are still there so we can migrate the data. // typedef odb::query query; @@ -79,6 +97,12 @@ main (int argc, char* argv[]) t.commit (); } + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_pre (*db, 3, "test2"); + t.commit (); + } break; } case 3: diff --git a/evolution/drop-table/makefile b/evolution/drop-table/makefile index 091a679..a08f0a1 100644 --- a/evolution/drop-table/makefile +++ b/evolution/drop-table/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/template/driver.cxx b/evolution/template/driver.cxx index 1e2ad90..357055d 100644 --- a/evolution/template/driver.cxx +++ b/evolution/template/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,6 +43,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 (); + } + { transaction t (db->begin ()); dummy d (1); @@ -53,11 +64,25 @@ 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)); t.commit (); } + + if (embedded) + { + transaction t (db->begin ()); + schema_catalog::migrate_schema_post (*db, 3, "test2"); + t.commit (); + } break; } case 3: diff --git a/evolution/template/makefile b/evolution/template/makefile index a914292..c3fc5f7 100644 --- a/evolution/template/makefile +++ b/evolution/template/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) -- cgit v1.1