Skip to content

Commit 3b2e424

Browse files
committed
Use a standalone table to store deletion files so that all kinds of storage could reuse the deletion infrastructure
1 parent 8f8dd8c commit 3b2e424

34 files changed

+365
-459
lines changed

models/db/file_status.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

models/fixtures/attachment.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
comment_id: 0
99
name: attach1
1010
download_count: 0
11-
status: 1
1211
size: 0
1312
created_unix: 946684800
1413

@@ -22,7 +21,6 @@
2221
comment_id: 0
2322
name: attach2
2423
download_count: 1
25-
status: 1
2624
size: 0
2725
created_unix: 946684800
2826

@@ -36,7 +34,6 @@
3634
comment_id: 1
3735
name: attach1
3836
download_count: 0
39-
status: 1
4037
size: 0
4138
created_unix: 946684800
4239

@@ -50,7 +47,6 @@
5047
comment_id: 1
5148
name: attach2
5249
download_count: 1
53-
status: 1
5450
size: 0
5551
created_unix: 946684800
5652

@@ -64,7 +60,6 @@
6460
comment_id: 0
6561
name: attach1
6662
download_count: 0
67-
status: 1
6863
size: 0
6964
created_unix: 946684800
7065

@@ -78,7 +73,6 @@
7873
comment_id: 2
7974
name: attach1
8075
download_count: 0
81-
status: 1
8276
size: 0
8377
created_unix: 946684800
8478

@@ -92,7 +86,6 @@
9286
comment_id: 2
9387
name: attach1
9488
download_count: 0
95-
status: 1
9689
size: 0
9790
created_unix: 946684800
9891

@@ -106,7 +99,6 @@
10699
comment_id: 0
107100
name: attach1
108101
download_count: 0
109-
status: 1
110102
size: 0
111103
created_unix: 946684800
112104

@@ -120,7 +112,6 @@
120112
comment_id: 0
121113
name: attach1
122114
download_count: 0
123-
status: 1
124115
size: 0
125116
created_unix: 946684800
126117

@@ -134,7 +125,6 @@
134125
comment_id: 0
135126
name: attach1
136127
download_count: 0
137-
status: 1
138128
size: 0
139129
created_unix: 946684800
140130

@@ -148,7 +138,6 @@
148138
comment_id: 0
149139
name: attach1
150140
download_count: 0
151-
status: 1
152141
size: 0
153142
created_unix: 946684800
154143

@@ -162,7 +151,6 @@
162151
comment_id: 0
163152
name: README.md
164153
download_count: 0
165-
status: 1
166154
size: 0
167155
created_unix: 946684800
168156

@@ -176,6 +164,5 @@
176164
comment_id: 7
177165
name: code_comment_uploaded_attachment.png
178166
download_count: 0
179-
status: 1
180167
size: 0
181168
created_unix: 946684812

