mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 00:20:25 +08:00 
			
		
		
		
	Split migrations folder (#21549)
There are too many files in `models/migrations` folder so that I split them into sub folders.
This commit is contained in:
		
							
								
								
									
										15
									
								
								models/migrations/v1_18/main_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								models/migrations/v1_18/main_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
// 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/migrations/base"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestMain(m *testing.M) {
 | 
			
		||||
	base.MainTest(m)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										28
									
								
								models/migrations/v1_18/v224.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								models/migrations/v1_18/v224.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,28 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func CreateUserBadgesTable(x *xorm.Engine) error {
 | 
			
		||||
	type Badge struct {
 | 
			
		||||
		ID          int64 `xorm:"pk autoincr"`
 | 
			
		||||
		Description string
 | 
			
		||||
		ImageURL    string
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	type userBadge struct {
 | 
			
		||||
		ID      int64 `xorm:"pk autoincr"`
 | 
			
		||||
		BadgeID int64
 | 
			
		||||
		UserID  int64 `xorm:"INDEX"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := x.Sync2(new(Badge)); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	return x.Sync2(new(userBadge))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								models/migrations/v1_18/v225.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								models/migrations/v1_18/v225.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func AlterPublicGPGKeyContentFieldsToMediumText(x *xorm.Engine) error {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
	if err := sess.Begin(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if setting.Database.UseMySQL {
 | 
			
		||||
		if _, err := sess.Exec("ALTER TABLE `gpg_key` CHANGE `content` `content` MEDIUMTEXT"); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if _, err := sess.Exec("ALTER TABLE `public_key` CHANGE `content` `content` MEDIUMTEXT"); err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return sess.Commit()
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										15
									
								
								models/migrations/v1_18/v226.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								models/migrations/v1_18/v226.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,15 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"xorm.io/builder"
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func FixPackageSemverField(x *xorm.Engine) error {
 | 
			
		||||
	_, err := x.Exec(builder.Update(builder.Eq{"semver_compatible": false}).From("`package`").Where(builder.In("`type`", "conan", "generic")))
 | 
			
		||||
	return err
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										64
									
								
								models/migrations/v1_18/v227.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								models/migrations/v1_18/v227.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,64 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"strconv"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/modules/setting"
 | 
			
		||||
	"code.gitea.io/gitea/modules/timeutil"
 | 
			
		||||
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type SystemSetting struct {
 | 
			
		||||
	ID           int64              `xorm:"pk autoincr"`
 | 
			
		||||
	SettingKey   string             `xorm:"varchar(255) unique"` // ensure key is always lowercase
 | 
			
		||||
	SettingValue string             `xorm:"text"`
 | 
			
		||||
	Version      int                `xorm:"version"` // prevent to override
 | 
			
		||||
	Created      timeutil.TimeStamp `xorm:"created"`
 | 
			
		||||
	Updated      timeutil.TimeStamp `xorm:"updated"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func insertSettingsIfNotExist(x *xorm.Engine, sysSettings []*SystemSetting) error {
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
	if err := sess.Begin(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	for _, setting := range sysSettings {
 | 
			
		||||
		exist, err := sess.Table("system_setting").Where("setting_key=?", setting.SettingKey).Exist()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
		if !exist {
 | 
			
		||||
			if _, err := sess.Insert(setting); err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return sess.Commit()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CreateSystemSettingsTable(x *xorm.Engine) error {
 | 
			
		||||
	if err := x.Sync2(new(SystemSetting)); err != nil {
 | 
			
		||||
		return fmt.Errorf("sync2: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// migrate xx to database
 | 
			
		||||
	sysSettings := []*SystemSetting{
 | 
			
		||||
		{
 | 
			
		||||
			SettingKey:   "picture.disable_gravatar",
 | 
			
		||||
			SettingValue: strconv.FormatBool(setting.DisableGravatar),
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			SettingKey:   "picture.enable_federated_avatar",
 | 
			
		||||
			SettingValue: strconv.FormatBool(setting.EnableFederatedAvatar),
 | 
			
		||||
		},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return insertSettingsIfNotExist(x, sysSettings)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								models/migrations/v1_18/v228.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								models/migrations/v1_18/v228.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"code.gitea.io/gitea/modules/timeutil"
 | 
			
		||||
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func AddTeamInviteTable(x *xorm.Engine) error {
 | 
			
		||||
	type TeamInvite struct {
 | 
			
		||||
		ID          int64              `xorm:"pk autoincr"`
 | 
			
		||||
		Token       string             `xorm:"UNIQUE(token) INDEX NOT NULL DEFAULT ''"`
 | 
			
		||||
		InviterID   int64              `xorm:"NOT NULL DEFAULT 0"`
 | 
			
		||||
		OrgID       int64              `xorm:"INDEX NOT NULL DEFAULT 0"`
 | 
			
		||||
		TeamID      int64              `xorm:"UNIQUE(team_mail) INDEX NOT NULL DEFAULT 0"`
 | 
			
		||||
		Email       string             `xorm:"UNIQUE(team_mail) NOT NULL DEFAULT ''"`
 | 
			
		||||
		CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
 | 
			
		||||
		UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return x.Sync2(new(TeamInvite))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								models/migrations/v1_18/v229.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								models/migrations/v1_18/v229.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 //nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/issues"
 | 
			
		||||
 | 
			
		||||
	"xorm.io/builder"
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func UpdateOpenMilestoneCounts(x *xorm.Engine) error {
 | 
			
		||||
	var openMilestoneIDs []int64
 | 
			
		||||
	err := x.Table("milestone").Select("id").Where(builder.Neq{"is_closed": 1}).Find(&openMilestoneIDs)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("error selecting open milestone IDs: %w", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, id := range openMilestoneIDs {
 | 
			
		||||
		_, err := x.ID(id).
 | 
			
		||||
			SetExpr("num_issues", builder.Select("count(*)").From("issue").Where(
 | 
			
		||||
				builder.Eq{"milestone_id": id},
 | 
			
		||||
			)).
 | 
			
		||||
			SetExpr("num_closed_issues", builder.Select("count(*)").From("issue").Where(
 | 
			
		||||
				builder.Eq{
 | 
			
		||||
					"milestone_id": id,
 | 
			
		||||
					"is_closed":    true,
 | 
			
		||||
				},
 | 
			
		||||
			)).
 | 
			
		||||
			Update(&issues.Milestone{})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("error updating issue counts in milestone %d: %w", id, err)
 | 
			
		||||
		}
 | 
			
		||||
		_, err = x.Exec("UPDATE `milestone` SET completeness=100*num_closed_issues/(CASE WHEN num_issues > 0 THEN num_issues ELSE 1 END) WHERE id=?",
 | 
			
		||||
			id,
 | 
			
		||||
		)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return fmt.Errorf("error setting completeness on milestone %d: %w", id, err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										47
									
								
								models/migrations/v1_18/v229_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								models/migrations/v1_18/v229_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,47 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 //nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/issues"
 | 
			
		||||
	"code.gitea.io/gitea/models/migrations/base"
 | 
			
		||||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Test_UpdateOpenMilestoneCounts(t *testing.T) {
 | 
			
		||||
	type ExpectedMilestone issues.Milestone
 | 
			
		||||
 | 
			
		||||
	// Prepare and load the testing database
 | 
			
		||||
	x, deferable := base.PrepareTestEnv(t, 0, new(issues.Milestone), new(ExpectedMilestone), new(issues.Issue))
 | 
			
		||||
	defer deferable()
 | 
			
		||||
	if x == nil || t.Failed() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := UpdateOpenMilestoneCounts(x); err != nil {
 | 
			
		||||
		assert.NoError(t, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	expected := []ExpectedMilestone{}
 | 
			
		||||
	if err := x.Table("expected_milestone").Asc("id").Find(&expected); !assert.NoError(t, err) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	got := []issues.Milestone{}
 | 
			
		||||
	if err := x.Table("milestone").Asc("id").Find(&got); !assert.NoError(t, err) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for i, e := range expected {
 | 
			
		||||
		got := got[i]
 | 
			
		||||
		assert.Equal(t, e.ID, got.ID)
 | 
			
		||||
		assert.Equal(t, e.NumIssues, got.NumIssues)
 | 
			
		||||
		assert.Equal(t, e.NumClosedIssues, got.NumClosedIssues)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										18
									
								
								models/migrations/v1_18/v230.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								models/migrations/v1_18/v230.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 // nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"xorm.io/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// AddConfidentialColumnToOAuth2ApplicationTable: add ConfidentialClient column, setting existing rows to true
 | 
			
		||||
func AddConfidentialClientColumnToOAuth2ApplicationTable(x *xorm.Engine) error {
 | 
			
		||||
	type OAuth2Application struct {
 | 
			
		||||
		ConfidentialClient bool `xorm:"NOT NULL DEFAULT TRUE"`
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return x.Sync(new(OAuth2Application))
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										48
									
								
								models/migrations/v1_18/v230_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								models/migrations/v1_18/v230_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
			
		||||
// Copyright 2022 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 v1_18 //nolint
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models/migrations/base"
 | 
			
		||||
 | 
			
		||||
	"github.com/stretchr/testify/assert"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Test_AddConfidentialClientColumnToOAuth2ApplicationTable(t *testing.T) {
 | 
			
		||||
	// premigration
 | 
			
		||||
	type OAuth2Application struct {
 | 
			
		||||
		ID int64
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// Prepare and load the testing database
 | 
			
		||||
	x, deferable := base.PrepareTestEnv(t, 0, new(OAuth2Application))
 | 
			
		||||
	defer deferable()
 | 
			
		||||
	if x == nil || t.Failed() {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := AddConfidentialClientColumnToOAuth2ApplicationTable(x); err != nil {
 | 
			
		||||
		assert.NoError(t, err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// postmigration
 | 
			
		||||
	type ExpectedOAuth2Application struct {
 | 
			
		||||
		ID                 int64
 | 
			
		||||
		ConfidentialClient bool
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	got := []ExpectedOAuth2Application{}
 | 
			
		||||
	if err := x.Table("o_auth2_application").Select("id, confidential_client").Find(&got); !assert.NoError(t, err) {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	assert.NotEmpty(t, got)
 | 
			
		||||
	for _, e := range got {
 | 
			
		||||
		assert.True(t, e.ConfidentialClient)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user