update remainder of delete functions to behave in similar way, some other small tweaks

This commit is contained in:
kim 2024-09-13 15:41:20 +01:00
commit 2485442086
23 changed files with 555 additions and 633 deletions

View file

@ -198,7 +198,7 @@ func (c *Caches) OnInvalidateStatus(status *gtsmodel.Status) {
// the media IDs in use before the media table is // the media IDs in use before the media table is
// aware of the status ID they are linked to. // aware of the status ID they are linked to.
// //
// c.DB.Media().Invalidate("StatusID") will not work. // c.DB.Media.Invalidate("StatusID") will not work.
c.DB.Media.InvalidateIDs("ID", status.AttachmentIDs) c.DB.Media.InvalidateIDs("ID", status.AttachmentIDs)
if status.BoostOfID != "" { if status.BoostOfID != "" {

View file

@ -789,20 +789,14 @@ func (a *accountDB) UpdateAccount(ctx context.Context, account *gtsmodel.Account
} }
func (a *accountDB) DeleteAccount(ctx context.Context, id string) error { func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
defer a.state.Caches.DB.Account.Invalidate("ID", id) // Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.Account
deleted.ID = id
// Load account into cache before attempting a delete, // Delete account from database and any related links in a transaction.
// as we need it cached in order to trigger the invalidate if err := a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// callback. This in turn invalidates others.
_, err := a.GetAccountByID(gtscontext.SetBarebones(ctx), id)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
return a.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// clear out any emoji links // clear out any emoji links
if _, err := tx. if _, err := tx.
NewDelete(). NewDelete().
@ -815,11 +809,21 @@ func (a *accountDB) DeleteAccount(ctx context.Context, id string) error {
// delete the account // delete the account
_, err := tx. _, err := tx.
NewDelete(). NewDelete().
TableExpr("? AS ?", bun.Ident("accounts"), bun.Ident("account")). Model(&deleted).
Where("? = ?", bun.Ident("account.id"), id). Where("? = ?", bun.Ident("id"), id).
Returning("?", bun.Ident("uri")).
Exec(ctx) Exec(ctx)
return err return err
}) }); err != nil {
return err
}
// Invalidate cached account by its ID, manually
// call invalidate hook in case not cached.
a.state.Caches.DB.Account.Invalidate("ID", id)
a.state.Caches.OnInvalidateAccount(&deleted)
return nil
} }
func (a *accountDB) GetAccountCustomCSSByUsername(ctx context.Context, username string) (string, error) { func (a *accountDB) GetAccountCustomCSSByUsername(ctx context.Context, username string) (string, error) {

View file

@ -260,27 +260,27 @@ func (c *conversationDB) LinkConversationToStatus(ctx context.Context, conversat
} }
func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error { func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
// Load conversation into cache before attempting a delete, // Gather necessary fields from
// as we need it cached in order to trigger the invalidate // deleted for cache invaliation.
// callback. This in turn invalidates others. var deleted gtsmodel.Conversation
_, err := c.GetConversationByID(gtscontext.SetBarebones(ctx), id) deleted.ID = id
if err != nil {
if errors.Is(err, db.ErrNoEntries) { // Delete conversation from DB.
// not an issue. if _, err := c.db.NewDelete().
err = nil Model(&deleted).
} Where("? = ?", bun.Ident("id"), id).
Returning("?", bun.Ident("account_id")).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err return err
} }
// Drop this now-cached conversation on return after delete. // Invalidate cached conversation by ID,
defer c.state.Caches.DB.Conversation.Invalidate("ID", id) // manually invalidate hook in case not cached.
c.state.Caches.DB.Conversation.Invalidate("ID", id)
c.state.Caches.OnInvalidateConversation(&deleted)
// Finally delete conversation from DB. return nil
_, err = c.db.NewDelete().
Model((*gtsmodel.Conversation)(nil)).
Where("? = ?", bun.Ident("id"), id).
Exec(ctx)
return err
} }
func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error { func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {

View file

@ -20,7 +20,6 @@ package bundb
import ( import (
"context" "context"
"database/sql" "database/sql"
"errors"
"slices" "slices"
"strings" "strings"
"time" "time"
@ -70,34 +69,15 @@ func (e *emojiDB) UpdateEmoji(ctx context.Context, emoji *gtsmodel.Emoji, column
func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error { func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
var ( var (
// Gather necessary fields from
// deleted for cache invaliation.
accountIDs []string accountIDs []string
statusIDs []string statusIDs []string
) )
defer func() { // Delete the emoji and all related links to it in a singular transaction.
// Invalidate cached emoji. if err := e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
e.state.Caches.DB.Emoji.Invalidate("ID", id)
// Invalidate cached account and status IDs.
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
}()
// Load emoji into cache before attempting a delete,
// as we need it cached in order to trigger the invalidate
// callback. This in turn invalidates others.
_, err := e.GetEmojiByID(
gtscontext.SetBarebones(ctx),
id,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
return e.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete relational links between this emoji // Delete relational links between this emoji
// and any statuses using it, returning the // and any statuses using it, returning the
// status IDs so we can later update them. // status IDs so we can later update them.
@ -195,7 +175,16 @@ func (e *emojiDB) DeleteEmojiByID(ctx context.Context, id string) error {
} }
return nil return nil
}) }); err != nil {
return err
}
// Invalidate emoji, and any effected statuses / accounts.
e.state.Caches.DB.Emoji.Invalidate("ID", id)
e.state.Caches.DB.Account.InvalidateIDs("ID", accountIDs)
e.state.Caches.DB.Status.InvalidateIDs("ID", statusIDs)
return nil
} }
func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) { func (e *emojiDB) GetEmojisBy(ctx context.Context, domain string, includeDisabled bool, includeEnabled bool, shortcode string, maxShortcodeDomain string, minShortcodeDomain string, limit int) ([]*gtsmodel.Emoji, error) {

View file

@ -18,312 +18,307 @@
package bundb_test package bundb_test
import ( import (
"context"
"slices"
"testing" "testing"
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
) )
type ListTestSuite struct { type ListTestSuite struct {
BunDBStandardTestSuite BunDBStandardTestSuite
} }
func (suite *ListTestSuite) testStructs() (*gtsmodel.List, []*gtsmodel.ListEntry, *gtsmodel.Account) { // func (suite *ListTestSuite) testStructs() (*gtsmodel.List, []*gtsmodel.ListEntry, *gtsmodel.Account) {
testList := &gtsmodel.List{} // testList := &gtsmodel.List{}
*testList = *suite.testLists["local_account_1_list_1"] // *testList = *suite.testLists["local_account_1_list_1"]
// Populate entries on this list as we'd expect them back from the db. // // Populate entries on this list as we'd expect them back from the db.
entries := make([]*gtsmodel.ListEntry, 0, len(suite.testListEntries)) // entries := make([]*gtsmodel.ListEntry, 0, len(suite.testListEntries))
for _, entry := range suite.testListEntries { // for _, entry := range suite.testListEntries {
entries = append(entries, entry) // entries = append(entries, entry)
} // }
// Sort by ID descending (again, as we'd expect from the db). // // Sort by ID descending (again, as we'd expect from the db).
slices.SortFunc(entries, func(a, b *gtsmodel.ListEntry) int { // slices.SortFunc(entries, func(a, b *gtsmodel.ListEntry) int {
const k = -1 // const k = -1
switch { // switch {
case a.ID > b.ID: // case a.ID > b.ID:
return +k // return +k
case a.ID < b.ID: // case a.ID < b.ID:
return -k // return -k
default: // default:
return 0 // return 0
} // }
}) // })
testAccount := &gtsmodel.Account{} // testAccount := &gtsmodel.Account{}
*testAccount = *suite.testAccounts["local_account_1"] // *testAccount = *suite.testAccounts["local_account_1"]
return testList, entries, testAccount // return testList, entries, testAccount
} // }
func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) { // func (suite *ListTestSuite) checkList(expected *gtsmodel.List, actual *gtsmodel.List) {
suite.Equal(expected.ID, actual.ID) // suite.Equal(expected.ID, actual.ID)
suite.Equal(expected.Title, actual.Title) // suite.Equal(expected.Title, actual.Title)
suite.Equal(expected.AccountID, actual.AccountID) // suite.Equal(expected.AccountID, actual.AccountID)
suite.Equal(expected.RepliesPolicy, actual.RepliesPolicy) // suite.Equal(expected.RepliesPolicy, actual.RepliesPolicy)
suite.NotNil(actual.Account) // suite.NotNil(actual.Account)
} // }
func (suite *ListTestSuite) checkListEntry(expected *gtsmodel.ListEntry, actual *gtsmodel.ListEntry) { // func (suite *ListTestSuite) checkListEntry(expected *gtsmodel.ListEntry, actual *gtsmodel.ListEntry) {
suite.Equal(expected.ID, actual.ID) // suite.Equal(expected.ID, actual.ID)
suite.Equal(expected.ListID, actual.ListID) // suite.Equal(expected.ListID, actual.ListID)
suite.Equal(expected.FollowID, actual.FollowID) // suite.Equal(expected.FollowID, actual.FollowID)
} // }
func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, actual []*gtsmodel.ListEntry) { // func (suite *ListTestSuite) checkListEntries(expected []*gtsmodel.ListEntry, actual []*gtsmodel.ListEntry) {
var ( // var (
lExpected = len(expected) // lExpected = len(expected)
lActual = len(actual) // lActual = len(actual)
) // )
if lExpected != lActual { // if lExpected != lActual {
suite.FailNow("", "expected %d list entries, got %d", lExpected, lActual) // suite.FailNow("", "expected %d list entries, got %d", lExpected, lActual)
} // }
var topID string // var topID string
for i, expectedEntry := range expected { // for i, expectedEntry := range expected {
actualEntry := actual[i] // actualEntry := actual[i]
// Ensure ID descending. // // Ensure ID descending.
if topID == "" { // if topID == "" {
topID = actualEntry.ID // topID = actualEntry.ID
} else { // } else {
suite.Less(actualEntry.ID, topID) // suite.Less(actualEntry.ID, topID)
} // }
suite.checkListEntry(expectedEntry, actualEntry) // suite.checkListEntry(expectedEntry, actualEntry)
} // }
} // }
func (suite *ListTestSuite) TestGetListByID() { // func (suite *ListTestSuite) TestGetListByID() {
testList, _, _ := suite.testStructs() // testList, _, _ := suite.testStructs()
dbList, err := suite.db.GetListByID(context.Background(), testList.ID) // dbList, err := suite.db.GetListByID(context.Background(), testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestGetListsForAccountID() { // func (suite *ListTestSuite) TestGetListsForAccountID() {
testList, _, testAccount := suite.testStructs() // testList, _, testAccount := suite.testStructs()
dbLists, err := suite.db.GetListsByAccountID(context.Background(), testAccount.ID) // dbLists, err := suite.db.GetListsByAccountID(context.Background(), testAccount.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
if l := len(dbLists); l != 1 { // if l := len(dbLists); l != 1 {
suite.FailNow("", "expected %d lists, got %d", 1, l) // suite.FailNow("", "expected %d lists, got %d", 1, l)
} // }
suite.checkList(testList, dbLists[0]) // suite.checkList(testList, dbLists[0])
} // }
func (suite *ListTestSuite) TestPutList() { // func (suite *ListTestSuite) TestPutList() {
ctx := context.Background() // ctx := context.Background()
_, _, testAccount := suite.testStructs() // _, _, testAccount := suite.testStructs()
testList := &gtsmodel.List{ // testList := &gtsmodel.List{
ID: "01H0J2PMYM54618VCV8Y8QYAT4", // ID: "01H0J2PMYM54618VCV8Y8QYAT4",
Title: "Test List!", // Title: "Test List!",
AccountID: testAccount.ID, // AccountID: testAccount.ID,
} // }
if err := suite.db.PutList(ctx, testList); err != nil { // if err := suite.db.PutList(ctx, testList); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge testlist as though default had been set. // // Bodge testlist as though default had been set.
testList.RepliesPolicy = gtsmodel.RepliesPolicyFollowed // testList.RepliesPolicy = gtsmodel.RepliesPolicyFollowed
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestUpdateList() { // func (suite *ListTestSuite) TestUpdateList() {
ctx := context.Background() // ctx := context.Background()
testList, _, _ := suite.testStructs() // testList, _, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Now do the update. // // Now do the update.
testList.Title = "New Title!" // testList.Title = "New Title!"
if err := suite.db.UpdateList(ctx, testList, "title"); err != nil { // if err := suite.db.UpdateList(ctx, testList, "title"); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Cache should be invalidated // // Cache should be invalidated
// + we should have updated list. // // + we should have updated list.
dbList, err = suite.db.GetListByID(ctx, testList.ID) // dbList, err = suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestDeleteList() { // func (suite *ListTestSuite) TestDeleteList() {
ctx := context.Background() // ctx := context.Background()
testList, _, _ := suite.testStructs() // testList, _, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Now do the delete. // // Now do the delete.
if err := suite.db.DeleteListByID(ctx, testList.ID); err != nil { // if err := suite.db.DeleteListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Cache should be invalidated // // Cache should be invalidated
// + we should have no list. // // + we should have no list.
_, err := suite.db.GetListByID(ctx, testList.ID) // _, err := suite.db.GetListByID(ctx, testList.ID)
suite.ErrorIs(err, db.ErrNoEntries) // suite.ErrorIs(err, db.ErrNoEntries)
// All accounts / follows attached to this // // All accounts / follows attached to this
// list should now be return empty values. // // list should now be return empty values.
listAccounts, err1 := suite.db.GetAccountsInList(ctx, testList.ID, nil) // listAccounts, err1 := suite.db.GetAccountsInList(ctx, testList.ID, nil)
listFollows, err2 := suite.db.GetFollowsInList(ctx, testList.ID, nil) // listFollows, err2 := suite.db.GetFollowsInList(ctx, testList.ID, nil)
suite.NoError(err1) // suite.NoError(err1)
suite.NoError(err2) // suite.NoError(err2)
suite.Empty(listAccounts) // suite.Empty(listAccounts)
suite.Empty(listFollows) // suite.Empty(listFollows)
} // }
func (suite *ListTestSuite) TestPutListEntries() { // func (suite *ListTestSuite) TestPutListEntries() {
ctx := context.Background() // ctx := context.Background()
testList, testEntries, _ := suite.testStructs() // testList, testEntries, _ := suite.testStructs()
listEntries := []*gtsmodel.ListEntry{ // listEntries := []*gtsmodel.ListEntry{
{ // {
ID: "01H0MKMQY69HWDSDR2SWGA17R4", // ID: "01H0MKMQY69HWDSDR2SWGA17R4",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKNFRFZS8R9WV6DBX31Y03", // random id, doesn't exist // FollowID: "01H0MKNFRFZS8R9WV6DBX31Y03", // random id, doesn't exist
}, // },
{ // {
ID: "01H0MKPGQF0E7QAVW5BKTHZ630", // ID: "01H0MKPGQF0E7QAVW5BKTHZ630",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKP6RR8VEHN3GVWFBP2H30", // random id, doesn't exist // FollowID: "01H0MKP6RR8VEHN3GVWFBP2H30", // random id, doesn't exist
}, // },
{ // {
ID: "01H0MKPPP2DT68FRBMR1FJM32T", // ID: "01H0MKPPP2DT68FRBMR1FJM32T",
ListID: testList.ID, // ListID: testList.ID,
FollowID: "01H0MKQ0KA29C6NFJ27GTZD16J", // random id, doesn't exist // FollowID: "01H0MKQ0KA29C6NFJ27GTZD16J", // random id, doesn't exist
}, // },
} // }
if err := suite.db.PutListEntries(ctx, listEntries); err != nil { // if err := suite.db.PutListEntries(ctx, listEntries); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Add these entries to the test list. // // Add these entries to the test list.
testEntries = append(testEntries, listEntries...) // testEntries = append(testEntries, listEntries...)
// Now get all list entries from the db. // // Now get all list entries from the db.
// Use barebones for this because the ones // // Use barebones for this because the ones
// we just added will fail if we try to get // // we just added will fail if we try to get
// the nonexistent follows. // // the nonexistent follows.
dbListEntries, err := suite.db.GetListEntries( // dbListEntries, err := suite.db.GetListEntries(
gtscontext.SetBarebones(ctx), // gtscontext.SetBarebones(ctx),
testList.ID, // testList.ID,
"", "", "", 0) // "", "", "", 0)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
suite.checkListEntries(testList.ListEntries, dbListEntries) // suite.checkListEntries(testList.ListEntries, dbListEntries)
} // }
func (suite *ListTestSuite) TestDeleteListEntry() { // func (suite *ListTestSuite) TestDeleteListEntry() {
ctx := context.Background() // ctx := context.Background()
testList, testEntries, _ := suite.testStructs() // testList, testEntries, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Delete the first entry. // // Delete the first entry.
if err := suite.db.DeleteListEntry(ctx, // if err := suite.db.DeleteListEntry(ctx,
testEntries[0].ListID, // testEntries[0].ListID,
testEntries[0].FollowID, // testEntries[0].FollowID,
); err != nil { // ); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Get list from the db again. // // Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge the testlist as though // // Bodge the testlist as though
// we'd removed the first entry. // // we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:] // testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestDeleteAllListEntriesByFollowID() { // func (suite *ListTestSuite) TestDeleteAllListEntriesByFollowID() {
ctx := context.Background() // ctx := context.Background()
testList, testEntries, _ := suite.testStructs() // testList, testEntries, _ := suite.testStructs()
// Get List in the cache first. // // Get List in the cache first.
if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil { // if _, err := suite.db.GetListByID(ctx, testList.ID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Delete the first entry. // // Delete the first entry.
if err := suite.db.DeleteAllListEntriesByFollowIDs(ctx, testEntries[0].FollowID); err != nil { // if err := suite.db.DeleteAllListEntriesByFollowIDs(ctx, testEntries[0].FollowID); err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Get list from the db again. // // Get list from the db again.
dbList, err := suite.db.GetListByID(ctx, testList.ID) // dbList, err := suite.db.GetListByID(ctx, testList.ID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
// Bodge the testlist as though // // Bodge the testlist as though
// we'd removed the first entry. // // we'd removed the first entry.
testList.ListEntries = testList.ListEntries[1:] // testList.ListEntries = testList.ListEntries[1:]
suite.checkList(testList, dbList) // suite.checkList(testList, dbList)
} // }
func (suite *ListTestSuite) TestListIncludesAccount() { // func (suite *ListTestSuite) TestListIncludesAccount() {
ctx := context.Background() // ctx := context.Background()
testList, _, _ := suite.testStructs() // testList, _, _ := suite.testStructs()
for accountID, expected := range map[string]bool{ // for accountID, expected := range map[string]bool{
suite.testAccounts["admin_account"].ID: true, // suite.testAccounts["admin_account"].ID: true,
suite.testAccounts["local_account_1"].ID: false, // suite.testAccounts["local_account_1"].ID: false,
suite.testAccounts["local_account_2"].ID: true, // suite.testAccounts["local_account_2"].ID: true,
"01H7074GEZJ56J5C86PFB0V2CT": false, // "01H7074GEZJ56J5C86PFB0V2CT": false,
} { // } {
includes, err := suite.db.IsAccountInList(ctx, testList.ID, accountID) // includes, err := suite.db.IsAccountInList(ctx, testList.ID, accountID)
if err != nil { // if err != nil {
suite.FailNow(err.Error()) // suite.FailNow(err.Error())
} // }
if includes != expected { // if includes != expected {
suite.FailNow("", "expected %t for accountID %s got %t", expected, accountID, includes) // suite.FailNow("", "expected %t for accountID %s got %t", expected, accountID, includes)
} // }
} // }
} // }
func TestListTestSuite(t *testing.T) { func TestListTestSuite(t *testing.T) {
suite.Run(t, new(ListTestSuite)) suite.Run(t, new(ListTestSuite))

View file

@ -24,7 +24,6 @@ import (
"time" "time"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/paging" "github.com/superseriousbusiness/gotosocial/internal/paging"
@ -122,30 +121,38 @@ func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAtt
} }
func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error { func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
// Load media into cache before attempting a delete, // Gather necessary fields from
// as we need it cached in order to trigger the invalidate // deleted for cache invaliation.
// callback. This in turn invalidates others. var deleted gtsmodel.MediaAttachment
media, err := m.GetAttachmentByID(gtscontext.SetBarebones(ctx), id) deleted.ID = id
if err != nil {
if errors.Is(err, db.ErrNoEntries) { // Delete media attachment and update related models in new transaction.
// not an issue. err := m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
err = nil
// Initially, delete the media model,
// returning the required fields we need.
if _, err := tx.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id).
Returning("?, ?, ?, ?",
bun.Ident("account_id"),
bun.Ident("status_id"),
bun.Ident("avatar"),
bun.Ident("header"),
).
Exec(ctx); err != nil {
return gtserror.Newf("error deleting media: %w", err)
} }
return err
}
// On return, ensure that media with ID is invalidated. // If media was attached to account,
defer m.state.Caches.DB.Media.Invalidate("ID", id) // we need to remove link from account.
if deleted.AccountID != "" {
// Delete media attachment in new transaction.
err = m.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
if media.AccountID != "" {
var account gtsmodel.Account var account gtsmodel.Account
// Get related account model. // Get related account model.
if _, err := tx.NewSelect(). if _, err := tx.NewSelect().
Model(&account). Model(&account).
Where("? = ?", bun.Ident("id"), media.AccountID). Where("? = ?", bun.Ident("id"), deleted.AccountID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) { Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting account: %w", err) return gtserror.Newf("error selecting account: %w", err)
} }
@ -153,11 +160,11 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
var set func(*bun.UpdateQuery) *bun.UpdateQuery var set func(*bun.UpdateQuery) *bun.UpdateQuery
switch { switch {
case *media.Avatar && account.AvatarMediaAttachmentID == id: case *deleted.Avatar && account.AvatarMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery { set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id")) return q.Set("? = NULL", bun.Ident("avatar_media_attachment_id"))
} }
case *media.Header && account.HeaderMediaAttachmentID == id: case *deleted.Header && account.HeaderMediaAttachmentID == id:
set = func(q *bun.UpdateQuery) *bun.UpdateQuery { set = func(q *bun.UpdateQuery) *bun.UpdateQuery {
return q.Set("? = NULL", bun.Ident("header_media_attachment_id")) return q.Set("? = NULL", bun.Ident("header_media_attachment_id"))
} }
@ -176,13 +183,15 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
} }
} }
if media.StatusID != "" { // If media was attached to a status,
// we need to remove link from status.
if deleted.StatusID != "" {
var status gtsmodel.Status var status gtsmodel.Status
// Get related status model. // Get related status model.
if _, err := tx.NewSelect(). if _, err := tx.NewSelect().
Model(&status). Model(&status).
Where("? = ?", bun.Ident("id"), media.StatusID). Where("? = ?", bun.Ident("id"), deleted.StatusID).
Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) { Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return gtserror.Newf("error selecting status: %w", err) return gtserror.Newf("error selecting status: %w", err)
} }
@ -206,17 +215,14 @@ func (m *mediaDB) DeleteAttachment(ctx context.Context, id string) error {
} }
} }
// Finally delete this media.
if _, err := tx.NewDelete().
Table("media_attachments").
Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil {
return gtserror.Newf("error deleting media: %w", err)
}
return nil return nil
}) })
// Invalidate cached media with ID, manually
// call invalidate hook in case not in cache.
m.state.Caches.DB.Media.Invalidate("ID", id)
m.state.Caches.OnInvalidateMedia(&deleted)
return err return err
} }

