@@ -38,14 +38,15 @@ public void onConfigure(SQLiteDatabase db) {
38
38
39
39
@ Override
40
40
public void onUpgrade (SQLiteDatabase db , int oldVersion , int newVersion ) {
41
- if (oldVersion < 5 ) {
41
+ if (oldVersion > newVersion ) {
42
+ // The documentation for this method does not explicitly state this cannot occur
43
+ // (newVersion < oldVersion). We cannot downgrade so recreate the database instead.
44
+ Log .w (TAG , "Database downgrade detected; DB downgrade is not supported" );
45
+ recreateDatabase (db );
46
+ } else if (oldVersion < 5 ) {
42
47
Log .w (TAG , "Old database schema detected; pre-v1.0.0, no DB schema backward compatibility is implemented" );
43
- db .execSQL ("DROP TABLE IF EXISTS " + IdentityRecordSchema .TABLE_IDENTITIES );
44
- db .execSQL ("DROP TABLE IF EXISTS " + AuthorizationsSchema .TABLE_AUTHORIZATIONS );
45
- db .execSQL ("DROP TABLE IF EXISTS " + PublicKeysSchema .TABLE_PUBLIC_KEYS );
46
- db .execSQL ("DROP TABLE IF EXISTS " + WalletUriBaseSchema .TABLE_WALLET_URI_BASE );
47
- onCreate (db );
48
- } else {
48
+ recreateDatabase (db );
49
+ } else try {
49
50
// first migrate from public keys to accounts if necessary
50
51
if (oldVersion == 5 ) {
51
52
Log .w (TAG , "Old database schema detected; pre-v2.0.0, migrating public keys to account records" );
@@ -88,28 +89,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
88
89
// migrate to multi account structure
89
90
Log .w (TAG , "Old database schema detected; pre-v2.1.0, migrating to multi account structure" );
90
91
91
- // try (final Cursor cursor = db.rawQuery("SELECT " +
92
- // AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ID +
93
- // ", " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID +
94
- // ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID +
95
- // ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_PUBLIC_KEY_RAW +
96
- // ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_LABEL +
97
- // " FROM " + AuthorizationsSchema.TABLE_AUTHORIZATIONS +
98
- // " INNER JOIN " + AccountRecordsSchema.TABLE_ACCOUNTS +
99
- // " ON " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID +
100
- // " = " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID,
101
- // null)) {
102
- // AccountRecordsDao accountRecordsDao = new AccountRecordsDao(db);
103
- // while (cursor.moveToNext()) {
104
- // final int parentId = cursor.getInt(0);
105
- // final int accountId = cursor.getInt(1);
106
- // ContentValues values = new ContentValues();
107
- // values.put(AccountRecordsSchema.COLUMN_ACCOUNTS_PARENT_ID, parentId);
108
- // accountRecordsDao.update(AccountRecordsSchema.TABLE_ACCOUNTS, values,
109
- // AccountRecordsSchema.COLUMN_ACCOUNTS_ID + "=" + accountId, null);
110
- // }
111
- // }
112
-
113
92
// migrate to multi account structure
114
93
// first add parent id column to accounts table
115
94
// add parent ids to accounts table
@@ -165,9 +144,21 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
165
144
authorizationMigrationTable );
166
145
167
146
db .execSQL ("DROP TABLE IF EXISTS " + authorizationMigrationTable );
147
+ } catch (Throwable ignored ) {
148
+ Log .w (TAG , "Database migration failed, recreating database" );
149
+ recreateDatabase (db );
168
150
}
169
151
}
170
152
153
+ private void recreateDatabase (SQLiteDatabase db ) {
154
+ db .execSQL ("DROP TABLE IF EXISTS " + IdentityRecordSchema .TABLE_IDENTITIES );
155
+ db .execSQL ("DROP TABLE IF EXISTS " + AuthorizationsSchema .TABLE_AUTHORIZATIONS );
156
+ db .execSQL ("DROP TABLE IF EXISTS " + PublicKeysSchema .TABLE_PUBLIC_KEYS );
157
+ db .execSQL ("DROP TABLE IF EXISTS " + WalletUriBaseSchema .TABLE_WALLET_URI_BASE );
158
+ db .execSQL ("DROP TABLE IF EXISTS " + AccountRecordsSchema .TABLE_ACCOUNTS );
159
+ onCreate (db );
160
+ }
161
+
171
162
@ NonNull
172
163
public static String getDatabaseName (@ NonNull AuthIssuerConfig authIssuerConfig ) {
173
164
return authIssuerConfig .name + DATABASE_NAME_SUFFIX ;
0 commit comments