Skip to content

Commit c842388

Browse files
authored
Refactor setting.Database.UseXXX to methods (#23354)
Replace #23350. Refactor `setting.Database.UseMySQL` to `setting.Database.Type.IsMySQL()`. To avoid mismatching between `Type` and `UseXXX`. This refactor can fix the bug mentioned in #23350, so it should be backported.
1 parent 84a2993 commit c842388

File tree

36 files changed

+103
-96
lines changed

36 files changed

+103
-96
lines changed

cmd/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func runConvert(ctx *cli.Context) error {
3535
log.Info("Log path: %s", setting.Log.RootPath)
3636
log.Info("Configuration file: %s", setting.CustomConf)
3737

38-
if !setting.Database.UseMySQL {
38+
if !setting.Database.Type.IsMySQL() {
3939
fmt.Println("This command can only be used with a MySQL database")
4040
return nil
4141
}

cmd/dump.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func runDump(ctx *cli.Context) error {
279279
}()
280280

281281
targetDBType := ctx.String("database")
282-
if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
282+
if len(targetDBType) > 0 && targetDBType != setting.Database.Type.String() {
283283
log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
284284
} else {
285285
log.Info("Dumping database...")

models/activities/action.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (a *Action) TableIndices() []*schemas.Index {
9999
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
100100

101101
indices := []*schemas.Index{actUserIndex, repoIndex}
102-
if setting.Database.UsePostgreSQL {
102+
if setting.Database.Type.IsPostgreSQL() {
103103
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
104104
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
105105
indices = append(indices, cudIndex)
@@ -640,15 +640,15 @@ func DeleteIssueActions(ctx context.Context, repoID, issueID int64) error {
640640

641641
// CountActionCreatedUnixString count actions where created_unix is an empty string
642642
func CountActionCreatedUnixString(ctx context.Context) (int64, error) {
643-
if setting.Database.UseSQLite3 {
643+
if setting.Database.Type.IsSQLite3() {
644644
return db.GetEngine(ctx).Where(`created_unix = ""`).Count(new(Action))
645645
}
646646
return 0, nil
647647
}
648648

649649
// FixActionCreatedUnixString set created_unix to zero if it is an empty string
650650
func FixActionCreatedUnixString(ctx context.Context) (int64, error) {
651-
if setting.Database.UseSQLite3 {
651+
if setting.Database.Type.IsSQLite3() {
652652
res, err := db.GetEngine(ctx).Exec(`UPDATE action SET created_unix = 0 WHERE created_unix = ""`)
653653
if err != nil {
654654
return 0, err

models/activities/action_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func TestGetFeedsCorrupted(t *testing.T) {
243243
}
244244

245245
func TestConsistencyUpdateAction(t *testing.T) {
246-
if !setting.Database.UseSQLite3 {
246+
if !setting.Database.Type.IsSQLite3() {
247247
t.Skip("Test is only for SQLite database.")
248248
}
249249
assert.NoError(t, unittest.PrepareTestDatabase())

models/activities/user_heatmap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ func getUserHeatmapData(user *user_model.User, team *organization.Team, doer *us
3939
groupBy := "created_unix / 900 * 900"
4040
groupByName := "timestamp" // We need this extra case because mssql doesn't allow grouping by alias
4141
switch {
42-
case setting.Database.UseMySQL:
42+
case setting.Database.Type.IsMySQL():
4343
groupBy = "created_unix DIV 900 * 900"
44-
case setting.Database.UseMSSQL:
44+
case setting.Database.Type.IsMSSQL():
4545
groupByName = groupBy
4646
}
4747

models/db/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// BuildCaseInsensitiveLike returns a condition to check if the given value is like the given key case-insensitively.
1616
// Handles especially SQLite correctly as UPPER there only transforms ASCII letters.
1717
func BuildCaseInsensitiveLike(key, value string) builder.Cond {
18-
if setting.Database.UseSQLite3 {
18+
if setting.Database.Type.IsSQLite3() {
1919
return builder.Like{"UPPER(" + key + ")", util.ToUpperASCII(value)}
2020
}
2121
return builder.Like{"UPPER(" + key + ")", strings.ToUpper(value)}

models/db/engine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ func newXORMEngine() (*xorm.Engine, error) {
101101

102102
var engine *xorm.Engine
103103

104-
if setting.Database.UsePostgreSQL && len(setting.Database.Schema) > 0 {
104+
if setting.Database.Type.IsPostgreSQL() && len(setting.Database.Schema) > 0 {
105105
// OK whilst we sort out our schema issues - create a schema aware postgres
106106
registerPostgresSchemaDriver()
107107
engine, err = xorm.NewEngine("postgresschema", connStr)
108108
} else {
109-
engine, err = xorm.NewEngine(setting.Database.Type, connStr)
109+
engine, err = xorm.NewEngine(setting.Database.Type.String(), connStr)
110110
}
111111

112112
if err != nil {

models/db/index.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func postgresGetNextResourceIndex(ctx context.Context, tableName string, groupID
7373

7474
// GetNextResourceIndex generates a resource index, it must run in the same transaction where the resource is created
7575
func GetNextResourceIndex(ctx context.Context, tableName string, groupID int64) (int64, error) {
76-
if setting.Database.UsePostgreSQL {
76+
if setting.Database.Type.IsPostgreSQL() {
7777
return postgresGetNextResourceIndex(ctx, tableName, groupID)
7878
}
7979

models/db/sequence.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// CountBadSequences looks for broken sequences from recreate-table mistakes
1515
func CountBadSequences(_ context.Context) (int64, error) {
16-
if !setting.Database.UsePostgreSQL {
16+
if !setting.Database.Type.IsPostgreSQL() {
1717
return 0, nil
1818
}
1919

@@ -34,7 +34,7 @@ func CountBadSequences(_ context.Context) (int64, error) {
3434

3535
// FixBadSequences fixes for broken sequences from recreate-table mistakes
3636
func FixBadSequences(_ context.Context) error {
37-
if !setting.Database.UsePostgreSQL {
37+
if !setting.Database.Type.IsPostgreSQL() {
3838
return nil
3939
}
4040

models/git/commit_status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func postgresGetCommitStatusIndex(ctx context.Context, repoID int64, sha string)
6565

6666
// GetNextCommitStatusIndex retried 3 times to generate a resource index
6767
func GetNextCommitStatusIndex(ctx context.Context, repoID int64, sha string) (int64, error) {
68-
if setting.Database.UsePostgreSQL {
68+
if setting.Database.Type.IsPostgreSQL() {
6969
return postgresGetCommitStatusIndex(ctx, repoID, sha)
7070
}
7171

0 commit comments

Comments
 (0)