View file

@ -159,24 +159,18 @@ func (m *mentionDB) PutMention(ctx context.Context, mention *gtsmodel.Mention) e
} }
func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error { func (m *mentionDB) DeleteMentionByID(ctx context.Context, id string) error {
defer m.state.Caches.DB.Mention.Invalidate("ID", id) // Delete mention with given ID,
// returning the deleted models.
// Load mention into cache before attempting a delete, if _, err := m.db.NewDelete().
// as we need it cached in order to trigger the invalidate Table("mentions").
// callback. This in turn invalidates others. Where("? = ?", bun.Ident("id"), id).
_, err := m.GetMention(gtscontext.SetBarebones(ctx), id) Exec(ctx); err != nil &&
if err != nil { !errors.Is(err, db.ErrNoEntries) {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
return err return err
} }
// Finally delete mention from DB. // Invalidate the cached mention with ID.
_, err = m.db.NewDelete(). m.state.Caches.DB.Mention.Invalidate("ID", id)
Table("mentions").
Where("? = ?", bun.Ident("id"), id). return nil
Exec(ctx)
return err
} }

View file

@ -234,13 +234,17 @@ func (m *moveDB) UpdateMove(ctx context.Context, move *gtsmodel.Move, columns ..
} }
func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error { func (m *moveDB) DeleteMoveByID(ctx context.Context, id string) error {
defer m.state.Caches.DB.Move.Invalidate("ID", id) // Delete move with given ID.
if _, err := m.db.NewDelete().
_, err := m.db.
NewDelete().
TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")). TableExpr("? AS ?", bun.Ident("moves"), bun.Ident("move")).
Where("? = ?", bun.Ident("move.id"), id). Where("? = ?", bun.Ident("move.id"), id).
Exec(ctx) Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return nil
}
return err // Invalidate the cached move model with ID.
m.state.Caches.DB.Move.Invalidate("ID", id)
return nil
} }

