Skip to content

Commit

Permalink
Merge pull request #688 from couchbase/feature/issue_687_schema_version
Browse files Browse the repository at this point in the history
Open db in READWRITE mode when querying the schema version
  • Loading branch information
snej committed May 1, 2015
2 parents 914b06b + 49123da commit ecbd18e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Source/API/CBLManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,13 @@ - (BOOL) upgradeDatabaseNamed: (NSString*)name

- (int) schemaVersionOfSqliteFile: (NSString*)dbPath error: (NSError**)outError {
int version = -1;

sqlite3* sqlite;
// Open the SQLite in the READWRITE mode so that the WAL enabled database from
// the -replaceDatabaseNamed: method can be open regardless of the existence of
// the -wal and -shm file.
int err = sqlite3_open_v2(dbPath.fileSystemRepresentation, &sqlite,
SQLITE_OPEN_READONLY, NULL);
if (err) {
SQLITE_OPEN_READWRITE, NULL);
if (err != SQLITE_OK) {
NSString* errMesg = [NSString stringWithUTF8String:sqlite3_errmsg(sqlite)];
Warn(@"Couldn't open sqlite %@ : %@", dbPath, errMesg);
if (outError) {
Expand All @@ -413,14 +415,15 @@ - (int) schemaVersionOfSqliteFile: (NSString*)dbPath error: (NSError**)outError
}

const char* sql = "PRAGMA user_version";
sqlite3_stmt* versionQuery;
err = sqlite3_prepare_v2(sqlite, sql, -1, &versionQuery, NULL);
if (!err) {
while (SQLITE_ROW == sqlite3_step(versionQuery)) {
sqlite3_stmt* stmt;
err = sqlite3_prepare_v2(sqlite, sql, -1, &stmt, NULL);
if (err == SQLITE_OK) {
if (SQLITE_ROW == sqlite3_step(stmt)) {
@autoreleasepool {
version = sqlite3_column_int(versionQuery, 0);
version = sqlite3_column_int(stmt, 0);
}
}
} else
Warn(@"Couldn't query user_version : %@", dbPath);
} else {
NSString* errMesg = [NSString stringWithUTF8String:sqlite3_errmsg(sqlite)];
Warn(@"Couldn't compile SQL `%s` : %@", sql, errMesg);
Expand All @@ -430,7 +433,7 @@ - (int) schemaVersionOfSqliteFile: (NSString*)dbPath error: (NSError**)outError
}
}

sqlite3_finalize(versionQuery);
sqlite3_finalize(stmt);
sqlite3_close(sqlite);

return version;
Expand Down

0 comments on commit ecbd18e

Please sign in to comment.