public bool Open() { if (open) { return true; } // Create the storage engine. database = SQLiteStorageEngineFactory.CreateStorageEngine(); // Try to open the storage engine and stop if we fail. if (database == null || !database.Open(path)) { string msg = "Unable to create a storage engine, fatal error"; Log.E(Database.Tag, msg); throw new InvalidOperationException(msg); } // Stuff we need to initialize every time the sqliteDb opens: if (!Initialize("PRAGMA foreign_keys = ON;")) { Log.E(Database.Tag, "Error turning on foreign keys"); return false; } // Check the user_version number we last stored in the sqliteDb: int dbVersion = database.GetVersion(); // Incompatible version changes increment the hundreds' place: if (dbVersion >= 100) { Log.W(Database.Tag, "Database: Database version (" + dbVersion + ") is newer than I know how to work with" ); database.Close(); return false; } if (dbVersion < 1) { // First-time initialization: // (Note: Declaring revs.sequence as AUTOINCREMENT means the values will always be // monotonically increasing, never reused. See <http://www.sqlite.org/autoinc.html>) if (!Initialize(Schema)) { database.Close(); return false; } dbVersion = 3; } if (dbVersion < 2) { // Version 2: added attachments.revpos string upgradeSql = "ALTER TABLE attachments ADD COLUMN revpos INTEGER DEFAULT 0; " + "PRAGMA user_version = 2"; if (!Initialize(upgradeSql)) { database.Close(); return false; } dbVersion = 2; } if (dbVersion < 3) { string upgradeSql = "CREATE TABLE localdocs ( " + "docid TEXT UNIQUE NOT NULL, " + "revid TEXT NOT NULL, " + "json BLOB); " + "CREATE INDEX localdocs_by_docid ON localdocs(docid); " + "PRAGMA user_version = 3"; if (!Initialize(upgradeSql)) { database.Close(); return false; } dbVersion = 3; } if (dbVersion < 4) { string upgradeSql = "CREATE TABLE info ( " + "key TEXT PRIMARY KEY, " + "value TEXT); " + "INSERT INTO INFO (key, value) VALUES ('privateUUID', '" + Misc.TDCreateUUID( ) + "'); " + "INSERT INTO INFO (key, value) VALUES ('publicUUID', '" + Misc.TDCreateUUID () + "'); " + "PRAGMA user_version = 4"; if (!Initialize(upgradeSql)) { database.Close(); return false; } } try { attachments = new BlobStore(GetAttachmentStorePath()); } catch (ArgumentException e) { Log.E(Database.Tag, "Could not initialize attachment store", e); database.Close(); return false; } open = true; return true; }