Skip to content

Commit

Permalink
Increase size of the language column in language_stat
Browse files Browse the repository at this point in the history
In #12379 it was discovered that enry v2 has a maximum language length
of 34 characters which is larger than the 30 previously provided.

This PR updates the language column to 50.

Fix #12379

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Aug 2, 2020
1 parent 1530069 commit 437ab15
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ var migrations = []Migration{
NewMigration("recalculate Stars number for all user", recalculateStars),
// v144 -> v145
NewMigration("update Matrix Webhook http method to 'PUT'", updateMatrixWebhookHTTPMethod),
// v145 -> v146
NewMigration("Increase Language field to 50 in LanguageStats", increaseLanguageField),
}

// GetCurrentDBVersion returns the current db version
Expand Down Expand Up @@ -464,7 +466,6 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
sess.Rollback()
return fmt.Errorf("Drop table `%s` columns %v: %v", tableName, columnNames, err)
}

return sess.Commit()
default:
log.Fatal("Unrecognized DB")
Expand Down
58 changes: 58 additions & 0 deletions models/migrations/v145.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2020 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 migrations

import (
"fmt"

"code.gitea.io/gitea/modules/setting"
"xorm.io/xorm"
)

func increaseLanguageField(x *xorm.Engine) error {
type LanguageStat struct {
Language string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"`
}

if err := x.Sync2(new(LanguageStat)); err != nil {
return err
}

if setting.Database.UseSQLite3 {
// SQLite maps VARCHAR to TEXT without size so we're done
return nil
}

// need to get the correct type for the new column
inferredTable, err := x.TableInfo(new(LanguageStat))
if err != nil {
return err
}
column := inferredTable.GetColumn("language")
sqlType := x.Dialect().SQLType(column)

sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
return err
}

switch {
case setting.Database.UseMySQL:
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE language_stat MODIFY COLUMN language %s", sqlType)); err != nil {
return err
}
case setting.Database.UseMSSQL:
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE language_stat ALTER COLUMN language %s", sqlType)); err != nil {
return err
}
case setting.Database.UsePostgreSQL:
if _, err := sess.Exec(fmt.Sprintf("ALTER TABLE language_stat ALTER COLUMN language %s", sqlType)); err != nil {
return err
}
}

return sess.Commit()
}
2 changes: 1 addition & 1 deletion models/repo_language_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type LanguageStat struct {
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
CommitID string
IsPrimary bool
Language string `xorm:"VARCHAR(30) UNIQUE(s) INDEX NOT NULL"`
Language string `xorm:"VARCHAR(50) UNIQUE(s) INDEX NOT NULL"`
Percentage float32 `xorm:"-"`
Size int64 `xorm:"NOT NULL DEFAULT 0"`
Color string `xorm:"-"`
Expand Down

0 comments on commit 437ab15

Please sign in to comment.