mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Reorder migrations, skip errors if running migration again (#3160)
* Reorder migrations, skip errors if running migration again * Rename migration file names to match migration version * Add note about ingored error
This commit is contained in:
		@@ -7,36 +7,63 @@ package migrations
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"code.gitea.io/gitea/models"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-xorm/xorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func addDefaultValueToUserProhibitLogin(x *xorm.Engine) (err error) {
 | 
			
		||||
	user := &models.User{
 | 
			
		||||
		ProhibitLogin: false,
 | 
			
		||||
func removeDuplicateUnitTypes(x *xorm.Engine) error {
 | 
			
		||||
	// RepoUnit describes all units of a repository
 | 
			
		||||
	type RepoUnit struct {
 | 
			
		||||
		RepoID int64
 | 
			
		||||
		Type   int
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if _, err := x.Where("`prohibit_login` IS NULL").Cols("prohibit_login").Update(user); err != nil {
 | 
			
		||||
	// Enumerate all the unit types
 | 
			
		||||
	const (
 | 
			
		||||
		UnitTypeCode            = iota + 1 // 1 code
 | 
			
		||||
		UnitTypeIssues                     // 2 issues
 | 
			
		||||
		UnitTypePullRequests               // 3 PRs
 | 
			
		||||
		UnitTypeReleases                   // 4 Releases
 | 
			
		||||
		UnitTypeWiki                       // 5 Wiki
 | 
			
		||||
		UnitTypeExternalWiki               // 6 ExternalWiki
 | 
			
		||||
		UnitTypeExternalTracker            // 7 ExternalTracker
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
	var externalIssueRepoUnits []RepoUnit
 | 
			
		||||
	err := x.Where("type = ?", UnitTypeExternalTracker).Find(&externalIssueRepoUnits)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Query repositories: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var externalWikiRepoUnits []RepoUnit
 | 
			
		||||
	err = x.Where("type = ?", UnitTypeExternalWiki).Find(&externalWikiRepoUnits)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Query repositories: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sess := x.NewSession()
 | 
			
		||||
	defer sess.Close()
 | 
			
		||||
 | 
			
		||||
	if err := sess.Begin(); err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	dialect := x.Dialect().DriverName()
 | 
			
		||||
 | 
			
		||||
	switch dialect {
 | 
			
		||||
	case "mysql":
 | 
			
		||||
		_, err = x.Exec("ALTER TABLE user MODIFY `prohibit_login` tinyint(1) NOT NULL DEFAULT 0")
 | 
			
		||||
	case "postgres":
 | 
			
		||||
		_, err = x.Exec("ALTER TABLE \"user\" ALTER COLUMN `prohibit_login` SET NOT NULL, ALTER COLUMN `prohibit_login` SET DEFAULT false")
 | 
			
		||||
	case "mssql":
 | 
			
		||||
		// xorm already set DEFAULT 0 for data type BIT in mssql
 | 
			
		||||
		_, err = x.Exec(`ALTER TABLE [user] ALTER COLUMN "prohibit_login" BIT NOT NULL`)
 | 
			
		||||
	case "sqlite3":
 | 
			
		||||
	for _, repoUnit := range externalIssueRepoUnits {
 | 
			
		||||
		if _, err = sess.Delete(&RepoUnit{
 | 
			
		||||
			RepoID: repoUnit.RepoID,
 | 
			
		||||
			Type:   UnitTypeIssues,
 | 
			
		||||
		}); err != nil {
 | 
			
		||||
			return fmt.Errorf("Delete repo unit: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return fmt.Errorf("Error changing user prohibit_login column definition: %v", err)
 | 
			
		||||
	for _, repoUnit := range externalWikiRepoUnits {
 | 
			
		||||
		if _, err = sess.Delete(&RepoUnit{
 | 
			
		||||
			RepoID: repoUnit.RepoID,
 | 
			
		||||
			Type:   UnitTypeWiki,
 | 
			
		||||
		}); err != nil {
 | 
			
		||||
			return fmt.Errorf("Delete repo unit: %v", err)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return err
 | 
			
		||||
	return sess.Commit()
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user