@@ -91,20 +91,32 @@ Future<void> migrateLegacyAppData(AppDatabase db) async {
91
91
assert (debugLog (" (account ignored because missing metadata)" ));
92
92
continue ;
93
93
}
94
- await db.createAccount (AccountsCompanion .insert (
95
- realmUrl: account.realm,
96
- userId: account.userId! ,
97
- email: account.email,
98
- apiKey: account.apiKey,
99
- zulipVersion: account.zulipVersion! ,
100
- // no zulipMergeBase; legacy app didn't record it
101
- zulipFeatureLevel: account.zulipFeatureLevel! ,
102
- // This app doesn't yet maintain ackedPushToken (#322), so avoid recording
103
- // a value that would then be allowed to get stale. See discussion:
104
- // https://github.com/zulip/zulip-flutter/pull/1588#discussion_r2148817025
105
- // TODO(#322): apply ackedPushToken
106
- // ackedPushToken: drift.Value(account.ackedPushToken),
107
- ));
94
+ try {
95
+ await db.createAccount (AccountsCompanion .insert (
96
+ realmUrl: account.realm,
97
+ userId: account.userId! ,
98
+ email: account.email,
99
+ apiKey: account.apiKey,
100
+ zulipVersion: account.zulipVersion! ,
101
+ // no zulipMergeBase; legacy app didn't record it
102
+ zulipFeatureLevel: account.zulipFeatureLevel! ,
103
+ // This app doesn't yet maintain ackedPushToken (#322), so avoid recording
104
+ // a value that would then be allowed to get stale. See discussion:
105
+ // https://github.com/zulip/zulip-flutter/pull/1588#discussion_r2148817025
106
+ // TODO(#322): apply ackedPushToken
107
+ // ackedPushToken: drift.Value(account.ackedPushToken),
108
+ ));
109
+ } on AccountAlreadyExistsException {
110
+ // There's one known way this can actually happen: the legacy app doesn't
111
+ // prevent duplicates on (realm, userId), only on (realm, email).
112
+ //
113
+ // So if e.g. the user changed their email on an account at some point
114
+ // in the past, and didn't go and delete the old version from the
115
+ // list of accounts, then the old version (the one later in the list,
116
+ // since the legacy app orders accounts by recency) will get dropped here.
117
+ assert (debugLog (" (account ignored because duplicate)" ));
118
+ continue ;
119
+ }
108
120
}
109
121
110
122
assert (debugLog ("Done migrating legacy app data." ));
0 commit comments