Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setup mysql tables as utf8mb4 and convert them #3516

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
type ProtectedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s)"`
BranchName string `xorm:"UNIQUE(s)"`
BranchName string `xorm:"VARCHAR(191) UNIQUE(s)"`
CanPush bool `xorm:"NOT NULL DEFAULT false"`
EnableWhitelist bool
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
Expand Down Expand Up @@ -287,8 +287,8 @@ func (repo *Repository) DeleteProtectedBranch(id int64) (err error) {
type DeletedBranch struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"UNIQUE(s) NOT NULL"`
Commit string `xorm:"UNIQUE(s) NOT NULL"`
Name string `xorm:"VARCHAR(191) UNIQUE(s) NOT NULL"`
Commit string `xorm:"VARCHAR(191) UNIQUE(s) NOT NULL"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is a limit being added here ?

DeletedByID int64 `xorm:"INDEX"`
DeletedBy *User `xorm:"-"`
DeletedUnix util.TimeStamp `xorm:"INDEX created"`
Expand Down
2 changes: 1 addition & 1 deletion models/external_login_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "github.com/markbates/goth"

// ExternalLoginUser makes the connecting between some existing user and additional external login sources
type ExternalLoginUser struct {
ExternalID string `xorm:"pk NOT NULL"`
ExternalID string `xorm:"VARCHAR(191) pk NOT NULL"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is a limit being added here ?

UserID int64 `xorm:"INDEX NOT NULL"`
LoginSourceID int64 `xorm:"pk NOT NULL"`
}
Expand Down
2 changes: 1 addition & 1 deletion models/issue_reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Reaction represents a reactions on issues and comments.
type Reaction struct {
ID int64 `xorm:"pk autoincr"`
Type string `xorm:"INDEX UNIQUE(s) NOT NULL"`
Type string `xorm:"VARCHAR(191) INDEX UNIQUE(s) NOT NULL"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is a limit being added here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@strk please take a look at #3516 (comment) where I explained where that limit comes from. In short, it's for compatibility with MariaDB/MySQL versions that run the InnoDB 5.6 engine as we cannot have indexed fields that are > 767 bytes long (191x 4 bytes for utf8mb4).

IssueID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
CommentID int64 `xorm:"INDEX UNIQUE(s)"`
UserID int64 `xorm:"INDEX UNIQUE(s) NOT NULL"`
Expand Down
2 changes: 1 addition & 1 deletion models/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// LFSMetaObject stores metadata for LFS tracked files.
type LFSMetaObject struct {
ID int64 `xorm:"pk autoincr"`
Oid string `xorm:"UNIQUE(s) INDEX NOT NULL"`
Oid string `xorm:"VARCHAR(191) UNIQUE(s) INDEX NOT NULL"`
Size int64 `xorm:"NOT NULL"`
RepositoryID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Existing bool `xorm:"-"`
Expand Down
2 changes: 1 addition & 1 deletion models/login_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (cfg *OAuth2Config) ToDB() ([]byte, error) {
type LoginSource struct {
ID int64 `xorm:"pk autoincr"`
Type LoginType
Name string `xorm:"UNIQUE"`
Name string `xorm:"VARCHAR(191) UNIQUE"`
IsActived bool `xorm:"INDEX NOT NULL DEFAULT false"`
IsSyncEnabled bool `xorm:"INDEX NOT NULL DEFAULT false"`
Cfg core.Conversion `xorm:"TEXT"`
Expand Down
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ var migrations = []Migration{
NewMigration("add language column for user setting", addLanguageSetting),
// v64 -> v65
NewMigration("add multiple assignees", addMultipleAssignees),
// v65 -> v66
NewMigration("change columns to utf8mb4 on mysql databases", mysqlColumnsToUTF8MB4),
}

// Migrate database to current version
Expand Down
67 changes: 67 additions & 0 deletions models/migrations/v65.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2018 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/log"
"code.gitea.io/gitea/modules/setting"

"github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
)

func mysqlColumnsToUTF8MB4(x *xorm.Engine) (err error) {
if !setting.UseMySQL {
log.Info("Nothing to do")
return nil
}

const maxvc = 191
migrationSuccess := true

tables, err := x.DBMetas()
if err != nil {
return fmt.Errorf("cannot get tables: %v", err)
}
for _, table := range tables {
readyForConversion := true
for _, col := range table.Columns() {
if !(len(col.Indexes) > 0 || col.IsPrimaryKey) {
continue
}
if !(col.SQLType.Name == "VARCHAR" && col.Length > maxvc) {
continue
}
log.Info("reducing column %s.%s from %d to %d bytes", table.Name, col.Name, col.Length, maxvc)
sqlstmt := fmt.Sprintf("alter table `%s` change column `%s` `%s` varchar(%d)", table.Name, col.Name, col.Name, maxvc)
if _, err := x.Exec(sqlstmt); err != nil {
if e, ok := err.(*mysql.MySQLError); ok {
if e.Number == 1265 || e.Number == 1406 {
log.Warn("failed. Please cut all data of this column down to a maximum of %d bytes", maxvc)
} else {
log.Warn("failed with %v", err)
}
readyForConversion = false
migrationSuccess = false
continue
}
return fmt.Errorf("something went horribly wrong: %v", err)
}
}
if readyForConversion {
log.Info("%s: converting table to utf8mb4", table.Name)
if _, err := x.Exec("alter table `" + table.Name + "` convert to character set utf8mb4"); err != nil {
log.Warn("conversion of %s failed: %v", table.Name, err)
migrationSuccess = false
}
}
}
if !migrationSuccess {
return fmt.Errorf("conversion of some of the tables failed. Please check the logs and re-run gitea")
}
return nil
}
4 changes: 2 additions & 2 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ func getEngine() (*xorm.Engine, error) {
switch DbCfg.Type {
case "mysql":
if DbCfg.Host[0] == '/' { // looks like a unix socket
connStr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8&parseTime=true",
connStr = fmt.Sprintf("%s:%s@unix(%s)/%s%scharset=utf8mb4&parseTime=true",
DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
} else {
connStr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8&parseTime=true",
connStr = fmt.Sprintf("%s:%s@tcp(%s)/%s%scharset=utf8mb4&parseTime=true",
DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
}
case "postgres":
Expand Down
2 changes: 1 addition & 1 deletion models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Notification struct {
Source NotificationSource `xorm:"SMALLINT INDEX NOT NULL"`

IssueID int64 `xorm:"INDEX NOT NULL"`
CommitID string `xorm:"INDEX"`
CommitID string `xorm:"VARCHAR(191) INDEX"`

UpdatedBy int64 `xorm:"INDEX NOT NULL"`

Expand Down
2 changes: 1 addition & 1 deletion models/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Release struct {
Repo *Repository `xorm:"-"`
PublisherID int64 `xorm:"INDEX"`
Publisher *User `xorm:"-"`
TagName string `xorm:"INDEX UNIQUE(n)"`
TagName string `xorm:"VARCHAR(191) INDEX UNIQUE(n)"`
LowerTagName string
Target string
Title string
Expand Down
4 changes: 2 additions & 2 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ type Repository struct {
OwnerID int64 `xorm:"UNIQUE(s)"`
OwnerName string `xorm:"-"`
Owner *User `xorm:"-"`
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"INDEX NOT NULL"`
LowerName string `xorm:"VARCHAR(191) UNIQUE(s) INDEX NOT NULL"`
Name string `xorm:"VARCHAR(191) INDEX NOT NULL"`
Description string
Website string
DefaultBranch string
Expand Down
2 changes: 1 addition & 1 deletion models/repo_redirect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "strings"
type RepoRedirect struct {
ID int64 `xorm:"pk autoincr"`
OwnerID int64 `xorm:"UNIQUE(s)"`
LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
LowerName string `xorm:"VARCHAR(191) UNIQUE(s) INDEX NOT NULL"`
RedirectRepoID int64 // repoID to redirect to
}

Expand Down
4 changes: 2 additions & 2 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ var (
// User represents the object of individual and member of organization.
type User struct {
ID int64 `xorm:"pk autoincr"`
LowerName string `xorm:"UNIQUE NOT NULL"`
Name string `xorm:"UNIQUE NOT NULL"`
LowerName string `xorm:"VARCHAR(191) UNIQUE NOT NULL"`
Name string `xorm:"VARCHAR(191) UNIQUE NOT NULL"`
FullName string
// Email is the primary email address (to be used for communication)
Email string `xorm:"NOT NULL"`
Expand Down
2 changes: 1 addition & 1 deletion models/user_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
type EmailAddress struct {
ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"INDEX NOT NULL"`
Email string `xorm:"UNIQUE NOT NULL"`
Email string `xorm:"VARCHAR(191) UNIQUE NOT NULL"`
IsActivated bool
IsPrimary bool `xorm:"-"`
}
Expand Down
2 changes: 1 addition & 1 deletion models/user_openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var (
type UserOpenID struct {
ID int64 `xorm:"pk autoincr"`
UID int64 `xorm:"INDEX NOT NULL"`
URI string `xorm:"UNIQUE NOT NULL"`
URI string `xorm:"VARCHAR(191) UNIQUE NOT NULL"`
Show bool `xorm:"DEFAULT false"`
}

Expand Down