Column in log table is not unique
DATETIME was set as primary key but that value is not always unique.
This solution is to remove primary key constraint from column "time"
to avoid SQLiteConstraintException caused in case same DATETIME value
is attempted to be inserted into log table.
(this is externally contributed change ef8c5a3947f592154e443e7c63c3e6f2ebde8a07
which was skipped in an earlier automerge)
b/8832525
Change-Id: I26ba5356f54b589882447f4aa51892e6b2928acd
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 52daa72..693d20c 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -2,7 +2,7 @@
package="com.android.providers.media"
android:sharedUserId="android.media"
android:sharedUserLabel="@string/uid_label"
- android:versionCode="600">
+ android:versionCode="601">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 816aa76..c53d49e 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -1807,6 +1807,15 @@
}
}
+ if (fromVersion < 601) {
+ // remove primary key constraint because column time is not necessarily unique
+ db.execSQL("CREATE TABLE IF NOT EXISTS log_tmp (time DATETIME, message TEXT);");
+ db.execSQL("DELETE FROM log_tmp;");
+ db.execSQL("INSERT INTO log_tmp SELECT time, message FROM log order by rowid;");
+ db.execSQL("DROP TABLE log;");
+ db.execSQL("ALTER TABLE log_tmp RENAME TO log;");
+ }
+
sanityCheck(db, fromVersion);
long elapsedSeconds = (SystemClock.currentTimeMicro() - startTime) / 1000000;
logToDb(db, "Database upgraded from version " + fromVersion + " to " + toVersion
@@ -1822,7 +1831,7 @@
new String[] { message });
// delete all but the last 500 rows
db.execSQL("DELETE FROM log WHERE rowid IN" +
- " (SELECT rowid FROM log ORDER BY time DESC LIMIT 500,-1);");
+ " (SELECT rowid FROM log ORDER BY rowid DESC LIMIT 500,-1);");
}
/**
@@ -5404,7 +5413,7 @@
}
if (dumpDbLog) {
c = db.query("log", new String[] {"time", "message"},
- null, null, null, null, "time");
+ null, null, null, null, "rowid");
try {
if (c != null) {
while (c.moveToNext()) {