Skip to content

Adding an "extras" column to transport databases #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@

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

import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;
import static org.mockito.Mockito.spy;

import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import java.util.Collections;
import javax.inject.Named;
import javax.inject.Singleton;

Expand All @@ -47,30 +52,53 @@ static EventStore eventStore(SQLiteEventStore store) {
@Provides
@Named("CREATE_EVENTS_SQL")
static String createEventsSql() {
return CREATE_EVENTS_SQL_V1;
return CREATE_EVENTS_SQL_V2;
}

@Provides
@Named("CREATE_EVENT_METADATA_SQL")
static String createEventMetadataSql() {
return CREATE_EVENT_METADATA_SQL_V1;
return CREATE_EVENT_METADATA_SQL_V2;
}

@Provides
@Named("CREATE_CONTEXTS_SQL")
static String createContextsSql() {
return CREATE_CONTEXTS_SQL_V1;
return CREATE_CONTEXTS_SQL_V2;
}

@Provides
@Named("CREATE_EVENT_BACKEND_INDEX")
static String getCreateEventBackendIndex() {
return CREATE_EVENT_BACKEND_INDEX_V1;
return CREATE_EVENT_BACKEND_INDEX_V2;
}

@Provides
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
static String createEventBackendPriorityIndex() {
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
}

@Provides
@Named("DROP_EVENTS_SQL")
static String dropEventsSQL() {
return DROP_EVENTS_SQL;
}

@Provides
@Named("DROP_EVENT_METADATA_SQL")
static String dropEventMetadataSql() {
return DROP_EVENT_METADATA_SQL;
}

@Provides
@Named("DROP_CONTEXTS_SQL")
static String dropContextsSql() {
return DROP_CONTEXTS_SQL;
}

@Provides
static DatabaseMigrationClient createDatabaseMigrationClient() {
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@

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

import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V1;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXTS_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENTS_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_BACKEND_INDEX_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.CREATE_EVENT_METADATA_SQL_V2;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_CONTEXTS_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENTS_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.DROP_EVENT_METADATA_SQL;
import static com.google.android.datatransport.runtime.scheduling.persistence.EventStoreModule.MIGRATE_TO_V2;

import com.google.android.datatransport.runtime.synchronization.SynchronizationGuard;
import dagger.Binds;
import dagger.Module;
import dagger.Provides;
import java.util.Collections;
import javax.inject.Named;

@Module
Expand Down Expand Up @@ -51,30 +56,53 @@ static EventStoreConfig storeConfig() {
@Provides
@Named("CREATE_EVENTS_SQL")
static String createEventsSql() {
return CREATE_EVENTS_SQL_V1;
return CREATE_EVENTS_SQL_V2;
}

@Provides
@Named("CREATE_EVENT_METADATA_SQL")
static String createEventMetadataSql() {
return CREATE_EVENT_METADATA_SQL_V1;
return CREATE_EVENT_METADATA_SQL_V2;
}

@Provides
@Named("CREATE_CONTEXTS_SQL")
static String createContextsSql() {
return CREATE_CONTEXTS_SQL_V1;
return CREATE_CONTEXTS_SQL_V2;
}

@Provides
@Named("CREATE_EVENT_BACKEND_INDEX")
static String getCreateEventBackendIndex() {
return CREATE_EVENT_BACKEND_INDEX_V1;
return CREATE_EVENT_BACKEND_INDEX_V2;
}

@Provides
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX")
static String createEventBackendPriorityIndex() {
return CREATE_CONTEXT_BACKEND_PRIORITY_INDEX_V1;
return CREATE_CONTEXT_BACKEND_PRIORITY_EXTRAS_INDEX_V2;
}

@Provides
@Named("DROP_EVENTS_SQL")
static String dropEventsSQL() {
return DROP_EVENTS_SQL;
}

@Provides
@Named("DROP_EVENT_METADATA_SQL")
static String dropEventMetadataSql() {
return DROP_EVENT_METADATA_SQL;
}

@Provides
@Named("DROP_CONTEXTS_SQL")
static String dropContextsSql() {
return DROP_CONTEXTS_SQL;
}

@Provides
static DatabaseMigrationClient createDatabaseMigrationClient() {
return new DatabaseMigrationClient(Collections.singletonList(MIGRATE_TO_V2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

package com.google.android.datatransport.runtime;

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

@Nullable
public abstract byte[] getExtras();

/**
* Priority of the event.
*
Expand Down Expand Up @@ -58,6 +62,8 @@ public abstract static class Builder {

public abstract Builder setBackendName(String name);

public abstract Builder setExtras(@Nullable byte[] extras);

/** @hide */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public abstract Builder setPriority(Priority priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,28 @@ class DatabaseBootstrapClient {
private final String createEventBackendIndex;
private final String createContextBackedPriorityIndex;

private final String dropEventsSql;
private final String dropEventMetadataSql;
private final String dropContextsSql;

@Inject
DatabaseBootstrapClient(
@Named("CREATE_EVENTS_SQL") String createEventsSql,
@Named("CREATE_EVENT_METADATA_SQL") String createEventMetadataSql,
@Named("CREATE_CONTEXTS_SQL") String createContextsSql,
@Named("CREATE_EVENT_BACKEND_INDEX") String createEventBackendIndex,
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX") String createContextBackendPriorityIndex) {
@Named("CREATE_CONTEXT_BACKEND_PRIORITY_INDEX") String createContextBackendPriorityIndex,
@Named("DROP_EVENTS_SQL") String dropEventsSql,
@Named("DROP_EVENT_METADATA_SQL") String dropEventMetadataSql,
@Named("DROP_CONTEXTS_SQL") String dropContextsSql) {
this.createEventsSql = createEventsSql;
this.createEventMetadataSql = createEventMetadataSql;
this.createContextsSql = createContextsSql;
this.createEventBackendIndex = createEventBackendIndex;
this.createContextBackedPriorityIndex = createContextBackendPriorityIndex;
this.dropEventsSql = dropEventsSql;
this.dropEventMetadataSql = dropEventMetadataSql;
this.dropContextsSql = dropContextsSql;
}

void bootstrap(SQLiteDatabase db) {
Expand All @@ -46,4 +56,11 @@ void bootstrap(SQLiteDatabase db) {
db.execSQL(createEventBackendIndex);
db.execSQL(createContextBackedPriorityIndex);
}

void teardown(SQLiteDatabase db) {
db.execSQL(dropEventsSql);
db.execSQL(dropEventMetadataSql);
db.execSQL(dropContextsSql);
// Indices are dropped automatically when the tables are dropped
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.android.datatransport.runtime.scheduling.persistence;

import android.database.sqlite.SQLiteDatabase;
import java.util.List;

class DatabaseMigrationClient {
private final List<Migration> incrementalMigrations;

DatabaseMigrationClient(List<Migration> incrementalMigrations) {
this.incrementalMigrations = incrementalMigrations;
}

void upgrade(SQLiteDatabase db, int fromVersion, int toVersion) {
for (Migration m : incrementalMigrations) {
m.upgrade(db, fromVersion, toVersion);
}
}

public interface Migration {
void upgrade(SQLiteDatabase db, int fromVersion, int toVersion);
}
}
Loading