Avoid more constraint violations during db-move

This commit is contained in:
Andrew Ferrazzutti 2022-08-15 17:33:06 -04:00 committed by Finn
parent 1ced8e01b2
commit 5470eb90d9

View file

@ -209,7 +209,9 @@ func moveAccounts(source *sql.DB, dest *sql.DB) error {
}
func moveRecipients(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT rowid, account_uuid, uuid, e164, registered FROM recipients")
rows, err := source.Query("SELECT rowid, account_uuid, uuid, e164, registered FROM recipients" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -249,7 +251,9 @@ func moveRecipients(source *sql.DB, dest *sql.DB) error {
}
func movePrekeys(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, id, record FROM prekeys")
rows, err := source.Query("SELECT account_uuid, id, record FROM prekeys" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -274,7 +278,10 @@ func movePrekeys(source *sql.DB, dest *sql.DB) error {
}
func moveSessions(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, device_id, record FROM sessions")
rows, err := source.Query("SELECT account_uuid, recipient, device_id, record FROM sessions" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" AND recipient IN (SELECT DISTINCT rowid FROM recipients)",
)
if err != nil {
return err
}
@ -309,7 +316,9 @@ func moveSessions(source *sql.DB, dest *sql.DB) error {
}
func moveSignedPrekeys(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, id, record FROM signed_prekeys")
rows, err := source.Query("SELECT account_uuid, id, record FROM signed_prekeys" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -334,7 +343,9 @@ func moveSignedPrekeys(source *sql.DB, dest *sql.DB) error {
}
func moveIdentityKeys(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, identity_key, trust_level, added FROM identity_keys")
rows, err := source.Query("SELECT account_uuid, recipient, identity_key, trust_level, added FROM identity_keys" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -361,7 +372,9 @@ func moveIdentityKeys(source *sql.DB, dest *sql.DB) error {
}
func moveAccountData(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, key, value FROM account_data")
rows, err := source.Query("SELECT account_uuid, key, value FROM account_data" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -411,7 +424,9 @@ func movePendingAccountData(source *sql.DB, dest *sql.DB) error {
}
func moveSenderKeys(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, address, device, distribution_id, record, created_at FROM sender_keys")
rows, err := source.Query("SELECT account_uuid, address, device, distribution_id, record, created_at FROM sender_keys" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -447,7 +462,9 @@ func moveSenderKeys(source *sql.DB, dest *sql.DB) error {
}
func moveSenderKeyShared(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, distribution_id, address, device FROM sender_key_shared")
rows, err := source.Query("SELECT account_uuid, distribution_id, address, device FROM sender_key_shared" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -483,7 +500,9 @@ func moveSenderKeyShared(source *sql.DB, dest *sql.DB) error {
}
func moveGroups(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT rowid, account_uuid, group_id, master_key, revision, last_avatar_fetch, distribution_id, group_info FROM groups")
rows, err := source.Query("SELECT rowid, account_uuid, group_id, master_key, revision, last_avatar_fetch, distribution_id, group_info FROM groups" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}
@ -519,7 +538,10 @@ func moveGroups(source *sql.DB, dest *sql.DB) error {
}
func moveGroupCredentials(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, date, credential FROM group_credentials")
rows, err := source.Query("SELECT account_uuid, date, max(credential) FROM group_credentials" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" GROUP BY account_uuid, date",
)
if err != nil {
return err
}
@ -545,7 +567,8 @@ func moveGroupCredentials(source *sql.DB, dest *sql.DB) error {
func moveContacts(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, name, color, profile_key, message_expiration_time, inbox_position FROM contacts" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" AND recipient IN (SELECT DISTINCT rowid FROM recipients)",
)
if err != nil {
return err
@ -579,7 +602,10 @@ func moveContacts(source *sql.DB, dest *sql.DB) error {
}
func moveProfileKeys(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, profile_key, profile_key_credential, request_pending, unidentified_access_mode FROM profile_keys")
rows, err := source.Query("SELECT account_uuid, recipient, profile_key, profile_key_credential, request_pending, unidentified_access_mode FROM profile_keys" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" AND recipient IN (SELECT DISTINCT rowid FROM recipients)",
)
if err != nil {
return err
}
@ -611,7 +637,10 @@ func moveProfileKeys(source *sql.DB, dest *sql.DB) error {
}
func moveProfiles(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, last_update, given_name, family_name, about, emoji, payment_address, badges FROM profiles")
rows, err := source.Query("SELECT account_uuid, recipient, last_update, given_name, family_name, about, emoji, payment_address, badges FROM profiles" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" AND recipient IN (SELECT DISTINCT rowid FROM recipients)",
)
if err != nil {
return err
}
@ -646,7 +675,10 @@ func moveProfiles(source *sql.DB, dest *sql.DB) error {
}
func moveProfileCapabilities(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, recipient, storage, gv1_migration, sender_key, announcement_group, change_number, stories FROM profile_capabilities")
rows, err := source.Query("SELECT account_uuid, recipient, storage, gv1_migration, sender_key, announcement_group, change_number, stories FROM profile_capabilities" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)" +
" AND recipient IN (SELECT DISTINCT rowid FROM recipients)",
)
if err != nil {
return err
}
@ -680,7 +712,9 @@ func moveProfileCapabilities(source *sql.DB, dest *sql.DB) error {
}
func moveProfileBadges(source *sql.DB, dest *sql.DB) error {
rows, err := source.Query("SELECT account_uuid, id, category, name, description, sprite6 FROM profile_badges")
rows, err := source.Query("SELECT account_uuid, id, category, name, description, sprite6 FROM profile_badges" +
" WHERE account_uuid IN (SELECT DISTINCT uuid FROM accounts)",
)
if err != nil {
return err
}