remove media_attachments.updated_at column

This commit is contained in:
kim 2024-12-03 12:51:48 +00:00
commit 736d45db7f
11 changed files with 134 additions and 90 deletions

View file

@ -19,7 +19,6 @@ package model
import (
"io"
"time"
"github.com/superseriousbusiness/gotosocial/internal/storage"
)
@ -30,8 +29,6 @@ type Content struct {
ContentType string
// ContentLength in bytes
ContentLength int64
// Time when the content was last updated.
ContentUpdated time.Time
// Actual content
Content io.ReadCloser
// Resource URL to forward to if the file can be fetched from the storage directly (e.g signed S3 URL)

View file

@ -505,7 +505,6 @@ func sizeofMedia() uintptr {
URL: exampleURI,
RemoteURL: exampleURI,
CreatedAt: exampleTime,
UpdatedAt: exampleTime,
Type: gtsmodel.FileTypeImage,
AccountID: exampleID,
Description: exampleText,

View file

@ -104,12 +104,6 @@ func (m *mediaDB) PutAttachment(ctx context.Context, media *gtsmodel.MediaAttach
}
func (m *mediaDB) UpdateAttachment(ctx context.Context, media *gtsmodel.MediaAttachment, columns ...string) error {
media.UpdatedAt = time.Now()
if len(columns) > 0 {
// If we're updating by column, ensure "updated_at" is included.
columns = append(columns, "updated_at")
}
return m.state.Caches.DB.Media.Store(media, func() error {
_, err := m.db.NewUpdate().
Model(media).

View file

@ -0,0 +1,57 @@
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package migrations
import (
"context"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/uptrace/bun"
)
func init() {
up := func(ctx context.Context, db *bun.DB) error {
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
// Check for 'updated_at' column on media attachments table, else return.
exists, err := doesColumnExist(ctx, tx, "media_attachments", "updated_at")
if err != nil {
return err
} else if !exists {
return nil
}
// Remove 'updated_at' column.
_, err = tx.NewDropColumn().
Model((*gtsmodel.MediaAttachment)(nil)).
Column("updated_at").
Exec(ctx)
return err
})
}
down := func(ctx context.Context, db *bun.DB) error {
return db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
return nil
})
}
if err := Migrations.Register(up, down); err != nil {
panic(err)
}
}

View file

@ -21,7 +21,6 @@ import (
"context"
"errors"
"slices"
"time"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
@ -365,14 +364,14 @@ func (s *statusDB) PutStatus(ctx context.Context, status *gtsmodel.Status) error
}
}
// change the status ID of the media attachments to the new status
// change the status ID of the media
// attachments to the current status
for _, a := range status.Attachments {
a.StatusID = status.ID
a.UpdatedAt = time.Now()
if _, err := tx.
NewUpdate().
Model(a).
Column("status_id", "updated_at").
Column("status_id").
Where("? = ?", bun.Ident("media_attachment.id"), a.ID).
Exec(ctx); err != nil {
if !errors.Is(err, db.ErrAlreadyExists) {
@ -399,7 +398,9 @@ func (s *statusDB) PutStatus(ctx context.Context, status *gtsmodel.Status) error
}
// Finally, insert the status
_, err := tx.NewInsert().Model(status).Exec(ctx)
_, err := tx.NewInsert().
Model(status).
Exec(ctx)
return err
})
})
@ -443,13 +444,14 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
}
}
// change the status ID of the media attachments to the new status
// change the status ID of the media
// attachments to the current status.
for _, a := range status.Attachments {
a.StatusID = status.ID
a.UpdatedAt = time.Now()
if _, err := tx.
NewUpdate().
Model(a).
Column("status_id").
Where("? = ?", bun.Ident("media_attachment.id"), a.ID).
Exec(ctx); err != nil {
if !errors.Is(err, db.ErrAlreadyExists) {
@ -476,8 +478,7 @@ func (s *statusDB) UpdateStatus(ctx context.Context, status *gtsmodel.Status, co
}
// Finally, update the status
_, err := tx.
NewUpdate().
_, err := tx.NewUpdate().
Model(status).
Column(columns...).
Where("? = ?", bun.Ident("status.id"), status.ID).

View file

@ -267,7 +267,17 @@ func (suite *StatusTestSuite) TestDereferencerGetStatusUpdated() {
editedAt time.Time
)
// Edit the "remote" status content.
// Perform any edits to the remote status.
editedContent = "updated status content!"
editedContentWarning = "CW: edited status content"
editedLanguage = testStatus.Language // no change
editedSensitive = *testStatus.Sensitive // no change
editedAttachmentIDs = testStatus.AttachmentIDs // no change
editedPollOptions = getPollOptions(testStatus) // no change
editedPollVotes = getPollVotes(testStatus) // no change
editedAt = time.Now()
// Edit the "remote" statusable obj.
suite.editStatusable(testStatusable,
editedContent,
editedContentWarning,
@ -323,19 +333,9 @@ func (suite *StatusTestSuite) TestDereferencerGetStatusUpdated() {
Language: testStatus.Language,
Sensitive: testStatus.Sensitive,
AttachmentIDs: testStatus.AttachmentIDs,
PollOptions: func() []string {
if testStatus.Poll != nil {
return testStatus.Poll.Options
}
return nil
}(),
PollVotes: func() []int {
if testStatus.Poll != nil {
return testStatus.Poll.Votes
}
return nil
}(),
CreatedAt: testStatus.CreatedAt,
PollOptions: getPollOptions(testStatus),
PollVotes: getPollVotes(testStatus),
CreatedAt: testStatus.CreatedAt,
},
)
}
@ -372,7 +372,17 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusUpdated() {
editedAt time.Time
)
// Edit the "remote" status content.
// Perform any edits to the remote status.
editedContent = "updated status content!"
editedContentWarning = "CW: edited status content"
editedLanguage = testStatus.Language // no change
editedSensitive = *testStatus.Sensitive // no change
editedAttachmentIDs = testStatus.AttachmentIDs // no change
editedPollOptions = getPollOptions(testStatus) // no change
editedPollVotes = getPollVotes(testStatus) // no change
editedAt = time.Now()
// Edit the "remote" statusable obj.
suite.editStatusable(testStatusable,
editedContent,
editedContentWarning,
@ -423,19 +433,9 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusUpdated() {
Language: testStatus.Language,
Sensitive: testStatus.Sensitive,
AttachmentIDs: testStatus.AttachmentIDs,
PollOptions: func() []string {
if testStatus.Poll != nil {
return testStatus.Poll.Options
}
return nil
}(),
PollVotes: func() []int {
if testStatus.Poll != nil {
return testStatus.Poll.Votes
}
return nil
}(),
CreatedAt: testStatus.CreatedAt,
PollOptions: getPollOptions(testStatus),
PollVotes: getPollVotes(testStatus),
CreatedAt: testStatus.CreatedAt,
},
)
}
@ -472,7 +472,17 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusReceivedUpdate() {
editedAt time.Time
)
// Edit the "remote" status content.
// Perform any edits to the remote status.
editedContent = "updated status content!"
editedContentWarning = "CW: edited status content"
editedLanguage = testStatus.Language // no change
editedSensitive = *testStatus.Sensitive // no change
editedAttachmentIDs = testStatus.AttachmentIDs // no change
editedPollOptions = getPollOptions(testStatus) // no change
editedPollVotes = getPollVotes(testStatus) // no change
editedAt = time.Now()
// Edit the "remote" statusable obj.
suite.editStatusable(testStatusable,
editedContent,
editedContentWarning,
@ -523,23 +533,15 @@ func (suite *StatusTestSuite) TestDereferencerRefreshStatusReceivedUpdate() {
Language: testStatus.Language,
Sensitive: testStatus.Sensitive,
AttachmentIDs: testStatus.AttachmentIDs,
PollOptions: func() []string {
if testStatus.Poll != nil {
return testStatus.Poll.Options
}
return nil
}(),
PollVotes: func() []int {
if testStatus.Poll != nil {
return testStatus.Poll.Votes
}
return nil
}(),
CreatedAt: testStatus.CreatedAt,
PollOptions: getPollOptions(testStatus),
PollVotes: getPollVotes(testStatus),
CreatedAt: testStatus.CreatedAt,
},
)
}
// editStatusable updates the given statusable attributes.
// note that this acts on the original object, no copying.
func (suite *StatusTestSuite) editStatusable(
statusable ap.Statusable,
content string,
@ -554,6 +556,10 @@ func (suite *StatusTestSuite) editStatusable(
suite.Fail("TODO")
}
// verifyEditedStatusUpdate verifies that a given status has
// the expected number of historic edits, the 'current' status
// attributes (encapsulated as an edit for minimized no. args),
// and the last given 'historic' status edit attributes.
func (suite *StatusTestSuite) verifyEditedStatusUpdate(
status *gtsmodel.Status, // the status to check
previousEdits int, // number of previous edits
@ -574,14 +580,8 @@ func (suite *StatusTestSuite) verifyEditedStatusUpdate(
suite.Equal(current.Language, status.Language)
suite.Equal(*current.Sensitive, *status.Sensitive)
suite.Equal(current.AttachmentIDs, status.AttachmentIDs)
var pollOptions []string
var pollVotes []int
if status.Poll != nil {
pollOptions = status.Poll.Options
pollVotes = status.Poll.Votes
}
suite.Equal(current.PollOptions, pollOptions)
suite.Equal(current.PollVotes, pollVotes)
suite.Equal(current.PollOptions, getPollOptions(status))
suite.Equal(current.PollVotes, getPollVotes(status))
suite.Equal(current.CreatedAt, status.CreatedAt)
// Check the latest historic edit matches expected.
@ -602,3 +602,19 @@ func (suite *StatusTestSuite) verifyEditedStatusUpdate(
func TestStatusTestSuite(t *testing.T) {
suite.Run(t, new(StatusTestSuite))
}
// getPollOptions extracts poll option strings from status (if poll is set).
func getPollOptions(status *gtsmodel.Status) []string {
if status.Poll != nil {
return status.Poll.Options
}
return nil
}
// getPollVotes extracts poll vote counts from status (if poll is set).
func getPollVotes(status *gtsmodel.Status) []int {
if status.Poll != nil {
return status.Poll.Votes
}
return nil
}

View file

@ -26,7 +26,6 @@ import (
type MediaAttachment struct {
ID string `bun:"type:CHAR(26),pk,nullzero,notnull,unique"` // id of this item in the database
CreatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item created
UpdatedAt time.Time `bun:"type:timestamptz,nullzero,notnull,default:current_timestamp"` // when was item last updated
StatusID string `bun:"type:CHAR(26),nullzero"` // ID of the status to which this is attached
URL string `bun:",nullzero"` // Where can the attachment be retrieved on *this* server
RemoteURL string `bun:",nullzero"` // Where can the attachment be retrieved on a remote server (empty for local media)

View file

@ -118,7 +118,6 @@ func (m *Manager) CreateMedia(
Header: util.Ptr(false),
Cached: util.Ptr(false),
CreatedAt: now,
UpdatedAt: now,
}
// Check if we were provided additional info

View file

@ -177,9 +177,7 @@ func (p *Processor) getAttachmentContent(
}
// Start preparing API content model.
apiContent := &apimodel.Content{
ContentUpdated: attach.UpdatedAt,
}
apiContent := &apimodel.Content{}
// Retrieve appropriate
// size file from storage.

View file

@ -20,7 +20,6 @@ package media_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/suite"
)
@ -42,8 +41,6 @@ func (suite *UnattachTestSuite) TestUnattachMedia() {
dbAttachment, errWithCode := suite.db.GetAttachmentByID(ctx, a.ID)
suite.NoError(errWithCode)
suite.WithinDuration(dbAttachment.UpdatedAt, time.Now(), 1*time.Minute)
suite.Empty(dbAttachment.StatusID)
}

View file

@ -718,7 +718,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH17FWEB39HZJ76B6VXSKF/attachment/original/01F8MH6NEM8D7527KZAECTCR76.jpg",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-04T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-04T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -761,7 +760,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01F8MH7TDVANYKWVE8VVKFPJTJ.gif",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -808,7 +806,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01CDR64G398ADCHXK08WWTHEZ5.mp4",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeVideo,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -858,7 +855,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01F8MH8RMYQ6MSNY3JM2XT1CQ5.jpg",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -905,7 +901,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/avatar/original/01F8MH58A357CV5K7R7TJMSH6S.jpg",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -952,7 +947,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/header/original/01PFPMWK2FF0D9WMHEJHR07C3Q.jpg",
RemoteURL: "",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -999,7 +993,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH1H7YV1Z7D2C8K2730QBF/attachment/original/01J2M20K6K9XQC4WSB961YJHV6.mp3",
RemoteURL: "",
CreatedAt: TimeMustParse("2024-01-10T11:24:00+02:00"),
UpdatedAt: TimeMustParse("2024-01-10T11:24:00+02:00"),
Type: gtsmodel.FileTypeAudio,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -1049,7 +1042,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3",
RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3",
CreatedAt: TimeMustParse("2024-11-01T10:01:00+02:00"),
UpdatedAt: TimeMustParse("2024-11-01T10:01:00+02:00"),
Type: gtsmodel.FileTypeUnknown,
FileMeta: gtsmodel.FileMeta{},
AccountID: "01F8MH5NBDF2MV7CTC4Q5128HF",
@ -1068,7 +1060,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01F8MH5ZK5VRH73AKHQM6Y9VNX/attachment/original/01FVW7RXPQ8YJHTEXYPE7Q8ZY0.jpg",
RemoteURL: "http://fossbros-anonymous.io/attachments/original/13bbc3f8-2b5e-46ea-9531-40b4974d9912.jpg",
CreatedAt: TimeMustParse("2021-09-20T12:40:37+02:00"),
UpdatedAt: TimeMustParse("2021-09-20T12:40:37+02:00"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -1114,7 +1105,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/062G5WYKY35KKD12EMSM3F8PJ8/header/original/01PFPMWK2FF0D9WMHEJHR07C3R.jpg",
RemoteURL: "http://fossbros-anonymous.io/attachments/small/a499f55b-2d1e-4acd-98d2-1ac2ba6d79b9.jpg",
CreatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
UpdatedAt: TimeMustParse("2022-06-09T13:12:00Z"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -1160,7 +1150,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7Y3C432WRSNS10EZM86SA5.jpg",
RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7Y6G0EMCKST3Q0914WW0MS.jpg",
CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
Type: gtsmodel.FileTypeImage,
FileMeta: gtsmodel.FileMeta{
Original: gtsmodel.Original{
@ -1205,7 +1194,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE7ZFX9GKA5ZZVD4FACABSS9.svg",
RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE7ZGJYTSYMXF927GF9353KR.svg",
CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
Type: gtsmodel.FileTypeUnknown,
FileMeta: gtsmodel.FileMeta{},
AccountID: "01FHMQX3GAABWSM0S2VZEC2SWC",
@ -1224,7 +1212,6 @@ func NewTestAttachments() map[string]*gtsmodel.MediaAttachment {
URL: "http://localhost:8080/fileserver/01FHMQX3GAABWSM0S2VZEC2SWC/attachment/original/01HE88YG74PVAB81PX2XA9F3FG.mp3",
RemoteURL: "http://example.org/fileserver/01HE7Y659ZWZ02JM4AWYJZ176Q/attachment/original/01HE892Y8ZS68TQCNPX7J888P3.mp3",
CreatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
UpdatedAt: TimeMustParse("2023-11-02T12:44:25+02:00"),
Type: gtsmodel.FileTypeUnknown,
FileMeta: gtsmodel.FileMeta{},
AccountID: "01FHMQX3GAABWSM0S2VZEC2SWC",