michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: michael@0: package org.mozilla.gecko.background.healthreport; michael@0: michael@0: import java.io.File; michael@0: michael@0: import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage.HealthReportSQLiteOpenHelper; michael@0: michael@0: import android.content.Context; michael@0: import android.database.sqlite.SQLiteDatabase; michael@0: michael@0: public class MockHealthReportSQLiteOpenHelper extends HealthReportSQLiteOpenHelper { michael@0: private int version; michael@0: michael@0: public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name) { michael@0: super(context, fakeProfileDirectory, name); michael@0: version = HealthReportSQLiteOpenHelper.CURRENT_VERSION; michael@0: } michael@0: michael@0: public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name, int version) { michael@0: super(context, fakeProfileDirectory, name, version); michael@0: this.version = version; michael@0: } michael@0: michael@0: @Override michael@0: public void onCreate(SQLiteDatabase db) { michael@0: if (version == HealthReportSQLiteOpenHelper.CURRENT_VERSION) { michael@0: super.onCreate(db); michael@0: } else if (version == 4) { michael@0: onCreateSchemaVersion4(db); michael@0: } else { michael@0: throw new IllegalStateException("Unknown version number, " + version + "."); michael@0: } michael@0: } michael@0: michael@0: // Copy-pasta from HealthReportDatabaseStorage.onCreate from v4. michael@0: public void onCreateSchemaVersion4(SQLiteDatabase db) { michael@0: db.beginTransaction(); michael@0: try { michael@0: db.execSQL("CREATE TABLE addons (id INTEGER PRIMARY KEY AUTOINCREMENT, " + michael@0: " body TEXT, " + michael@0: " UNIQUE (body) " + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE TABLE environments (id INTEGER PRIMARY KEY AUTOINCREMENT, " + michael@0: " hash TEXT, " + michael@0: " profileCreation INTEGER, " + michael@0: " cpuCount INTEGER, " + michael@0: " memoryMB INTEGER, " + michael@0: " isBlocklistEnabled INTEGER, " + michael@0: " isTelemetryEnabled INTEGER, " + michael@0: " extensionCount INTEGER, " + michael@0: " pluginCount INTEGER, " + michael@0: " themeCount INTEGER, " + michael@0: " architecture TEXT, " + michael@0: " sysName TEXT, " + michael@0: " sysVersion TEXT, " + michael@0: " vendor TEXT, " + michael@0: " appName TEXT, " + michael@0: " appID TEXT, " + michael@0: " appVersion TEXT, " + michael@0: " appBuildID TEXT, " + michael@0: " platformVersion TEXT, " + michael@0: " platformBuildID TEXT, " + michael@0: " os TEXT, " + michael@0: " xpcomabi TEXT, " + michael@0: " updateChannel TEXT, " + michael@0: " addonsID INTEGER, " + michael@0: " FOREIGN KEY (addonsID) REFERENCES addons(id) ON DELETE RESTRICT, " + michael@0: " UNIQUE (hash) " + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE TABLE measurements (id INTEGER PRIMARY KEY AUTOINCREMENT, " + michael@0: " name TEXT, " + michael@0: " version INTEGER, " + michael@0: " UNIQUE (name, version) " + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE TABLE fields (id INTEGER PRIMARY KEY AUTOINCREMENT, " + michael@0: " measurement INTEGER, " + michael@0: " name TEXT, " + michael@0: " flags INTEGER, " + michael@0: " FOREIGN KEY (measurement) REFERENCES measurements(id) ON DELETE CASCADE, " + michael@0: " UNIQUE (measurement, name)" + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE TABLE events_integer (" + michael@0: " date INTEGER, " + michael@0: " env INTEGER, " + michael@0: " field INTEGER, " + michael@0: " value INTEGER, " + michael@0: " FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " + michael@0: " FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE TABLE events_textual (" + michael@0: " date INTEGER, " + michael@0: " env INTEGER, " + michael@0: " field INTEGER, " + michael@0: " value TEXT, " + michael@0: " FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " + michael@0: " FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" + michael@0: ")"); michael@0: michael@0: db.execSQL("CREATE INDEX idx_events_integer_date_env_field ON events_integer (date, env, field)"); michael@0: db.execSQL("CREATE INDEX idx_events_textual_date_env_field ON events_textual (date, env, field)"); michael@0: michael@0: db.execSQL("CREATE VIEW events AS " + michael@0: "SELECT date, env, field, value FROM events_integer " + michael@0: "UNION ALL " + michael@0: "SELECT date, env, field, value FROM events_textual"); michael@0: michael@0: db.execSQL("CREATE VIEW named_events AS " + michael@0: "SELECT date, " + michael@0: " environments.hash AS environment, " + michael@0: " measurements.name AS measurement_name, " + michael@0: " measurements.version AS measurement_version, " + michael@0: " fields.name AS field_name, " + michael@0: " fields.flags AS field_flags, " + michael@0: " value FROM " + michael@0: "events JOIN environments ON events.env = environments.id " + michael@0: " JOIN fields ON events.field = fields.id " + michael@0: " JOIN measurements ON fields.measurement = measurements.id"); michael@0: michael@0: db.execSQL("CREATE VIEW named_fields AS " + michael@0: "SELECT measurements.name AS measurement_name, " + michael@0: " measurements.id AS measurement_id, " + michael@0: " measurements.version AS measurement_version, " + michael@0: " fields.name AS field_name, " + michael@0: " fields.id AS field_id, " + michael@0: " fields.flags AS field_flags " + michael@0: "FROM fields JOIN measurements ON fields.measurement = measurements.id"); michael@0: michael@0: db.execSQL("CREATE VIEW current_measurements AS " + michael@0: "SELECT name, MAX(version) AS version FROM measurements GROUP BY name"); michael@0: michael@0: // createAddonsEnvironmentsView(db): michael@0: db.execSQL("CREATE VIEW environments_with_addons AS " + michael@0: "SELECT e.id AS id, " + michael@0: " e.hash AS hash, " + michael@0: " e.profileCreation AS profileCreation, " + michael@0: " e.cpuCount AS cpuCount, " + michael@0: " e.memoryMB AS memoryMB, " + michael@0: " e.isBlocklistEnabled AS isBlocklistEnabled, " + michael@0: " e.isTelemetryEnabled AS isTelemetryEnabled, " + michael@0: " e.extensionCount AS extensionCount, " + michael@0: " e.pluginCount AS pluginCount, " + michael@0: " e.themeCount AS themeCount, " + michael@0: " e.architecture AS architecture, " + michael@0: " e.sysName AS sysName, " + michael@0: " e.sysVersion AS sysVersion, " + michael@0: " e.vendor AS vendor, " + michael@0: " e.appName AS appName, " + michael@0: " e.appID AS appID, " + michael@0: " e.appVersion AS appVersion, " + michael@0: " e.appBuildID AS appBuildID, " + michael@0: " e.platformVersion AS platformVersion, " + michael@0: " e.platformBuildID AS platformBuildID, " + michael@0: " e.os AS os, " + michael@0: " e.xpcomabi AS xpcomabi, " + michael@0: " e.updateChannel AS updateChannel, " + michael@0: " addons.body AS addonsBody " + michael@0: "FROM environments AS e, addons " + michael@0: "WHERE e.addonsID = addons.id"); michael@0: michael@0: db.setTransactionSuccessful(); michael@0: } finally { michael@0: db.endTransaction(); michael@0: } michael@0: } michael@0: }