Skip to content

Commit 58dee2d

Browse files
committed
Added tests for upgrade
1 parent d92ef7c commit 58dee2d

File tree

11 files changed

+385
-52
lines changed

11 files changed

+385
-52
lines changed

transport/transport-runtime/src/androidTest/java/com/google/android/datatransport/runtime/scheduling/persistence/SpyEventStoreModule.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,22 @@
1414

1515
package com.google.android.datatransport.runtime.scheduling.persistence;
1616

17-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
18-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
19-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
20-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
21-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
17+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V2;
18+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
19+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V2;
20+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V2;
21+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V2;
22+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
23+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
24+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
25+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;
2226
import static org.mockito.Mockito.spy;
2327

2428
import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
2529
import dagger.Binds;
2630
import dagger.Module;
2731
import dagger.Provides;
32+
import java.util.Collections;
2833
import javax.inject.Named;
2934
import javax.inject.Singleton;
3035

@@ -47,30 +52,53 @@ static EventStore eventStore(SQLiteEventStore store) {
4752
@Provides
4853
@Named("CREATE_EVENTS_SQL")
4954
static String createEventsSql() {
50-
return CREATE_EVENTS_SQL_V1;
55+
return CREATE_EVENTS_SQL_V2;
5156
}
5257

5358
@Provides
5459
@Named("CREATE_EVENT_METADATA_SQL")
5560
static String createEventMetadataSql() {
56-
return CREATE_EVENT_METADATA_SQL_V1;
61+
return CREATE_EVENT_METADATA_SQL_V2;
5762
}
5863

5964
@Provides
6065
@Named("CREATE_CONTEXTS_SQL")
6166
static String createContextsSql() {
62-
return CREATE_CONTEXTS_SQL_V1;
67+
return CREATE_CONTEXTS_SQL_V2;
6368
}
6469

6570
@Provides
6671
@Named("CREATE_EVENT_BACKEND_INDEX")
6772
static String getCreateEventBackendIndex() {
68-
return CREATE_EVENT_BACKEND_INDEX_V1;
73+
return CREATE_EVENT_BACKEND_INDEX_V2;
6974
}
7075

7176
@Provides
7277
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
7378
static String createEventBackendPriorityIndex() {
74-
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
79+
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
80+
}
81+
82+
@Provides
83+
@Named("DROP_EVENTS_SQL")
84+
static String dropEventsSQL() {
85+
return DROP_EVENTS_SQL;
86+
}
87+
88+
@Provides
89+
@Named("DROP_EVENT_METADATA_SQL")
90+
static String dropEventMetadataSql() {
91+
return DROP_EVENT_METADATA_SQL;
92+
}
93+
94+
@Provides
95+
@Named("DROP_CONTEXTS_SQL")
96+
static String dropContextsSql() {
97+
return DROP_CONTEXTS_SQL;
98+
}
99+
100+
@Provides
101+
static DatabaseMigrationClient createDatabaseMigrationClient() {
102+
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
75103
}
76104
}

transport/transport-runtime/src/androidTest/java/com/google/android/datatransport/runtime/scheduling/persistence/TestEventStoreModule.java

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,21 @@
1414

1515
package com.google.android.datatransport.runtime.scheduling.persistence;
1616

17-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
18-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
19-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
20-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
21-
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
17+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V2;
18+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
19+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V2;
20+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V2;
21+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V2;
22+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
23+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
24+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
25+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;
2226

2327
import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
2428
import dagger.Binds;
2529
import dagger.Module;
2630
import dagger.Provides;
31+
import java.util.Collections;
2732
import javax.inject.Named;
2833

2934
@Module
@@ -51,30 +56,53 @@ static EventStoreConfig storeConfig() {
5156
@Provides
5257
@Named("CREATE_EVENTS_SQL")
5358
static String createEventsSql() {
54-
return CREATE_EVENTS_SQL_V1;
59+
return CREATE_EVENTS_SQL_V2;
5560
}
5661

5762
@Provides
5863
@Named("CREATE_EVENT_METADATA_SQL")
5964
static String createEventMetadataSql() {
60-
return CREATE_EVENT_METADATA_SQL_V1;
65+
return CREATE_EVENT_METADATA_SQL_V2;
6166
}
6267

6368
@Provides
6469
@Named("CREATE_CONTEXTS_SQL")
6570
static String createContextsSql() {
66-
return CREATE_CONTEXTS_SQL_V1;
71+
return CREATE_CONTEXTS_SQL_V2;
6772
}
6873

6974
@Provides
7075
@Named("CREATE_EVENT_BACKEND_INDEX")
7176
static String getCreateEventBackendIndex() {
72-
return CREATE_EVENT_BACKEND_INDEX_V1;
77+
return CREATE_EVENT_BACKEND_INDEX_V2;
7378
}
7479

7580
@Provides
7681
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
7782
static String createEventBackendPriorityIndex() {
78-
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
83+
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
84+
}
85+
86+
@Provides
87+
@Named("DROP_EVENTS_SQL")
88+
static String dropEventsSQL() {
89+
return DROP_EVENTS_SQL;
90+
}
91+
92+
@Provides
93+
@Named("DROP_EVENT_METADATA_SQL")
94+
static String dropEventMetadataSql() {
95+
return DROP_EVENT_METADATA_SQL;
96+
}
97+
98+
@Provides
99+
@Named("DROP_CONTEXTS_SQL")
100+
static String dropContextsSql() {
101+
return DROP_CONTEXTS_SQL;
102+
}
103+
104+
@Provides
105+
static DatabaseMigrationClient createDatabaseMigrationClient() {
106+
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
79107
}
80108
}

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/TransportContext.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.android.datatransport.runtime;
1616

