Skip to content

Commit cbb8bc2

Browse files
committed
Merge pull request #611 from couchbase/feature/issue_609_prebuilt_database
Fixed #609 - pre-built database
2 parents c6e8d2c + 07220fd commit cbb8bc2

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/main/java/com/couchbase/lite/Database.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,25 @@ public synchronized boolean open() {
11971197
// Android version should be one version higher.
11981198
if (dbVersion < 18) {
11991199
// Version 12: Because of a bug fix that changes JSON collation, invalidate view indexes
1200-
String upgradeSql = "DELETE FROM maps; UPDATE views SET lastsequence=0; " +
1200+
1201+
// instead of delete all rows in maps table, drop table and recreate it.
1202+
String upgradeSql = "DROP TABLE maps";
1203+
if (!initialize(upgradeSql)) {
1204+
return false;
1205+
}
1206+
1207+
upgradeSql = "CREATE TABLE IF NOT EXISTS maps ( " +
1208+
" view_id INTEGER NOT NULL REFERENCES views(view_id) ON DELETE CASCADE, " +
1209+
" sequence INTEGER NOT NULL REFERENCES revs(sequence) ON DELETE CASCADE, " +
1210+
" key TEXT NOT NULL COLLATE JSON, " +
1211+
" value TEXT); " +
1212+
" CREATE INDEX IF NOT EXISTS maps_keys on maps(view_id, key COLLATE JSON); " +
1213+
" CREATE INDEX IF NOT EXISTS maps_sequence ON maps(sequence);";
1214+
if (!initialize(upgradeSql)) {
1215+
return false;
1216+
}
1217+
1218+
upgradeSql = "UPDATE views SET lastsequence=0; " +
12011219
"PRAGMA user_version = 18";
12021220
if (!initialize(upgradeSql)) {
12031221
return false;
@@ -1211,6 +1229,20 @@ public synchronized boolean open() {
12111229
// 1. Creates attachments table if it does not exist.
12121230
// 2. Iterate revs table to populate attachments table.
12131231
if (dbVersion >= 101) {
1232+
1233+
// NOTE: CBL iOS v1.1.0 does not have maps table, Needs to create it if it does not exist.
1234+
String upgradeSql = "CREATE TABLE IF NOT EXISTS maps ( " +
1235+
" view_id INTEGER NOT NULL REFERENCES views(view_id) ON DELETE CASCADE, " +
1236+
" sequence INTEGER NOT NULL REFERENCES revs(sequence) ON DELETE CASCADE, " +
1237+
" key TEXT NOT NULL COLLATE JSON, " +
1238+
" value TEXT); " +
1239+
" CREATE INDEX IF NOT EXISTS maps_keys on maps(view_id, key COLLATE JSON); " +
1240+
" CREATE INDEX IF NOT EXISTS maps_sequence ON maps(sequence);";
1241+
if (!initialize(upgradeSql)) {
1242+
return false;
1243+
}
1244+
1245+
12141246
// Check if attachments table exists. If not, create the table, and iterate revs
12151247
// to populate attachment table
12161248
boolean existsAttachments = false;
@@ -1231,7 +1263,7 @@ public synchronized boolean open() {
12311263

12321264
if (!existsAttachments) {
12331265
// 1. create attachments table
1234-
String upgradeSql = "CREATE TABLE attachments ( " +
1266+
upgradeSql = "CREATE TABLE attachments ( " +
12351267
"sequence INTEGER NOT NULL REFERENCES revs(sequence) ON DELETE CASCADE, " +
12361268
"filename TEXT NOT NULL, " +
12371269
"key BLOB NOT NULL, " +

src/main/java/com/couchbase/lite/Manager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public void replaceDatabase(String databaseName, InputStream databaseStream, Map
284284

285285
private void replaceDatabase(String databaseName, InputStream databaseStream, Iterator<Map.Entry<String, InputStream>> attachmentStreams) throws CouchbaseLiteException {
286286
try {
287-
Database database = getDatabase(databaseName);
287+
//Database database = getDatabase(databaseName);
288+
Database database = getDatabaseWithoutOpening(databaseName, false);
288289
String dstAttachmentsPath = database.getAttachmentStorePath();
289290
OutputStream destStream = new FileOutputStream(new File(database.getPath()));
290291
StreamUtils.copyStream(databaseStream, destStream);

0 commit comments

Comments
 (0)