code.gitea.io/gitea@v1.21.7/models/migrations/v1_14/v165.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package v1_14 //nolint
     5  
     6  import (
     7  	"code.gitea.io/gitea/models/migrations/base"
     8  
     9  	"xorm.io/xorm"
    10  	"xorm.io/xorm/schemas"
    11  )
    12  
    13  func ConvertHookTaskTypeToVarcharAndTrim(x *xorm.Engine) error {
    14  	dbType := x.Dialect().URI().DBType
    15  	if dbType == schemas.SQLITE { // For SQLITE, varchar or char will always be represented as TEXT
    16  		return nil
    17  	}
    18  
    19  	type HookTask struct { //nolint:unused
    20  		Typ string `xorm:"VARCHAR(16) index"`
    21  	}
    22  
    23  	if err := base.ModifyColumn(x, "hook_task", &schemas.Column{
    24  		Name: "typ",
    25  		SQLType: schemas.SQLType{
    26  			Name: "VARCHAR",
    27  		},
    28  		Length:         16,
    29  		Nullable:       true, // To keep compatible as nullable
    30  		DefaultIsEmpty: true,
    31  	}); err != nil {
    32  		return err
    33  	}
    34  
    35  	var hookTaskTrimSQL string
    36  	if dbType == schemas.MSSQL {
    37  		hookTaskTrimSQL = "UPDATE hook_task SET typ = RTRIM(LTRIM(typ))"
    38  	} else {
    39  		hookTaskTrimSQL = "UPDATE hook_task SET typ = TRIM(typ)"
    40  	}
    41  	if _, err := x.Exec(hookTaskTrimSQL); err != nil {
    42  		return err
    43  	}
    44  
    45  	type Webhook struct { //nolint:unused
    46  		Type string `xorm:"VARCHAR(16) index"`
    47  	}
    48  
    49  	if err := base.ModifyColumn(x, "webhook", &schemas.Column{
    50  		Name: "type",
    51  		SQLType: schemas.SQLType{
    52  			Name: "VARCHAR",
    53  		},
    54  		Length:         16,
    55  		Nullable:       true, // To keep compatible as nullable
    56  		DefaultIsEmpty: true,
    57  	}); err != nil {
    58  		return err
    59  	}
    60  
    61  	var webhookTrimSQL string
    62  	if dbType == schemas.MSSQL {
    63  		webhookTrimSQL = "UPDATE webhook SET type = RTRIM(LTRIM(type))"
    64  	} else {
    65  		webhookTrimSQL = "UPDATE webhook SET type = TRIM(type)"
    66  	}
    67  	_, err := x.Exec(webhookTrimSQL)
    68  	return err
    69  }