View file

@ -22,6 +22,7 @@ import (
"errors" "errors"
"slices" "slices"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext" "github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtserror" "github.com/superseriousbusiness/gotosocial/internal/gtserror"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@ -292,7 +293,8 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
NewDelete(). NewDelete().
Table("notifications"). Table("notifications").
Where("? = ?", bun.Ident("id"), id). Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil { Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err return err
} }
@ -303,7 +305,7 @@ func (n *notificationDB) DeleteNotificationByID(ctx context.Context, id string)
func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error { func (n *notificationDB) DeleteNotifications(ctx context.Context, types []string, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" { if targetAccountID == "" && originAccountID == "" {
return errors.New("DeleteNotifications: one of targetAccountID or originAccountID must be set") return gtserror.New("one of targetAccountID or originAccountID must be set")
} }
q := n.db. q := n.db.

View file

@ -181,13 +181,20 @@ func (p *pollDB) DeletePollByID(ctx context.Context, id string) error {
if _, err := p.db.NewDelete(). if _, err := p.db.NewDelete().
Table("polls"). Table("polls").
Where("? = ?", bun.Ident("id"), id). Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil { Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err return err
} }
// Invalidate poll by ID from cache. // Wrap provided ID in a poll
// model for calling cache hook.
var deleted gtsmodel.Poll
deleted.ID = id
// Invalidate cached poll with ID, manually
// call invalidate hook in case not cached.
p.state.Caches.DB.Poll.Invalidate("ID", id) p.state.Caches.DB.Poll.Invalidate("ID", id)
p.state.Caches.DB.PollVoteIDs.Invalidate(id) p.state.Caches.OnInvalidatePoll(&deleted)
return nil return nil
} }
@ -384,148 +391,44 @@ func (p *pollDB) PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
}) })
} }
func (p *pollDB) DeletePollVotes(ctx context.Context, pollID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete all votes in poll.
res, err := tx.NewDelete().
Table("poll_votes").
Where("? = ?", bun.Ident("poll_id"), pollID).
Exec(ctx)
if err != nil {
// irrecoverable
return err
}
ra, err := res.RowsAffected()
if err != nil {
// irrecoverable
return err
}
if ra == 0 {
// No poll votes deleted,
// nothing to update.
return nil
}
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no votes found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Zero all counts.
poll.ResetVotes()
// Finally, update the poll entry.
_, err = tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
return err
})
if err != nil {
return err
}
// Invalidate poll vote and poll entry from caches.
p.state.Caches.DB.Poll.Invalidate("ID", pollID)
p.state.Caches.DB.PollVote.Invalidate("PollID", pollID)
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID)
return nil
}
func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error { func (p *pollDB) DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error {
err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error { // Gather necessary fields from
// Slice should only ever be of length // deleted for cache invaliation.
// 0 or 1; it's a slice of slices only var deleted gtsmodel.PollVote
// because we can't LIMIT deletes to 1. deleted.AccountID = accountID
var choicesSlice [][]int deleted.PollID = pollID
// Delete the poll vote with given poll and account IDs, and update vote counts.
if err := p.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Delete vote in poll by account, // Delete vote in poll by account,
// returning the ID + choices of the vote. // returning deleted model info.
if err := tx.NewDelete(). switch _, err := tx.NewDelete().
Table("poll_votes"). Model(&deleted).
Where("? = ?", bun.Ident("poll_id"), pollID). Where("? = ?", bun.Ident("poll_id"), pollID).
Where("? = ?", bun.Ident("account_id"), accountID). Where("? = ?", bun.Ident("account_id"), accountID).
Returning("?", bun.Ident("choices")). Returning("?", bun.Ident("choices")).
Scan(ctx, &choicesSlice); err != nil { Exec(ctx); {
// irrecoverable.
return err
}
if len(choicesSlice) != 1 {
// No poll votes by this
// acct on this poll.
return nil
}
// Extract the *actual* choices.
choices := choicesSlice[0]
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Scan(ctx); {
case err == nil: case err == nil:
// no issue. // no issue
case errors.Is(err, db.ErrNoEntries): case errors.Is(err, db.ErrNoEntries):
// no poll found,
// return here.
return nil return nil
default: default:
// irrecoverable.
return err return err
} }
// Decrement votes for choices. // Update the votes for this deleted poll.
poll.DecrementVotes(choices) err := updatePollCounts(ctx, tx, &deleted)
// Finally, update the poll entry.
_, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), pollID).
Exec(ctx)
return err return err
}) }); err != nil {
if err != nil {
return err return err
} }
// Invalidate poll vote and poll entry from caches. // Invalidate the poll vote cache by given poll + account IDs, also
p.state.Caches.DB.Poll.Invalidate("ID", pollID) // manually call invalidation hook in case not actually stored in cache.
p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID) p.state.Caches.DB.PollVote.Invalidate("PollID,AccountID", pollID, accountID)
p.state.Caches.DB.PollVoteIDs.Invalidate(pollID) p.state.Caches.OnInvalidatePollVote(&deleted)
return nil return nil
} }
@ -555,6 +458,48 @@ func (p *pollDB) DeletePollVotesByAccountID(ctx context.Context, accountID strin
return nil return nil
} }
// updatePollCounts updates the vote counts on a poll for the given deleted PollVote model.
func updatePollCounts(ctx context.Context, tx bun.Tx, deleted *gtsmodel.PollVote) error {
// Select current poll counts from DB,
// taking minimal columns needed to
// increment/decrement votes.
var poll gtsmodel.Poll
switch err := tx.NewSelect().
Model(&poll).
Column("options", "votes", "voters").
Where("? = ?", bun.Ident("id"), deleted.PollID).
Scan(ctx); {
case err == nil:
// no issue.
case errors.Is(err, db.ErrNoEntries):
// no poll found,
// return here.
return nil
default:
// irrecoverable.
return err
}
// Decrement votes for these choices.
poll.DecrementVotes(deleted.Choices)
// Finally, update the poll entry.
if _, err := tx.NewUpdate().
Model(&poll).
Column("votes", "voters").
Where("? = ?", bun.Ident("id"), deleted.PollID).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err
}
return nil
}
// newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID. // newSelectPollVotes returns a new select query for all rows in the poll_votes table with poll_id = pollID.
func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery { func newSelectPollVotes(db *bun.DB, pollID string) *bun.SelectQuery {
return db.NewSelect(). return db.NewSelect().

View file

@ -26,7 +26,6 @@ import (
"github.com/stretchr/testify/suite" "github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db" "github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id" "github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/util" "github.com/superseriousbusiness/gotosocial/internal/util"
@ -286,41 +285,6 @@ func (suite *PollTestSuite) TestDeletePoll() {
} }
} }
func (suite *PollTestSuite) TestDeletePollVotes() {
// Create a new context for this test.
ctx, cncl := context.WithCancel(context.Background())
defer cncl()
for _, poll := range suite.testPolls {
// Delete votes associated with poll from database.
err := suite.db.DeletePollVotes(ctx, poll.ID)
suite.NoError(err)
// Fetch latest version of poll from database.
poll, err = suite.db.GetPollByID(
gtscontext.SetBarebones(ctx),
poll.ID,
)
suite.NoError(err)
// Check that poll counts are all zero.
suite.Equal(*poll.Voters, 0)
suite.Equal(make([]int, len(poll.Options)), poll.Votes)
}
}
func (suite *PollTestSuite) TestDeletePollVotesNoPoll() {
// Create a new context for this test.
ctx, cncl := context.WithCancel(context.Background())
defer cncl()
// Try to delete votes of nonexistent poll.
nonPollID := "01HF6V4XWTSZWJ80JNPPDTD4DB"
err := suite.db.DeletePollVotes(ctx, nonPollID)
suite.NoError(err)
}
func (suite *PollTestSuite) TestDeletePollVotesBy() { func (suite *PollTestSuite) TestDeletePollVotesBy() {
ctx, cncl := context.WithCancel(context.Background()) ctx, cncl := context.WithCancel(context.Background())
defer cncl() defer cncl()

View file

@ -248,45 +248,36 @@ func (r *reportDB) PutReport(ctx context.Context, report *gtsmodel.Report) error
}) })
} }
func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) { func (r *reportDB) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error {
// Update the report's last-updated // Update the report's last-updated
report.UpdatedAt = time.Now() report.UpdatedAt = time.Now()
if len(columns) != 0 { if len(columns) != 0 {
columns = append(columns, "updated_at") columns = append(columns, "updated_at")
} }
if _, err := r.db. return r.state.Caches.DB.Report.Store(report, func() error {
NewUpdate(). _, err := r.db.
Model(report). NewUpdate().
Where("? = ?", bun.Ident("report.id"), report.ID). Model(report).
Column(columns...). Where("? = ?", bun.Ident("report.id"), report.ID).
Exec(ctx); err != nil { Column(columns...).
return nil, err Exec(ctx)
} return err
})
r.state.Caches.DB.Report.Invalidate("ID", report.ID)
return report, nil
} }
func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error { func (r *reportDB) DeleteReportByID(ctx context.Context, id string) error {
defer r.state.Caches.DB.Report.Invalidate("ID", id) // Delete the report from DB.
if _, err := r.db.NewDelete().
// Load status into cache before attempting a delete, TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
// as we need it cached in order to trigger the invalidate Where("? = ?", bun.Ident("report.id"), id).
// callback. This in turn invalidates others. Exec(ctx); err != nil &&
_, err := r.GetReportByID(gtscontext.SetBarebones(ctx), id) !errors.Is(err, db.ErrNoEntries) {
if err != nil {
if errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
return err return err
} }
// Finally delete report from DB. // Invalidate any cached report model by ID.
_, err = r.db.NewDelete(). r.state.Caches.DB.Report.Invalidate("ID", id)
TableExpr("? AS ?", bun.Ident("reports"), bun.Ident("report")).
Where("? = ?", bun.Ident("report.id"), id). return nil
Exec(ctx)
return err
} }

