-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add repo_id for attachment #16958
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add repo_id for attachment #16958
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
bed7e0b
Add repo_id for attachment
lunny 06601ce
deleting attachments only have repo_id
lunny 14d64e1
Merge branch 'master' into lunny/add_attachment_repoid
6543 9ad3cb0
refactor & fix DeleteWiki
6543 a6ca8de
Fix tests
6543 5d687f3
move upload.Verify into attachment_service
6543 c139a49
Fix test
lunny 7d9cfda
test: make db prepared
6543 083b4a4
Add test for migration v193
lunny a4060d4
Merge branch 'lunny/add_attachment_repoid' of github.com:lunny/gitea …
lunny b02329f
Apply suggestions from code review
6543 2ae23cd
update fixtures
6543 e3f06ad
fix TestGetAttachment/NotLinked
6543 d4db913
fix v193_test.go
6543 b96cde6
Merge branch 'main' into lunny/add_attachment_repoid
6543 f7e9dce
Merge branch 'main' into lunny/add_attachment_repoid
lunny 59c0804
Merge branch 'main' into lunny/add_attachment_repoid
6543 47cb9c7
Merge branch 'main' into lunny/add_attachment_repoid
zeripath 1ef8d4d
Merge branch 'main' into lunny/add_attachment_repoid
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"xorm.io/xorm" | ||
) | ||
|
||
func addRepoIDForAttachment(x *xorm.Engine) error { | ||
type Attachment struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UUID string `xorm:"uuid UNIQUE"` | ||
RepoID int64 `xorm:"INDEX"` // this should not be zero | ||
IssueID int64 `xorm:"INDEX"` // maybe zero when creating | ||
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating | ||
UploaderID int64 `xorm:"INDEX DEFAULT 0"` | ||
} | ||
if err := x.Sync2(new(Attachment)); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := x.Exec("UPDATE `attachment` set repo_id = (SELECT repo_id FROM `issue` WHERE `issue`.id = `attachment`.issue_id) WHERE `attachment`.issue_id > 0"); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := x.Exec("UPDATE `attachment` set repo_id = (SELECT repo_id FROM `release` WHERE `release`.id = `attachment`.release_id) WHERE `attachment`.release_id > 0"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package migrations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_addRepoIDForAttachment(t *testing.T) { | ||
type Attachment struct { | ||
ID int64 `xorm:"pk autoincr"` | ||
UUID string `xorm:"uuid UNIQUE"` | ||
RepoID int64 `xorm:"INDEX"` // this should not be zero | ||
IssueID int64 `xorm:"INDEX"` // maybe zero when creating | ||
ReleaseID int64 `xorm:"INDEX"` // maybe zero when creating | ||
UploaderID int64 `xorm:"INDEX DEFAULT 0"` | ||
} | ||
|
||
// Prepare and load the testing database | ||
x, deferable := prepareTestEnv(t, 0, new(Attachment)) | ||
defer deferable() | ||
if x == nil || t.Failed() { | ||
return | ||
} | ||
|
||
// Run the migration | ||
if err := addRepoIDForAttachment(x); err != nil { | ||
assert.NoError(t, err) | ||
return | ||
} | ||
|
||
type Issue struct { | ||
ID int64 | ||
RepoID int64 | ||
} | ||
|
||
var issueAttachments []*Attachment | ||
err := x.Where("issue_id > 0").Find(&issueAttachments) | ||
assert.NoError(t, err) | ||
for _, attach := range issueAttachments { | ||
assert.Greater(t, attach.RepoID, 0) | ||
assert.Greater(t, attach.IssueID, 0) | ||
var issue Issue | ||
has, err := x.ID(attach.IssueID).Get(&issue) | ||
assert.NoError(t, err) | ||
assert.True(t, has) | ||
assert.EqualValues(t, attach.RepoID, issue.RepoID) | ||
} | ||
|
||
type Release struct { | ||
ID int64 | ||
RepoID int64 | ||
} | ||
|
||
var releaseAttachments []*Attachment | ||
err = x.Where("release_id > 0").Find(&releaseAttachments) | ||
assert.NoError(t, err) | ||
for _, attach := range releaseAttachments { | ||
assert.Greater(t, attach.RepoID, 0) | ||
assert.Greater(t, attach.IssueID, 0) | ||
var release Release | ||
has, err := x.ID(attach.ReleaseID).Get(&release) | ||
assert.NoError(t, err) | ||
assert.True(t, has) | ||
assert.EqualValues(t, attach.RepoID, release.RepoID) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.