From 33ba8f74f45ebc971bb298f10c932671655fc325 Mon Sep 17 00:00:00 2001 From: Gogs Date: Sun, 23 Feb 2025 21:00:47 +0100 Subject: [PATCH] Change data model. Rename repository.pulls_allow_rebase to repository.pulls_allow_alt, and add repository.pulls_prefer_rebase. --- internal/database/migrations/migrations.go | 4 ++ internal/database/migrations/v23.go | 20 ++++++++ internal/database/migrations/v23_test.go | 56 ++++++++++++++++++++++ internal/database/repo.go | 3 +- 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 internal/database/migrations/v23.go create mode 100644 internal/database/migrations/v23_test.go diff --git a/internal/database/migrations/migrations.go b/internal/database/migrations/migrations.go index 04c1e3636..c431e46ec 100644 --- a/internal/database/migrations/migrations.go +++ b/internal/database/migrations/migrations.go @@ -63,6 +63,10 @@ var migrations = []Migration{ // on v22. Let's make a noop v22 to make sure every instance will not miss a // real future migration. NewMigration("noop", func(*gorm.DB) error { return nil }), + NewMigration( + "rename repository.pulls_allow_rebase -> repository.pulls_allow_alt", + renameRepoPullsAllowRebaseToPullsAllowAlt, + ), } var errMigrationSkipped = errors.New("the migration has been skipped") diff --git a/internal/database/migrations/v23.go b/internal/database/migrations/v23.go new file mode 100644 index 000000000..20b98ed35 --- /dev/null +++ b/internal/database/migrations/v23.go @@ -0,0 +1,20 @@ +// Copyright 2025 The Gogs 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 ( + "gorm.io/gorm" +) + +func renameRepoPullsAllowRebaseToPullsAllowAlt(db *gorm.DB) error { + type Repository struct { + PullsAllowRebase bool `gorm:"not null;default:FALSE"` + PullsAllowAlt bool `gorm:"not null;default:FALSE"` + } + if db.Migrator().HasColumn(&Repository{}, "PullsAllowAlt") { + return errMigrationSkipped + } + return db.Migrator().RenameColumn(&Repository{}, "PullsAllowRebase", "PullsAllowAlt") +} diff --git a/internal/database/migrations/v23_test.go b/internal/database/migrations/v23_test.go new file mode 100644 index 000000000..f5d158c92 --- /dev/null +++ b/internal/database/migrations/v23_test.go @@ -0,0 +1,56 @@ +// Copyright 2025 The Gogs 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 ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "gogs.io/gogs/internal/dbtest" +) + +type repositoryPreV22 struct { + PullsAllowRebase bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` +} + +func (*repositoryPreV22) TableName() string { + return "repository" +} + +type repositoryV22 struct { + PullsAllowAlt bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` +} + +func (*repositoryV22) TableName() string { + return "repository" +} + +func TestRenameRepoPullsAllowRebaseToPullsAllowAlt(t *testing.T) { + if testing.Short() { + t.Skip() + } + t.Parallel() + + db := dbtest.NewDB(t, "renamePullsAllowRebaseToPullsAllowAlt", new(repositoryPreV22)) + err := db.Create( + &repositoryPreV22{ + PullsAllowRebase: false, + }, + ).Error + require.NoError(t, err) + assert.False(t, db.Migrator().HasColumn(&repositoryV22{}, "PullsAllowAlt")) + assert.True(t, db.Migrator().HasColumn(&repositoryPreV22{}, "PullsAllowRebase")) + + err = renameRepoPullsAllowRebaseToPullsAllowAlt(db) + require.NoError(t, err) + assert.True(t, db.Migrator().HasColumn(&repositoryV22{}, "PullsAllowAlt")) + assert.False(t, db.Migrator().HasColumn(&repositoryPreV22{}, "PullsAllowRebase")) + + // Re-run should be skipped + err = renameRepoPullsAllowRebaseToPullsAllowAlt(db) + require.Equal(t, errMigrationSkipped, err) +} diff --git a/internal/database/repo.go b/internal/database/repo.go index f55c35160..23d007008 100644 --- a/internal/database/repo.go +++ b/internal/database/repo.go @@ -201,7 +201,8 @@ type Repository struct { ExternalMetas map[string]string `xorm:"-" gorm:"-" json:"-"` EnablePulls bool `xorm:"NOT NULL DEFAULT true" gorm:"not null;default:TRUE"` PullsIgnoreWhitespace bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` - PullsAllowRebase bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` + PullsPreferRebase bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` + PullsAllowAlt bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` IsFork bool `xorm:"NOT NULL DEFAULT false" gorm:"not null;default:FALSE"` ForkID int64