View file

@ -202,7 +202,7 @@ func (suite *ReportTestSuite) TestUpdateReport() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00") report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
if _, err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil { if err := suite.db.UpdateReport(ctx, report, "action_taken", "action_taken_by_account_id", "action_taken_at"); err != nil {
suite.FailNow(err.Error()) suite.FailNow(err.Error())
} }
@ -228,7 +228,7 @@ func (suite *ReportTestSuite) TestUpdateReportAllColumns() {
report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID report.ActionTakenByAccountID = suite.testAccounts["admin_account"].ID
report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00") report.ActionTakenAt = testrig.TimeMustParse("2022-05-14T12:20:03+02:00")
if _, err := suite.db.UpdateReport(ctx, report); err != nil { if err := suite.db.UpdateReport(ctx, report); err != nil {
suite.FailNow(err.Error()) suite.FailNow(err.Error())
} }

View file

@ -19,8 +19,10 @@ package bundb
import ( import (
"context" "context"
"errors"
"time" "time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel" "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/state" "github.com/superseriousbusiness/gotosocial/internal/state"
"github.com/uptrace/bun" "github.com/uptrace/bun"
@ -110,13 +112,18 @@ func (s *sinBinStatusDB) UpdateSinBinStatus(
} }
func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error { func (s *sinBinStatusDB) DeleteSinBinStatusByID(ctx context.Context, id string) error {
// On return ensure status invalidated from cache. // Delete the status from DB.
defer s.state.Caches.DB.SinBinStatus.Invalidate("ID", id) if _, err := s.db.
_, err := s.db.
NewDelete(). NewDelete().
TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")). TableExpr("? AS ?", bun.Ident("sin_bin_statuses"), bun.Ident("sin_bin_status")).
Where("? = ?", bun.Ident("sin_bin_status.id"), id). Where("? = ?", bun.Ident("sin_bin_status.id"), id).
Exec(ctx) Exec(ctx); err != nil &&
return err !errors.Is(err, db.ErrNoEntries) {
return err
}
// Invalidate any cached sinbin status model by ID.
s.state.Caches.DB.SinBinStatus.Invalidate("ID", id)
return nil
} }

View file

@ -479,24 +479,13 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
} }
func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error { func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
// Load status into cache before attempting a delete, // Gather necessary fields from
// as we need it cached in order to trigger the invalidate // deleted for cache invaliation.
// callback. This in turn invalidates others. var deleted gtsmodel.Status
_, err := s.GetStatusByID( deleted.ID = id
gtscontext.SetBarebones(ctx),
id,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
// NOTE: even if db.ErrNoEntries is returned, we
// still run the below transaction to ensure related
// objects are appropriately deleted.
return err
}
// On return ensure status invalidated from cache. // Delete status from database and any related links in a transaction.
defer s.state.Caches.DB.Status.Invalidate("ID", id) if err := s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
return s.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// delete links between this status and any emojis it uses // delete links between this status and any emojis it uses
if _, err := tx. if _, err := tx.
NewDelete(). NewDelete().
@ -517,26 +506,42 @@ func (s *statusDB) DeleteStatusByID(ctx context.Context, id string) error {
// Delete links between this status // Delete links between this status
// and any threads it was a part of. // and any threads it was a part of.
_, err = tx. if _, err := tx.
NewDelete(). NewDelete().
TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")). TableExpr("? AS ?", bun.Ident("thread_to_statuses"), bun.Ident("thread_to_status")).
Where("? = ?", bun.Ident("thread_to_status.status_id"), id). Where("? = ?", bun.Ident("thread_to_status.status_id"), id).
Exec(ctx) Exec(ctx); err != nil {
if err != nil {
return err return err
} }
// delete the status itself // delete the status itself
if _, err := tx. if _, err := tx.
NewDelete(). NewDelete().
TableExpr("? AS ?", bun.Ident("statuses"), bun.Ident("status")). Model(&deleted).
Where("? = ?", bun.Ident("status.id"), id). Where("? = ?", bun.Ident("id"), id).
Exec(ctx); err != nil { Returning("?, ?, ?, ?, ?",
bun.Ident("account_id"),
bun.Ident("boost_of_id"),
bun.Ident("in_reply_to_id"),
bun.Ident("attachments"),
bun.Ident("poll_id"),
).
Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err return err
} }
return nil return nil
}) }); err != nil {
return err
}
// Invalidate cached status by its ID, manually
// call the invalidate hook in case not cached.
s.state.Caches.DB.Status.Invalidate("ID", id)
s.state.Caches.OnInvalidateStatus(&deleted)
return nil
} }
func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) { func (s *statusDB) GetStatusesUsingEmoji(ctx context.Context, emojiID string) ([]*gtsmodel.Status, error) {

View file

@ -257,60 +257,85 @@ func (s *statusBookmarkDB) PutStatusBookmark(ctx context.Context, bookmark *gtsm
} }
func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error { func (s *statusBookmarkDB) DeleteStatusBookmarkByID(ctx context.Context, id string) error {
_, err := s.db. // Gather necessary fields from
NewDelete(). // deleted for cache invaliation.
Table("status_bookmarks"). var deleted gtsmodel.StatusBookmark
deleted.ID = id
// Delete block with given URI,
// returning the deleted models.
if _, err := s.db.NewDelete().
Model(&deleted).
Where("? = ?", bun.Ident("id"), id). Where("? = ?", bun.Ident("id"), id).
Exec(ctx) Returning("?", bun.Ident("status_id")).
if err != nil { Exec(ctx); err != nil &&
!errors.Is(err, db.ErrNoEntries) {
return err return err
} }
// Invalidate cached status bookmark by its ID,
// manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("ID", id) s.state.Caches.DB.StatusBookmark.Invalidate("ID", id)
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
return nil return nil
} }
func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error { func (s *statusBookmarkDB) DeleteStatusBookmarks(ctx context.Context, targetAccountID string, originAccountID string) error {
if targetAccountID == "" && originAccountID == "" { if targetAccountID == "" && originAccountID == "" {
return errors.New("DeleteBookmarks: one of targetAccountID or originAccountID must be set") return gtserror.New("one of targetAccountID or originAccountID must be set")
} }
// Gather necessary fields from
// deleted for cache invaliation.
var deleted []*gtsmodel.StatusBookmark
q := s.db. q := s.db.
NewDelete(). NewDelete().
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")) Model(&deleted).
Returning("?", bun.Ident("status_id"))
if targetAccountID != "" { if targetAccountID != "" {
q = q.Where("? = ?", bun.Ident("status_bookmark.target_account_id"), targetAccountID) q = q.Where("? = ?", bun.Ident("target_account_id"), targetAccountID)
defer s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID)
} }
if originAccountID != "" { if originAccountID != "" {
q = q.Where("? = ?", bun.Ident("status_bookmark.account_id"), originAccountID) q = q.Where("? = ?", bun.Ident("account_id"), originAccountID)
defer s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
} }
if _, err := q.Exec(ctx); err != nil { if _, err := q.Exec(ctx); err != nil {
return err return err
} }
if targetAccountID != "" { for _, deleted := range deleted {
s.state.Caches.DB.StatusBookmark.Invalidate("TargetAccountID", targetAccountID) // Invalidate cached status bookmark by status ID,
} // manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", deleted.StatusID)
if originAccountID != "" { s.state.Caches.OnInvalidateStatusBookmark(deleted)
s.state.Caches.DB.StatusBookmark.Invalidate("AccountID", originAccountID)
} }
return nil return nil
} }
func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error { func (s *statusBookmarkDB) DeleteStatusBookmarksForStatus(ctx context.Context, statusID string) error {
q := s.db. // Delete status bookmarks
NewDelete(). // from database by status ID.
q := s.db.NewDelete().
TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")). TableExpr("? AS ?", bun.Ident("status_bookmarks"), bun.Ident("status_bookmark")).
Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID) Where("? = ?", bun.Ident("status_bookmark.status_id"), statusID)
if _, err := q.Exec(ctx); err != nil { if _, err := q.Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
return err return err
} }
// Wrap provided ID in a bookmark
// model for calling cache hook.
var deleted gtsmodel.StatusBookmark
deleted.StatusID = statusID
// Invalidate cached status bookmark by status ID,
// manually call invalidate hook in case not cached.
s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID) s.state.Caches.DB.StatusBookmark.Invalidate("StatusID", statusID)
s.state.Caches.OnInvalidateStatusBookmark(&deleted)
return nil return nil
} }

