mobile/android/tests/background/junit3/src/healthreport/MockHealthReportSQLiteOpenHelper.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 package org.mozilla.gecko.background.healthreport;
     6 import java.io.File;
     8 import org.mozilla.gecko.background.healthreport.HealthReportDatabaseStorage.HealthReportSQLiteOpenHelper;
    10 import android.content.Context;
    11 import android.database.sqlite.SQLiteDatabase;
    13 public class MockHealthReportSQLiteOpenHelper extends HealthReportSQLiteOpenHelper {
    14   private int version;
    16   public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name) {
    17     super(context, fakeProfileDirectory, name);
    18     version = HealthReportSQLiteOpenHelper.CURRENT_VERSION;
    19   }
    21   public MockHealthReportSQLiteOpenHelper(Context context, File fakeProfileDirectory, String name, int version) {
    22     super(context, fakeProfileDirectory, name, version);
    23     this.version = version;
    24   }
    26   @Override
    27   public void onCreate(SQLiteDatabase db) {
    28     if (version == HealthReportSQLiteOpenHelper.CURRENT_VERSION) {
    29       super.onCreate(db);
    30     } else if (version == 4) {
    31       onCreateSchemaVersion4(db);
    32     } else {
    33       throw new IllegalStateException("Unknown version number, " + version + ".");
    34     }
    35   }
    37   // Copy-pasta from HealthReportDatabaseStorage.onCreate from v4.
    38   public void onCreateSchemaVersion4(SQLiteDatabase db) {
    39     db.beginTransaction();
    40     try {
    41       db.execSQL("CREATE TABLE addons (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    42                  "                     body TEXT, " +
    43                  "                     UNIQUE (body) " +
    44                  ")");
    46       db.execSQL("CREATE TABLE environments (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    47                  "                           hash TEXT, " +
    48                  "                           profileCreation INTEGER, " +
    49                  "                           cpuCount        INTEGER, " +
    50                  "                           memoryMB        INTEGER, " +
    51                  "                           isBlocklistEnabled INTEGER, " +
    52                  "                           isTelemetryEnabled INTEGER, " +
    53                  "                           extensionCount     INTEGER, " +
    54                  "                           pluginCount        INTEGER, " +
    55                  "                           themeCount         INTEGER, " +
    56                  "                           architecture    TEXT, " +
    57                  "                           sysName         TEXT, " +
    58                  "                           sysVersion      TEXT, " +
    59                  "                           vendor          TEXT, " +
    60                  "                           appName         TEXT, " +
    61                  "                           appID           TEXT, " +
    62                  "                           appVersion      TEXT, " +
    63                  "                           appBuildID      TEXT, " +
    64                  "                           platformVersion TEXT, " +
    65                  "                           platformBuildID TEXT, " +
    66                  "                           os              TEXT, " +
    67                  "                           xpcomabi        TEXT, " +
    68                  "                           updateChannel   TEXT, " +
    69                  "                           addonsID        INTEGER, " +
    70                  "                           FOREIGN KEY (addonsID) REFERENCES addons(id) ON DELETE RESTRICT, " +
    71                  "                           UNIQUE (hash) " +
    72                  ")");
    74       db.execSQL("CREATE TABLE measurements (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    75                  "                           name TEXT, " +
    76                  "                           version INTEGER, " +
    77                  "                           UNIQUE (name, version) " +
    78                  ")");
    80       db.execSQL("CREATE TABLE fields (id INTEGER PRIMARY KEY AUTOINCREMENT, " +
    81                  "                     measurement INTEGER, " +
    82                  "                     name TEXT, " +
    83                  "                     flags INTEGER, " +
    84                  "                     FOREIGN KEY (measurement) REFERENCES measurements(id) ON DELETE CASCADE, " +
    85                  "                     UNIQUE (measurement, name)" +
    86                  ")");
    88       db.execSQL("CREATE TABLE events_integer (" +
    89                  "                 date  INTEGER, " +
    90                  "                 env   INTEGER, " +
    91                  "                 field INTEGER, " +
    92                  "                 value INTEGER, " +
    93                  "                 FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " +
    94                  "                 FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" +
    95                  ")");
    97       db.execSQL("CREATE TABLE events_textual (" +
    98                  "                 date  INTEGER, " +
    99                  "                 env   INTEGER, " +
   100                  "                 field INTEGER, " +
   101                  "                 value TEXT, " +
   102                  "                 FOREIGN KEY (field) REFERENCES fields(id) ON DELETE CASCADE, " +
   103                  "                 FOREIGN KEY (env) REFERENCES environments(id) ON DELETE CASCADE" +
   104                  ")");
   106       db.execSQL("CREATE INDEX idx_events_integer_date_env_field ON events_integer (date, env, field)");
   107       db.execSQL("CREATE INDEX idx_events_textual_date_env_field ON events_textual (date, env, field)");
   109       db.execSQL("CREATE VIEW events AS " +
   110                  "SELECT date, env, field, value FROM events_integer " +
   111                  "UNION ALL " +
   112                  "SELECT date, env, field, value FROM events_textual");
   114       db.execSQL("CREATE VIEW named_events AS " +
   115                  "SELECT date, " +
   116                  "       environments.hash AS environment, " +
   117                  "       measurements.name AS measurement_name, " +
   118                  "       measurements.version AS measurement_version, " +
   119                  "       fields.name AS field_name, " +
   120                  "       fields.flags AS field_flags, " +
   121                  "       value FROM " +
   122                  "events JOIN environments ON events.env = environments.id " +
   123                  "       JOIN fields ON events.field = fields.id " +
   124                  "       JOIN measurements ON fields.measurement = measurements.id");
   126       db.execSQL("CREATE VIEW named_fields AS " +
   127                  "SELECT measurements.name AS measurement_name, " +
   128                  "       measurements.id AS measurement_id, " +
   129                  "       measurements.version AS measurement_version, " +
   130                  "       fields.name AS field_name, " +
   131                  "       fields.id AS field_id, " +
   132                  "       fields.flags AS field_flags " +
   133                  "FROM fields JOIN measurements ON fields.measurement = measurements.id");
   135       db.execSQL("CREATE VIEW current_measurements AS " +
   136                  "SELECT name, MAX(version) AS version FROM measurements GROUP BY name");
   138       // createAddonsEnvironmentsView(db):
   139       db.execSQL("CREATE VIEW environments_with_addons AS " +
   140           "SELECT e.id AS id, " +
   141           "       e.hash AS hash, " +
   142           "       e.profileCreation AS profileCreation, " +
   143           "       e.cpuCount AS cpuCount, " +
   144           "       e.memoryMB AS memoryMB, " +
   145           "       e.isBlocklistEnabled AS isBlocklistEnabled, " +
   146           "       e.isTelemetryEnabled AS isTelemetryEnabled, " +
   147           "       e.extensionCount AS extensionCount, " +
   148           "       e.pluginCount AS pluginCount, " +
   149           "       e.themeCount AS themeCount, " +
   150           "       e.architecture AS architecture, " +
   151           "       e.sysName AS sysName, " +
   152           "       e.sysVersion AS sysVersion, " +
   153           "       e.vendor AS vendor, " +
   154           "       e.appName AS appName, " +
   155           "       e.appID AS appID, " +
   156           "       e.appVersion AS appVersion, " +
   157           "       e.appBuildID AS appBuildID, " +
   158           "       e.platformVersion AS platformVersion, " +
   159           "       e.platformBuildID AS platformBuildID, " +
   160           "       e.os AS os, " +
   161           "       e.xpcomabi AS xpcomabi, " +
   162           "       e.updateChannel AS updateChannel, " +
   163           "       addons.body AS addonsBody " +
   164           "FROM environments AS e, addons " +
   165           "WHERE e.addonsID = addons.id");
   167       db.setTransactionSuccessful();
   168     } finally {
   169       db.endTransaction();
   170     }
   171   }
   172 }

mercurial