From ab0fb8ac7e097dcdd3111dc2865f0de14ab27118 Mon Sep 17 00:00:00 2001
From: Boris Kolpackov <boris@codesynthesis.com>
Date: Mon, 22 Aug 2011 10:47:30 +0200
Subject: Enable foreign key constraints checking in SQLite

Due to bugs in SQLite DDL foreign key support, we have to temporarily
disable foreign keys when re-creating the schema. New manual section:
12.5.3, "Foreign Key Constraints".
---
 libcommon/common/common.cxx | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

(limited to 'libcommon')

diff --git a/libcommon/common/common.cxx b/libcommon/common/common.cxx
index 37f8424..47dee81 100644
--- a/libcommon/common/common.cxx
+++ b/libcommon/common/common.cxx
@@ -14,6 +14,7 @@
 #  include <odb/mysql/database.hxx>
 #  include <odb/mysql/connection-factory.hxx>
 #elif defined(DATABASE_SQLITE)
+#  include <odb/connection.hxx>
 #  include <odb/transaction.hxx>
 #  include <odb/schema-catalog.hxx>
 #  include <odb/sqlite/database.hxx>
@@ -81,15 +82,23 @@ create_database (int& argc,
 
   db.reset (
     new sqlite::database (
-      argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, f));
+      argc, argv, false, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, true, f));
 
-  // Create the database schema.
+  // Create the database schema. Due to bugs in SQLite foreign key
+  // support for DDL statements, we need to temporarily disable
+  // foreign keys.
   //
   if (schema)
   {
-    transaction t (db->begin ());
+    connection_ptr c (db->connection ());
+
+    c->execute ("PRAGMA foreign_keys=OFF");
+
+    transaction t (c->begin ());
     schema_catalog::create_schema (*db);
     t.commit ();
+
+    c->execute ("PRAGMA foreign_keys=ON");
   }
 #elif defined(DATABASE_PGSQL)
   auto_ptr<pgsql::connection_factory> f;
-- 
cgit v1.1