View file

@ -67,12 +67,14 @@ func (t *tombstoneDB) PutTombstone(ctx context.Context, tombstone *gtsmodel.Tomb
} }
func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error { func (t *tombstoneDB) DeleteTombstone(ctx context.Context, id string) error {
defer t.state.Caches.DB.Tombstone.Invalidate("ID", id)
// Delete tombstone from DB. // Delete tombstone from DB.
_, err := t.db.NewDelete(). _, err := t.db.NewDelete().
TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")). TableExpr("? AS ?", bun.Ident("tombstones"), bun.Ident("tombstone")).
Where("? = ?", bun.Ident("tombstone.id"), id). Where("? = ?", bun.Ident("tombstone.id"), id).
Exec(ctx) Exec(ctx)
// Invalidate any cached tombstone by given ID.
t.state.Caches.DB.Tombstone.Invalidate("ID", id)
return err return err
} }

View file

@ -209,26 +209,26 @@ func (u *userDB) UpdateUser(ctx context.Context, user *gtsmodel.User, columns ..
} }
func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error { func (u *userDB) DeleteUserByID(ctx context.Context, userID string) error {
defer u.state.Caches.DB.User.Invalidate("ID", userID) // Gather necessary fields from
// deleted for cache invaliation.
var deleted gtsmodel.User
deleted.ID = userID
// Load user into cache before attempting a delete, // Delete user from DB.
// as we need it cached in order to trigger the invalidate if _, err := u.db.NewDelete().
// callback. This in turn invalidates others. Model(&deleted).
_, err := u.GetUserByID(gtscontext.SetBarebones(ctx), userID) Where("? = ?", bun.Ident("user.id"), userID).
if err != nil { Returning("?", bun.Ident("user.account_id")).
if errors.Is(err, db.ErrNoEntries) { Exec(ctx); err != nil && !errors.Is(err, db.ErrNoEntries) {
// not an issue.
err = nil
}
return err return err
} }
// Finally delete user from DB. // Invalidate cached user by ID, manually
_, err = u.db.NewDelete(). // call invalidate hook in case not cached.
TableExpr("? AS ?", bun.Ident("users"), bun.Ident("user")). u.state.Caches.DB.User.Invalidate("ID", userID)
Where("? = ?", bun.Ident("user.id"), userID). u.state.Caches.OnInvalidateUser(&deleted)
Exec(ctx)
return err return nil
} }
func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error { func (u *userDB) PutDeniedUser(ctx context.Context, deniedUser *gtsmodel.DeniedUser) error {

View file

@ -57,9 +57,6 @@ type Poll interface {
// PutPollVote puts the given PollVote in the database. // PutPollVote puts the given PollVote in the database.
PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error PutPollVote(ctx context.Context, vote *gtsmodel.PollVote) error
// DeletePollVotes deletes all PollVotes in Poll with given ID from the database.
DeletePollVotes(ctx context.Context, pollID string) error
// DeletePollVoteBy deletes the PollVote in Poll with ID, by account ID, from the database. // DeletePollVoteBy deletes the PollVote in Poll with ID, by account ID, from the database.
DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error DeletePollVoteBy(ctx context.Context, pollID string, accountID string) error

View file

@ -44,7 +44,7 @@ type Report interface {
// provided, then all columns will be updated. // provided, then all columns will be updated.
// updated_at will also be updated, no need to pass this // updated_at will also be updated, no need to pass this
// as a specific column. // as a specific column.
UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) (*gtsmodel.Report, error) UpdateReport(ctx context.Context, report *gtsmodel.Report, columns ...string) error
// DeleteReportByID deletes report with the given id. // DeleteReportByID deletes report with the given id.
DeleteReportByID(ctx context.Context, id string) error DeleteReportByID(ctx context.Context, id string) error

View file

@ -826,9 +826,6 @@ func (d *Dereferencer) fetchStatusPoll(
if err := d.state.DB.DeletePollByID(ctx, pollID); err != nil { if err := d.state.DB.DeletePollByID(ctx, pollID); err != nil {
return gtserror.Newf("error deleting existing poll from database: %w", err) return gtserror.Newf("error deleting existing poll from database: %w", err)
} }
if err := d.state.DB.DeletePollVotes(ctx, pollID); err != nil {
return gtserror.Newf("error deleting existing votes from database: %w", err)
}
return nil return nil
} }
) )