17+
import androidx.annotation.Nullable;
1718
import androidx.annotation.RestrictTo;
1819
import com.google.android.datatransport.Priority;
1920
import com.google.auto.value.AutoValue;
@@ -26,6 +27,9 @@ public abstract class TransportContext {
2627
/** Backend events are sent to. */
2728
public abstract String getBackendName();
2829

30+
@Nullable
31+
public abstract byte[] getExtras();
32+
2933
/**
3034
* Priority of the event.
3135
*
@@ -39,7 +43,9 @@ public abstract class TransportContext {
3943

4044
/** Returns a new builder for {@link TransportContext}. */
4145
public static Builder builder() {
42-
return new AutoValue_TransportContext.Builder().setPriority(Priority.DEFAULT);
46+
return new AutoValue_TransportContext.Builder()
47+
.setPriority(Priority.DEFAULT)
48+
.setExtras("sd".getBytes());
4349
}
4450

4551
/**
@@ -58,6 +64,8 @@ public abstract static class Builder {
5864

5965
public abstract Builder setBackendName(String name);
6066

67+
public abstract Builder setExtras(@Nullable byte[] extras);
68+
6169
/** @hide */
6270
@RestrictTo(RestrictTo.Scope.LIBRARY)
6371
public abstract Builder setPriority(Priority priority);

transport/transport-runtime/src/main/java/com/google/android/datatransport/runtime/scheduling/persistence/DatabaseBootstrapClient.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,28 @@ class DatabaseBootstrapClient {
2525
private final String createEventBackendIndex;
2626
private final String createContextBackedPriorityIndex;
2727

28+
private final String dropEventsSql;
29+
private final String dropEventMetadataSql;
30+
private final String dropContextsSql;
31+
2832
@Inject
2933
DatabaseBootstrapClient(
3034
@Named("CREATE_EVENTS_SQL") String createEventsSql,
3135
@Named("CREATE_EVENT_METADATA_SQL") String createEventMetadataSql,
3236
@Named("CREATE_CONTEXTS_SQL") String createContextsSql,
3337
@Named("CREATE_EVENT_BACKEND_INDEX") String createEventBackendIndex,
34-
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX") String createContextBackendPriorityIndex) {
38+
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX") String createContextBackendPriorityIndex,
39+
@Named("DROP_EVENTS_SQL") String dropEventsSql,
40+
@Named("DROP_EVENT_METADATA_SQL") String dropEventMetadataSql,
41+
@Named("DROP_CONTEXTS_SQL") String dropContextsSql) {
3542
this.createEventsSql = createEventsSql;
3643
this.createEventMetadataSql = createEventMetadataSql;
3744
this.createContextsSql = createContextsSql;
3845
this.createEventBackendIndex = createEventBackendIndex;
3946
this.createContextBackedPriorityIndex = createContextBackendPriorityIndex;
47+
this.dropEventsSql = dropEventsSql;
48+
this.dropEventMetadataSql = dropEventMetadataSql;
49+
this.dropContextsSql = dropContextsSql;
4050
}
4151

4252
void bootstrap(SQLiteDatabase db) {
@@ -46,4 +56,11 @@ void bootstrap(SQLiteDatabase db) {
4656
db.execSQL(createEventBackendIndex);
4757
db.execSQL(createContextBackedPriorityIndex);
4858
}
59+
60+
void teardown(SQLiteDatabase db) {
61+
db.execSQL(dropEventsSql);
62+
db.execSQL(dropEventMetadataSql);
63+
db.execSQL(dropContextsSql);
64+
// Indices are dropped automatically when the tables are dropped
65+
}
4966
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.google.android.datatransport.runtime.scheduling.persistence;
2+
3+
import android.database.sqlite.SQLiteDatabase;
4+
import java.util.List;
5+
6+
class DatabaseMigrationClient {
7+
private final List<Migration> incrementalMigrations;
8+
9+
DatabaseMigrationClient(List<Migration> incrementalMigrations) {
10+
this.incrementalMigrations = incrementalMigrations;
11+
}
12+
13+
void upgrade(SQLiteDatabase db, int fromVersion, int toVersion) {
14+
for (Migration m : incrementalMigrations) {
15+
m.upgrade(db, fromVersion, toVersion);
16+
}
17+
}
18+
19+
public interface Migration {
20+
void upgrade(SQLiteDatabase db, int fromVersion, int toVersion);
21+
}
22+
}

0 commit comments

Comments
 (0)