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