View file

@ -142,7 +142,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
columns = append(columns, "action_taken") columns = append(columns, "action_taken")
} }
updatedReport, err := p.state.DB.UpdateReport(ctx, report, columns...) err = p.state.DB.UpdateReport(ctx, report, columns...)
if err != nil { if err != nil {
return nil, gtserror.NewErrorInternalError(err) return nil, gtserror.NewErrorInternalError(err)
} }
@ -156,7 +156,7 @@ func (p *Processor) ReportResolve(ctx context.Context, account *gtsmodel.Account
Target: report.Account, Target: report.Account,
}) })
apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, updatedReport, account) apimodelReport, err := p.converter.ReportToAdminAPIReport(ctx, report, account)
if err != nil { if err != nil {
return nil, gtserror.NewErrorInternalError(err) return nil, gtserror.NewErrorInternalError(err)
} }

View file

@ -126,11 +126,6 @@ func (u *utils) wipeStatus(
errs.Appendf("error deleting status poll: %w", err) errs.Appendf("error deleting status poll: %w", err)
} }
// Delete any poll votes pointing to this poll ID.
if err := u.state.DB.DeletePollVotes(ctx, pollID); err != nil {
errs.Appendf("error deleting status poll votes: %w", err)
}
// Cancel any scheduled expiry task for poll. // Cancel any scheduled expiry task for poll.
_ = u.state.Workers.Scheduler.Cancel(pollID) _ = u.state.Workers.Scheduler.Cancel(pollID)
} }