summaryrefslogtreecommitdiffstats
path: root/models/migrations/v1_9
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--models/migrations/v1_9/v82.go133
-rw-r--r--models/migrations/v1_9/v83.go27
-rw-r--r--models/migrations/v1_9/v84.go17
-rw-r--r--models/migrations/v1_9/v85.go118
-rw-r--r--models/migrations/v1_9/v86.go16
-rw-r--r--models/migrations/v1_9/v87.go17
6 files changed, 328 insertions, 0 deletions
diff --git a/models/migrations/v1_9/v82.go b/models/migrations/v1_9/v82.go
new file mode 100644
index 00000000..26806dd6
--- /dev/null
+++ b/models/migrations/v1_9/v82.go
@@ -0,0 +1,133 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "fmt"
+ "path/filepath"
+ "strings"
+
+ "code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/setting"
+
+ "xorm.io/xorm"
+)
+
+func FixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
+ type Release struct {
+ ID int64
+ RepoID int64
+ Sha1 string
+ TagName string
+ }
+
+ type Repository struct {
+ ID int64
+ OwnerID int64
+ Name string
+ }
+
+ type User struct {
+ ID int64
+ Name string
+ }
+
+ // UserPath returns the path absolute path of user repositories.
+ UserPath := func(userName string) string {
+ return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
+ }
+
+ // RepoPath returns repository path by given user and repository name.
+ RepoPath := func(userName, repoName string) string {
+ return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
+ }
+
+ // Update release sha1
+ const batchSize = 100
+ sess := x.NewSession()
+ defer sess.Close()
+
+ var (
+ err error
+ count int
+ gitRepoCache = make(map[int64]*git.Repository)
+ repoCache = make(map[int64]*Repository)
+ userCache = make(map[int64]*User)
+ )
+
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+
+ for start := 0; ; start += batchSize {
+ releases := make([]*Release, 0, batchSize)
+ if err = sess.Limit(batchSize, start).Asc("id").Where("is_tag=?", false).Find(&releases); err != nil {
+ return err
+ }
+ if len(releases) == 0 {
+ break
+ }
+
+ for _, release := range releases {
+ gitRepo, ok := gitRepoCache[release.RepoID]
+ if !ok {
+ repo, ok := repoCache[release.RepoID]
+ if !ok {
+ repo = new(Repository)
+ has, err := sess.ID(release.RepoID).Get(repo)
+ if err != nil {
+ return err
+ } else if !has {
+ return fmt.Errorf("Repository %d is not exist", release.RepoID)
+ }
+
+ repoCache[release.RepoID] = repo
+ }
+
+ user, ok := userCache[repo.OwnerID]
+ if !ok {
+ user = new(User)
+ has, err := sess.ID(repo.OwnerID).Get(user)
+ if err != nil {
+ return err
+ } else if !has {
+ return fmt.Errorf("User %d is not exist", repo.OwnerID)
+ }
+
+ userCache[repo.OwnerID] = user
+ }
+
+ gitRepo, err = git.OpenRepository(git.DefaultContext, RepoPath(user.Name, repo.Name))
+ if err != nil {
+ return err
+ }
+ defer gitRepo.Close()
+ gitRepoCache[release.RepoID] = gitRepo
+ }
+
+ release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
+ if err != nil && !git.IsErrNotExist(err) {
+ return err
+ }
+
+ if err == nil {
+ if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
+ return err
+ }
+ }
+
+ count++
+ if count >= 1000 {
+ if err = sess.Commit(); err != nil {
+ return err
+ }
+ if err = sess.Begin(); err != nil {
+ return err
+ }
+ count = 0
+ }
+ }
+ }
+ return sess.Commit()
+}
diff --git a/models/migrations/v1_9/v83.go b/models/migrations/v1_9/v83.go
new file mode 100644
index 00000000..10e6c458
--- /dev/null
+++ b/models/migrations/v1_9/v83.go
@@ -0,0 +1,27 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "code.gitea.io/gitea/modules/timeutil"
+
+ "xorm.io/xorm"
+)
+
+func AddUploaderIDForAttachment(x *xorm.Engine) error {
+ type Attachment struct {
+ ID int64 `xorm:"pk autoincr"`
+ UUID string `xorm:"uuid UNIQUE"`
+ IssueID int64 `xorm:"INDEX"`
+ ReleaseID int64 `xorm:"INDEX"`
+ UploaderID int64 `xorm:"INDEX DEFAULT 0"`
+ CommentID int64
+ Name string
+ DownloadCount int64 `xorm:"DEFAULT 0"`
+ Size int64 `xorm:"DEFAULT 0"`
+ CreatedUnix timeutil.TimeStamp `xorm:"created"`
+ }
+
+ return x.Sync(new(Attachment))
+}
diff --git a/models/migrations/v1_9/v84.go b/models/migrations/v1_9/v84.go
new file mode 100644
index 00000000..c7155fe9
--- /dev/null
+++ b/models/migrations/v1_9/v84.go
@@ -0,0 +1,17 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "xorm.io/xorm"
+)
+
+func AddGPGKeyImport(x *xorm.Engine) error {
+ type GPGKeyImport struct {
+ KeyID string `xorm:"pk CHAR(16) NOT NULL"`
+ Content string `xorm:"TEXT NOT NULL"`
+ }
+
+ return x.Sync(new(GPGKeyImport))
+}
diff --git a/models/migrations/v1_9/v85.go b/models/migrations/v1_9/v85.go
new file mode 100644
index 00000000..a23d7c5d
--- /dev/null
+++ b/models/migrations/v1_9/v85.go
@@ -0,0 +1,118 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "fmt"
+
+ "code.gitea.io/gitea/models/migrations/base"
+ "code.gitea.io/gitea/modules/log"
+ "code.gitea.io/gitea/modules/timeutil"
+ "code.gitea.io/gitea/modules/util"
+
+ "xorm.io/xorm"
+)
+
+func HashAppToken(x *xorm.Engine) error {
+ // AccessToken see models/token.go
+ type AccessToken struct {
+ ID int64 `xorm:"pk autoincr"`
+ UID int64 `xorm:"INDEX"`
+ Name string
+ Sha1 string
+ Token string `xorm:"-"`
+ TokenHash string // sha256 of token - we will ensure UNIQUE later
+ TokenSalt string
+ TokenLastEight string `xorm:"token_last_eight"`
+
+ CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
+ UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
+ HasRecentActivity bool `xorm:"-"`
+ HasUsed bool `xorm:"-"`
+ }
+
+ // First remove the index
+ sess := x.NewSession()
+ defer sess.Close()
+
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+
+ if err := sess.Sync(new(AccessToken)); err != nil {
+ return fmt.Errorf("Sync: %w", err)
+ }
+
+ if err := sess.Commit(); err != nil {
+ return err
+ }
+
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+
+ // transform all tokens to hashes
+ const batchSize = 100
+ for start := 0; ; start += batchSize {
+ tokens := make([]*AccessToken, 0, batchSize)
+ if err := sess.Limit(batchSize, start).Find(&tokens); err != nil {
+ return err
+ }
+ if len(tokens) == 0 {
+ break
+ }
+
+ for _, token := range tokens {
+ // generate salt
+ salt, err := util.CryptoRandomString(10)
+ if err != nil {
+ return err
+ }
+ token.TokenSalt = salt
+ token.TokenHash = base.HashToken(token.Sha1, salt)
+ if len(token.Sha1) < 8 {
+ log.Warn("Unable to transform token %s with name %s belonging to user ID %d, skipping transformation", token.Sha1, token.Name, token.UID)
+ continue
+ }
+ token.TokenLastEight = token.Sha1[len(token.Sha1)-8:]
+ token.Sha1 = "" // ensure to blank out column in case drop column doesn't work
+
+ if _, err := sess.ID(token.ID).Cols("token_hash, token_salt, token_last_eight, sha1").Update(token); err != nil {
+ return fmt.Errorf("couldn't add in sha1, token_hash, token_salt and token_last_eight: %w", err)
+ }
+ }
+ }
+
+ // Commit and begin new transaction for dropping columns
+ if err := sess.Commit(); err != nil {
+ return err
+ }
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+
+ if err := base.DropTableColumns(sess, "access_token", "sha1"); err != nil {
+ return err
+ }
+ if err := sess.Commit(); err != nil {
+ return err
+ }
+ return resyncHashAppTokenWithUniqueHash(x)
+}
+
+func resyncHashAppTokenWithUniqueHash(x *xorm.Engine) error {
+ // AccessToken see models/token.go
+ type AccessToken struct {
+ TokenHash string `xorm:"UNIQUE"` // sha256 of token - we will ensure UNIQUE later
+ }
+ sess := x.NewSession()
+ defer sess.Close()
+ if err := sess.Begin(); err != nil {
+ return err
+ }
+ if err := sess.Sync(new(AccessToken)); err != nil {
+ return fmt.Errorf("Sync: %w", err)
+ }
+ return sess.Commit()
+}
diff --git a/models/migrations/v1_9/v86.go b/models/migrations/v1_9/v86.go
new file mode 100644
index 00000000..cf2725d1
--- /dev/null
+++ b/models/migrations/v1_9/v86.go
@@ -0,0 +1,16 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "xorm.io/xorm"
+)
+
+func AddHTTPMethodToWebhook(x *xorm.Engine) error {
+ type Webhook struct {
+ HTTPMethod string `xorm:"http_method DEFAULT 'POST'"`
+ }
+
+ return x.Sync(new(Webhook))
+}
diff --git a/models/migrations/v1_9/v87.go b/models/migrations/v1_9/v87.go
new file mode 100644
index 00000000..fa01b6e5
--- /dev/null
+++ b/models/migrations/v1_9/v87.go
@@ -0,0 +1,17 @@
+// Copyright 2019 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package v1_9 //nolint
+
+import (
+ "xorm.io/xorm"
+)
+
+func AddAvatarFieldToRepository(x *xorm.Engine) error {
+ type Repository struct {
+ // ID(10-20)-md5(32) - must fit into 64 symbols
+ Avatar string `xorm:"VARCHAR(64)"`
+ }
+
+ return x.Sync(new(Repository))
+}