Fixed issue #763 by being more specific when creating temp table for update.

When performing the old style `CREATE TABLE ... AS SELECT ...` (CTAS) statement,
no indexes are added. In addition, rowid is not added. Even if manually
specifying an autoincrement column in the original schema, this autoincrement
column does not get recreated with the CTAS statement. So instead, this change
reuses the original `CREATE TABLE` statement which explicitly defines all of the
relevant columns. In addition, it explicitly adds an autoincrement integer primary
key. This has the same semantics as the existing implicit `rowid` column that
sqlite creates. From from https://sqlite.org/autoinc.html:

> In SQLite, a column with type INTEGER PRIMARY KEY is an alias for the ROWID
> (except in WITHOUT ROWID tables) which is always a 64-bit signed integer.

However, as it is explicit now, is copied when doing the
`INSERT INTO ... SELECT ...` statement to get data from the real table to the
temp table in preperation for updates (and back again after the update has
populated the temp table).

Note that this makes the `INSERT INTO ... SELECT ...` statements slightly more
brittle, because now we need the table definition used to create the temp table
(from `DBHelper.CREATE_APP_TABLE`) to have the same column order as those in the
real `fdroid_app` table. While this may sound like a silly comment to make, it
is important because database migrations can result in a database having the
correct set of columns, but in a different order to how they were specified
in the original create table statement.

If a database migration performs an `ALTER TABLE ... ADD COLUMN ...` the column
will be added at the end. If at the same time the `CREATE TABLE` is changed so
that the new column is specified as the second to last column in the list of
columns, then the `INSERT INTO ... SELECT ...` will not work as expected.
This commit is contained in:
Peter Serwylo 2016-09-24 07:16:51 +10:00
parent 9b13d98943
commit 1ba6034e19
2 changed files with 3 additions and 2 deletions

View File

@ -69,7 +69,7 @@ class DBHelper extends SQLiteOpenHelper {
+ "PRIMARY KEY (" + ApkTable.Cols.APP_ID + ", " + ApkTable.Cols.VERSION_CODE + ", " + ApkTable.Cols.REPO_ID + ")"
+ ");";
private static final String CREATE_TABLE_APP = "CREATE TABLE " + AppMetadataTable.NAME
static final String CREATE_TABLE_APP = "CREATE TABLE " + AppMetadataTable.NAME
+ " ( "
+ AppMetadataTable.Cols.PACKAGE_NAME + " text not null, "
+ AppMetadataTable.Cols.NAME + " text not null, "

View File

@ -161,7 +161,8 @@ public class TempAppProvider extends AppProvider {
final SQLiteDatabase db = db();
ensureTempTableDetached(db);
db.execSQL("ATTACH DATABASE ':memory:' AS " + DB);
db.execSQL("CREATE TABLE " + DB + "." + getTableName() + " AS SELECT * FROM main." + AppMetadataTable.NAME);
db.execSQL(DBHelper.CREATE_TABLE_APP.replaceFirst(AppMetadataTable.NAME, DB + "." + getTableName()));
db.execSQL("INSERT INTO " + DB + "." + getTableName() + " SELECT * FROM " + AppMetadataTable.NAME);
db.execSQL("CREATE INDEX IF NOT EXISTS " + DB + ".app_id ON " + getTableName() + " (" + AppMetadataTable.Cols.PACKAGE_NAME + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS " + DB + ".app_upstreamVercode ON " + getTableName() + " (" + AppMetadataTable.Cols.UPSTREAM_VERSION_CODE + ");");
db.execSQL("CREATE INDEX IF NOT EXISTS " + DB + ".app_compatible ON " + getTableName() + " (" + AppMetadataTable.Cols.IS_COMPATIBLE + ");");