|
| 1 | +/* |
| 2 | + * Copyright © 2017 IBM Corp. All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | + * except in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 11 | + * either express or implied. See the License for the specific language governing permissions |
| 12 | + * and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.cloudant.sync.internal.query; |
| 16 | + |
| 17 | +import static org.junit.Assert.assertEquals; |
| 18 | + |
| 19 | +import com.cloudant.sync.internal.documentstore.Database200MigrationTest; |
| 20 | +import com.cloudant.sync.internal.documentstore.migrations.SchemaOnlyMigration; |
| 21 | +import com.cloudant.sync.internal.sqlite.SQLCallable; |
| 22 | +import com.cloudant.sync.internal.sqlite.SQLDatabase; |
| 23 | +import com.cloudant.sync.internal.sqlite.SQLDatabaseFactory; |
| 24 | +import com.cloudant.sync.query.FieldSort; |
| 25 | + |
| 26 | +import org.junit.Before; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.concurrent.Future; |
| 31 | + |
| 32 | +public class Index2MigrationTest extends AbstractIndexTestBase { |
| 33 | + |
| 34 | + /** |
| 35 | + * Version 2 of the index includes an additional index_settings column, which defaults to a null |
| 36 | + * value. This method creates a version 2 index and then moves the table to a temporary table. |
| 37 | + * It rolls the index database version back to 0 and steps forward the schema versions; creating |
| 38 | + * a version 1 index table. It then copies the created index metadata (excluding the new column) |
| 39 | + * from the temporary table into the version 1 table having the effect of creating the required |
| 40 | + * index with version metadata. |
| 41 | + * |
| 42 | + * @throws Exception |
| 43 | + * @see Database200MigrationTest#setUp() |
| 44 | + */ |
| 45 | + @Before |
| 46 | + public void createIndexAndRollback() throws Exception { |
| 47 | + |
| 48 | + // Create an index |
| 49 | + String indexName = im.createJsonIndex(Arrays.<FieldSort>asList(new FieldSort("name")), |
| 50 | + "basic").indexName; |
| 51 | + assertEquals("The \"basic\" index should have been created.", "basic", indexName); |
| 52 | + |
| 53 | + // Make it look like a version 1 index |
| 54 | + Future<Void> rollback = indexManagerDatabaseQueue.submit(new SQLCallable<Void>() { |
| 55 | + |
| 56 | + @Override |
| 57 | + public Void call(SQLDatabase db) throws Exception { |
| 58 | + // Messy because SQLite doesn't support dropping a column |
| 59 | + // Copy the index data to a tmp location |
| 60 | + String tmpTable = "tmp" + QueryConstants.INDEX_METADATA_TABLE_NAME; |
| 61 | + db.execSQL("ALTER TABLE " + QueryConstants.INDEX_METADATA_TABLE_NAME + " RENAME " + |
| 62 | + "TO " + tmpTable); |
| 63 | + // Reset to version 0 |
| 64 | + db.execSQL("PRAGMA user_version=0;"); |
| 65 | + // Create the index metadata table at version 1 |
| 66 | + SQLDatabaseFactory.updateSchema(db, new SchemaOnlyMigration(QueryConstants |
| 67 | + .getSchemaVersion1()), 1); |
| 68 | + // Copy the data from the tmp table into the new version 1 table |
| 69 | + db.execSQL("INSERT INTO " + QueryConstants.INDEX_METADATA_TABLE_NAME + |
| 70 | + " SELECT index_name, index_type, field_name, last_sequence FROM " + |
| 71 | + tmpTable); |
| 72 | + // Drop the tmp table |
| 73 | + db.execSQL("DROP TABLE " + tmpTable); |
| 74 | + return null; |
| 75 | + } |
| 76 | + }); |
| 77 | + // Await the rollback completing |
| 78 | + rollback.get(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Test for issue https://github.com/cloudant/sync-android/issues/527 where a NPE could be |
| 83 | + * encountered when trying to use an index from v1 in v2. |
| 84 | + * |
| 85 | + * @throws Exception |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testIndexMigrate1to2() throws Exception { |
| 89 | + // Update to version 2 |
| 90 | + indexManagerDatabaseQueue.updateSchema(new SchemaOnlyMigration(QueryConstants |
| 91 | + .getSchemaVersion2()), 2); |
| 92 | + |
| 93 | + //List the indexes |
| 94 | + im.listIndexes(); |
| 95 | + } |
| 96 | +} |
0 commit comments