Skip to content

Commit aa9f55e

Browse files
committed
Modelling creates as migrations
1 parent 8f336bd commit aa9f55e

File tree

9 files changed

+134
-348
lines changed

9 files changed

+134
-348
lines changed

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

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@
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_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;
2217
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
2318
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
2419
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
20+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V1;
2521
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;
22+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.SCHEMA_VERSION;
2623
import static org.mockito.Mockito.spy;
2724

2825
import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
2926
import dagger.Binds;
3027
import dagger.Module;
3128
import dagger.Provides;
32-
import java.util.Collections;
29+
import java.util.Arrays;
30+
import java.util.List;
3331
import javax.inject.Named;
3432
import javax.inject.Singleton;
3533

@@ -49,36 +47,6 @@ static EventStore eventStore(SQLiteEventStore store) {
4947
@Binds
5048
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);
5149

52-
@Provides
53-
@Named("CREATE_EVENTS_SQL")
54-
static String createEventsSql() {
55-
return CREATE_EVENTS_SQL_V2;
56-
}
57-
58-
@Provides
59-
@Named("CREATE_EVENT_METADATA_SQL")
60-
static String createEventMetadataSql() {
61-
return CREATE_EVENT_METADATA_SQL_V2;
62-
}
63-
64-
@Provides
65-
@Named("CREATE_CONTEXTS_SQL")
66-
static String createContextsSql() {
67-
return CREATE_CONTEXTS_SQL_V2;
68-
}
69-
70-
@Provides
71-
@Named("CREATE_EVENT_BACKEND_INDEX")
72-
static String getCreateEventBackendIndex() {
73-
return CREATE_EVENT_BACKEND_INDEX_V2;
74-
}
75-
76-
@Provides
77-
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
78-
static String createEventBackendPriorityIndex() {
79-
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
80-
}
81-
8250
@Provides
8351
@Named("DROP_EVENTS_SQL")
8452
static String dropEventsSQL() {
@@ -98,7 +66,13 @@ static String dropContextsSql() {
9866
}
9967

10068
@Provides
101-
static DatabaseMigrationClient createDatabaseMigrationClient() {
102-
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
69+
static List<SchemaManager.Migration> databaseMigrations() {
70+
return Arrays.asList(MIGRATE_TO_V1, MIGRATE_TO_V2);
71+
}
72+
73+
@Provides
74+
@Named("SCHEMA_VERSION")
75+
static int schemaVersion() {
76+
return SCHEMA_VERSION;
10377
}
10478
}

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

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,19 @@
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_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;
2217
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
2318
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
2419
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
20+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V1;
2521
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;
22+
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.SCHEMA_VERSION;
2623

2724
import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
2825
import dagger.Binds;
2926
import dagger.Module;
3027
import dagger.Provides;
31-
import java.util.Collections;
28+
import java.util.Arrays;
29+
import java.util.List;
3230
import javax.inject.Named;
3331

3432
@Module
@@ -53,36 +51,6 @@ static EventStoreConfig storeConfig() {
5351
@Binds
5452
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);
5553

56-
@Provides
57-
@Named("CREATE_EVENTS_SQL")
58-
static String createEventsSql() {
59-
return CREATE_EVENTS_SQL_V2;
60-
}
61-
62-
@Provides
63-
@Named("CREATE_EVENT_METADATA_SQL")
64-
static String createEventMetadataSql() {
65-
return CREATE_EVENT_METADATA_SQL_V2;
66-
}
67-
68-
@Provides
69-
@Named("CREATE_CONTEXTS_SQL")
70-
static String createContextsSql() {
71-
return CREATE_CONTEXTS_SQL_V2;
72-
}
73-
74-
@Provides
75-
@Named("CREATE_EVENT_BACKEND_INDEX")
76-
static String getCreateEventBackendIndex() {
77-
return CREATE_EVENT_BACKEND_INDEX_V2;
78-
}
79-
80-
@Provides
81-
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
82-
static String createEventBackendPriorityIndex() {
83-
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
84-
}
85-
8654
@Provides
8755
@Named("DROP_EVENTS_SQL")
8856
static String dropEventsSQL() {
@@ -102,7 +70,13 @@ static String dropContextsSql() {
10270
}
10371

10472
@Provides
105-
static DatabaseMigrationClient createDatabaseMigrationClient() {
106-
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
73+
static List<SchemaManager.Migration> databaseMigrations() {
74+
return Arrays.asList(MIGRATE_TO_V1, MIGRATE_TO_V2);
75+
}
76+
77+
@Provides
78+
@Named("SCHEMA_VERSION")
79+
static int schemaVersion() {
80+
return SCHEMA_VERSION;
10781
}
10882
}

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
import dagger.Binds;
1919
import dagger.Module;
2020
import dagger.Provides;
21-
import java.util.Collections;
21+
import java.util.Arrays;
22+
import java.util.List;
2223
import javax.inject.Named;
2324

2425
@Module
@@ -34,7 +35,7 @@ public abstract class EventStoreModule {
3435
// 4b. Bootstrapping the table at Vn
3536

3637
// Note: 4. Migrations handle only upgrades. Downgrades will drop and recreate all tables/indices.
37-
static final String CREATE_EVENTS_SQL_V2 =
38+
private static final String CREATE_EVENTS_SQL_V1 =
3839
"CREATE TABLE events "
3940
+ "(_id INTEGER PRIMARY KEY,"
4041
+ " context_id INTEGER NOT NULL,"
@@ -46,37 +47,52 @@ public abstract class EventStoreModule {
4647
+ " num_attempts INTEGER NOT NULL,"
4748
+ "FOREIGN KEY (context_id) REFERENCES transport_contexts(_id) ON DELETE CASCADE)";
4849

49-
static final String CREATE_EVENT_METADATA_SQL_V2 =
50+
private static final String CREATE_EVENT_METADATA_SQL_V1 =
5051
"CREATE TABLE event_metadata "
5152
+ "(_id INTEGER PRIMARY KEY,"
5253
+ " event_id INTEGER NOT NULL,"
5354
+ " name TEXT NOT NULL,"
5455
+ " value TEXT NOT NULL,"
5556
+ "FOREIGN KEY (event_id) REFERENCES events(_id) ON DELETE CASCADE)";
5657

57-
static final String CREATE_CONTEXTS_SQL_V2 =
58+
private static final String CREATE_CONTEXTS_SQL_V1 =
5859
"CREATE TABLE transport_contexts "
5960
+ "(_id INTEGER PRIMARY KEY,"
6061
+ " backend_name TEXT NOT NULL,"
6162
+ " priority INTEGER NOT NULL,"
62-
+ " next_request_ms INTEGER NOT NULL,"
63-
+ " extras TEXT)";
63+
+ " next_request_ms INTEGER NOT NULL)";
6464

65-
static final String CREATE_EVENT_BACKEND_INDEX_V2 =
65+
private static final String CREATE_EVENT_BACKEND_INDEX_V1 =
6666
"CREATE INDEX events_backend_id on events(context_id)";
6767

68-
static final String CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2 =
69-
"CREATE UNIQUE INDEX contexts_backend_priority_extras on transport_contexts(backend_name, priority, extras)";
68+
private static final String CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1 =
69+
"CREATE UNIQUE INDEX contexts_backend_priority on transport_contexts(backend_name, priority)";
7070

7171
static final String DROP_EVENTS_SQL = "DROP TABLE events";
7272

7373
static final String DROP_EVENT_METADATA_SQL = "DROP TABLE event_metadata";
7474

7575
static final String DROP_CONTEXTS_SQL = "DROP TABLE transport_contexts";
7676

77-
static final DatabaseMigrationClient.Migration MIGRATE_TO_V2 =
77+
static int SCHEMA_VERSION = 2;
78+
79+
static final SchemaManager.Migration MIGRATE_TO_V1 =
80+
(db, fromVersion, toVersion) -> {
81+
if (fromVersion < 1 && toVersion >= 1) {
82+
// if (true) {
83+
// throw new RuntimeException("failing");
84+
// }
85+
db.execSQL(CREATE_EVENTS_SQL_V1);
86+
db.execSQL(CREATE_EVENT_METADATA_SQL_V1);
87+
db.execSQL(CREATE_CONTEXTS_SQL_V1);
88+
db.execSQL(CREATE_EVENT_BACKEND_INDEX_V1);
89+
db.execSQL(CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1);
90+
}
91+
};
92+
93+
static final SchemaManager.Migration MIGRATE_TO_V2 =
7894
(db, fromVersion, toVersion) -> {
79-
if (fromVersion == 1 && toVersion == 2) {
95+
if (fromVersion < 2 && toVersion >= 2) {
8096
db.execSQL("ALTER TABLE transport_contexts ADD COLUMN extras TEXT");
8197
db.execSQL(
8298
"CREATE UNIQUE INDEX contexts_backend_priority_extras on transport_contexts(backend_name, priority, extras)");
@@ -95,36 +111,6 @@ static EventStoreConfig storeConfig() {
95111
@Binds
96112
abstract SynchronizationGuard synchronizationGuard(SQLiteEventStore store);
97113

98-
@Provides
99-
@Named("CREATE_EVENTS_SQL")
100-
static String createEventsSql() {
101-
return CREATE_EVENTS_SQL_V2;
102-
}
103-
104-
@Provides
105-
@Named("CREATE_EVENT_METADATA_SQL")
106-
static String createEventMetadataSql() {
107-
return CREATE_EVENT_METADATA_SQL_V2;
108-
}
109-
110-
@Provides
111-
@Named("CREATE_CONTEXTS_SQL")
112-
static String createContextsSql() {
113-
return CREATE_CONTEXTS_SQL_V2;
114-
}
115-
116-
@Provides
117-
@Named("CREATE_EVENT_BACKEND_INDEX")
118-
static String getCreateEventBackendIndex() {
119-
return CREATE_EVENT_BACKEND_INDEX_V2;
120-
}
121-
122-
@Provides
123-
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
124-
static String createEventBackendPriorityIndex() {
125-
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
126-
}
127-
128114
@Provides
129115
@Named("DROP_EVENTS_SQL")
130116
static String dropEventsSQL() {
@@ -144,7 +130,13 @@ static String dropContextsSql() {
144130
}
145131

146132
@Provides
147-
static DatabaseMigrationClient createDatabaseMigrationClient() {
148-
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
133+
@Named("SCHEMA_VERSION")
134+
static int schemaVersion() {
135+
return SCHEMA_VERSION;
136+
}
137+
138+
@Provides
139+
static List<SchemaManager.Migration> databaseMigrations() {
140+
return Arrays.asList(MIGRATE_TO_V1, MIGRATE_TO_V2);
149141
}
150142
}

0 commit comments

Comments
 (0)