code.gitea.io/gitea@v1.22.3/models/migrations/v1_22/v286.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  package v1_22 //nolint
     4  
     5  import (
     6  	"errors"
     7  	"fmt"
     8  
     9  	"code.gitea.io/gitea/modules/log"
    10  	"code.gitea.io/gitea/modules/setting"
    11  
    12  	"xorm.io/xorm"
    13  )
    14  
    15  func expandHashReferencesToSha256(x *xorm.Engine) error {
    16  	alteredTables := [][2]string{
    17  		{"commit_status", "context_hash"},
    18  		{"comment", "commit_sha"},
    19  		{"pull_request", "merge_base"},
    20  		{"pull_request", "merged_commit_id"},
    21  		{"review", "commit_id"},
    22  		{"review_state", "commit_sha"},
    23  		{"repo_archiver", "commit_id"},
    24  		{"release", "sha1"},
    25  		{"repo_indexer_status", "commit_sha"},
    26  	}
    27  
    28  	db := x.NewSession()
    29  	defer db.Close()
    30  
    31  	if err := db.Begin(); err != nil {
    32  		return err
    33  	}
    34  
    35  	if !setting.Database.Type.IsSQLite3() {
    36  		if setting.Database.Type.IsMSSQL() {
    37  			// drop indexes that need to be re-created afterwards
    38  			droppedIndexes := []string{
    39  				"DROP INDEX [IDX_commit_status_context_hash] ON [commit_status]",
    40  				"DROP INDEX [UQE_review_state_pull_commit_user] ON [review_state]",
    41  				"DROP INDEX [UQE_repo_archiver_s] ON [repo_archiver]",
    42  			}
    43  			for _, s := range droppedIndexes {
    44  				_, err := db.Exec(s)
    45  				if err != nil {
    46  					return errors.New(s + " " + err.Error())
    47  				}
    48  			}
    49  		}
    50  
    51  		for _, alts := range alteredTables {
    52  			var err error
    53  			if setting.Database.Type.IsMySQL() {
    54  				_, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` MODIFY COLUMN `%s` VARCHAR(64)", alts[0], alts[1]))
    55  			} else if setting.Database.Type.IsMSSQL() {
    56  				_, err = db.Exec(fmt.Sprintf("ALTER TABLE [%s] ALTER COLUMN [%s] NVARCHAR(64)", alts[0], alts[1]))
    57  			} else {
    58  				_, err = db.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` TYPE VARCHAR(64)", alts[0], alts[1]))
    59  			}
    60  			if err != nil {
    61  				return fmt.Errorf("alter column '%s' of table '%s' failed: %w", alts[1], alts[0], err)
    62  			}
    63  		}
    64  
    65  		if setting.Database.Type.IsMSSQL() {
    66  			recreateIndexes := []string{
    67  				"CREATE INDEX IDX_commit_status_context_hash ON commit_status(context_hash)",
    68  				"CREATE UNIQUE INDEX UQE_review_state_pull_commit_user ON review_state(user_id, pull_id, commit_sha)",
    69  				"CREATE UNIQUE INDEX UQE_repo_archiver_s ON repo_archiver(repo_id, type, commit_id)",
    70  			}
    71  			for _, s := range recreateIndexes {
    72  				_, err := db.Exec(s)
    73  				if err != nil {
    74  					return errors.New(s + " " + err.Error())
    75  				}
    76  			}
    77  		}
    78  	}
    79  	log.Debug("Updated database tables to hold SHA256 git hash references")
    80  
    81  	return db.Commit()
    82  }
    83  
    84  func addObjectFormatNameToRepository(x *xorm.Engine) error {
    85  	type Repository struct {
    86  		ObjectFormatName string `xorm:"VARCHAR(6) NOT NULL DEFAULT 'sha1'"`
    87  	}
    88  
    89  	if err := x.Sync(new(Repository)); err != nil {
    90  		return err
    91  	}
    92  
    93  	// Here to catch weird edge-cases where column constraints above are
    94  	// not applied by the DB backend
    95  	_, err := x.Exec("UPDATE `repository` set `object_format_name` = 'sha1' WHERE `object_format_name` = '' or `object_format_name` IS NULL")
    96  	return err
    97  }
    98  
    99  func AdjustDBForSha256(x *xorm.Engine) error {
   100  	if err := expandHashReferencesToSha256(x); err != nil {
   101  		return err
   102  	}
   103  	return addObjectFormatNameToRepository(x)
   104  }