models/issues/comment.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,8 @@ func UpdateComment(ctx context.Context, c *Comment, contentVersion int, doer *us
11161116

11171117
// DeleteComment deletes the comment
11181118
func DeleteComment(ctx context.Context, comment *Comment) error {
1119-
if _, err := db.GetEngine(ctx).ID(comment.ID).NoAutoCondition().Delete(comment); err != nil {
1119+
e := db.GetEngine(ctx)
1120+
if _, err := e.ID(comment.ID).NoAutoCondition().Delete(comment); err != nil {
11201121
return err
11211122
}
11221123

@@ -1131,7 +1132,7 @@ func DeleteComment(ctx context.Context, comment *Comment) error {
11311132
return err
11321133
}
11331134
}
1134-
if _, err := db.GetEngine(ctx).Table("action").
1135+
if _, err := e.Table("action").
11351136
Where("comment_id = ?", comment.ID).
11361137
Update(map[string]any{
11371138
"is_deleted": true,

models/issues/comment_list.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,7 @@ func (comments CommentList) LoadAttachmentsByIssue(ctx context.Context) error {
349349
}
350350

351351
attachments := make([]*repo_model.Attachment, 0, len(comments)/2)
352-
if err := db.GetEngine(ctx).
353-
Where("issue_id=? AND comment_id>0", comments[0].IssueID).
354-
And("status = ?", db.FileStatusNormal).
355-
Find(&attachments); err != nil {
352+
if err := db.GetEngine(ctx).Where("issue_id=? AND comment_id>0", comments[0].IssueID).Find(&attachments); err != nil {
356353
return err
357354
}
358355

@@ -380,7 +377,6 @@ func (comments CommentList) LoadAttachments(ctx context.Context) (err error) {
380377
limit := min(left, db.DefaultMaxInSize)
381378
rows, err := db.GetEngine(ctx).
382379
In("comment_id", commentsIDs[:limit]).
383-
And("status = ?", db.FileStatusNormal).
384380
Rows(new(repo_model.Attachment))
385381
if err != nil {
386382
return err

models/issues/comment_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ func Test_UpdateCommentAttachment(t *testing.T) {
5050

5151
comment := unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{ID: 1})
5252
attachment := repo_model.Attachment{
53-
Name: "test.txt",
54-
Status: db.FileStatusNormal,
55-
UUID: "test-uuid",
53+
Name: "test.txt",
5654
}
5755
assert.NoError(t, db.Insert(db.DefaultContext, &attachment))
5856

models/issues/issue_list.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,6 @@ func (issues IssueList) LoadAttachments(ctx context.Context) (err error) {
339339
limit := min(left, db.DefaultMaxInSize)
340340
rows, err := db.GetEngine(ctx).
341341
In("issue_id", issuesIDs[:limit]).
342-
And("status = ?", db.FileStatusNormal).
343342
Rows(new(repo_model.Attachment))
344343
if err != nil {
345344
return err

models/migrations/migrations.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ func prepareMigrationTasks() []*migration {
385385
newMigration(320, "Migrate two_factor_policy to login_source table", v1_24.MigrateSkipTwoFactor),
386386

387387
// Gitea 1.24.0-rc0 ends at migration ID number 320 (database version 321)
388-
newMigration(321, "Add file status columns to attachment table", v1_25.AddFileStatusToAttachment),
388+
newMigration(321, "Add storage_path_deletion table", v1_25.AddStoragePathDeletion),
389389
}
390390
return preparedMigrations
391391
}

models/migrations/v1_25/main_test.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

models/migrations/v1_25/v321.go

Lines changed: 12 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,23 @@
44
package v1_25
55

66
import (
7-
"code.gitea.io/gitea/models/db"
87
"code.gitea.io/gitea/modules/timeutil"
98

109
"xorm.io/xorm"
11-
"xorm.io/xorm/schemas"
1210
)
1311

14-
type Attachment321 struct {
15-
ID int64 `xorm:"pk autoincr"`
16-
UUID string `xorm:"uuid"`
17-
RepoID int64 // this should not be zero
18-
IssueID int64 // maybe zero when creating
19-
ReleaseID int64 // maybe zero when creating
20-
UploaderID int64 `xorm:"DEFAULT 0"` // Notice: will be zero before this column added
21-
CommentID int64
22-
Name string
23-
DownloadCount int64 `xorm:"DEFAULT 0"`
24-
Status db.FileStatus `xorm:"DEFAULT 1 NOT NULL"` // 1 = normal, 2 = to be deleted
25-
DeleteFailedCount int `xorm:"DEFAULT 0 NOT NULL"` // Number of times the deletion failed, used to prevent infinite loop
26-
LastDeleteFailedTime timeutil.TimeStamp // Last time the deletion failed, used to prevent infinite loop
27-
Size int64 `xorm:"DEFAULT 0"`
28-
CreatedUnix timeutil.TimeStamp `xorm:"created"`
29-
}
30-
31-
func (a *Attachment321) TableName() string {
32-
return "attachment"
33-
}
34-
35-
// TableIndices implements xorm's TableIndices interface
36-
func (a *Attachment321) TableIndices() []*schemas.Index {
37-
uuidIndex := schemas.NewIndex("uuid", schemas.UniqueType)
38-
uuidIndex.AddColumn("uuid")
39-
40-
repoIndex := schemas.NewIndex("repo_id", schemas.IndexType)
41-
repoIndex.AddColumn("repo_id")
42-
43-
issueIndex := schemas.NewIndex("issue_id", schemas.IndexType)
44-
issueIndex.AddColumn("issue_id")
45-
46-
releaseIndex := schemas.NewIndex("release_id", schemas.IndexType)
47-
releaseIndex.AddColumn("release_id")
48-
49-
uploaderIndex := schemas.NewIndex("uploader_id", schemas.IndexType)
50-
uploaderIndex.AddColumn("uploader_id")
51-
52-
commentIndex := schemas.NewIndex("comment_id", schemas.IndexType)
53-
commentIndex.AddColumn("comment_id")
54-
55-
statusIndex := schemas.NewIndex("status", schemas.IndexType)
56-
statusIndex.AddColumn("status")
57-
58-
statusIDIndex := schemas.NewIndex("status_id", schemas.IndexType)
59-
statusIDIndex.AddColumn("status", "id") // For status = ? AND id > ? query
60-
61-
return []*schemas.Index{
62-
uuidIndex,
63-
repoIndex,
64-
issueIndex,
65-
releaseIndex,
66-
uploaderIndex,
67-
commentIndex,
68-
statusIndex,
69-
statusIDIndex,
12+
func AddStoragePathDeletion(x *xorm.Engine) error {
13+
// StoragePathDeletion represents a file or directory that is pending deletion.
14+
type StoragePathDeletion struct {
15+
ID int64
16+
StorageName string // storage name defines in storage module
17+
PathType int // 1 for file, 2 for directory
18+
RelativePath string `xorm:"TEXT"`
19+
DeleteFailedCount int `xorm:"DEFAULT 0 NOT NULL"` // Number of times the deletion failed, used to prevent infinite loop
20+
LastDeleteFailedReason string `xorm:"TEXT"` // Last reason the deletion failed, used to prevent infinite loop
21+
LastDeleteFailedTime timeutil.TimeStamp // Last time the deletion failed, used to prevent infinite loop
22+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
7023
}
71-
}
7224

73-
func AddFileStatusToAttachment(x *xorm.Engine) error {
74-
return x.Sync(new(Attachment321))
25+
return x.Sync(new(StoragePathDeletion))
7526
}

models/migrations/v1_25/v321_test.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)