-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"` | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is a limit being added here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"` | ||
|
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 | ||
} |
There was a problem hiding this comment.
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 ?