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

Migrate to Xormigrate #31523

Closed
wants to merge 69 commits into from
Closed
Show file tree
Hide file tree
Changes from 61 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
f1f542d
Add basic stuff
qwerty287 Jun 29, 2024
e5c57a6
Some more stuff
qwerty287 Jun 29, 2024
3a952f8
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Jul 7, 2024
bedf168
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Jul 15, 2024
8b49ed9
Merge branch 'main' into xormigrate
qwerty287 Jul 16, 2024
8adc25a
Merge branch 'main' into xormigrate
qwerty287 Jul 18, 2024
5ef3cf4
make fmt
qwerty287 Jul 18, 2024
e46974d
fix lints
qwerty287 Jul 18, 2024
7d417e0
Remove duplicated id
qwerty287 Jul 18, 2024
faa3c52
fix duplicated id
qwerty287 Jul 18, 2024
a81a925
Revert "Remove duplicated id"
qwerty287 Jul 18, 2024
694b13e
fix missing id
qwerty287 Jul 18, 2024
507abd1
make tidy
qwerty287 Jul 18, 2024
be176ed
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Jul 20, 2024
17e9d32
do nothing on fresh db
qwerty287 Jul 20, 2024
6c057fe
error when version missing
qwerty287 Jul 20, 2024
737c444
fix
qwerty287 Jul 20, 2024
2203bcb
only run init when version is not existing
qwerty287 Jul 20, 2024
e4acc57
Run migrations if they are not done yet
qwerty287 Jul 21, 2024
882be8f
Merge branch 'main' into xormigrate
qwerty287 Jul 21, 2024
4e82ec3
Merge branch 'main' into xormigrate
qwerty287 Jul 23, 2024
980a813
Merge branch 'main' into xormigrate
qwerty287 Jul 24, 2024
68d2ba9
Merge branch 'main' into xormigrate
qwerty287 Jul 25, 2024
50b1145
Merge branch 'main' into xormigrate
qwerty287 Jul 27, 2024
75d10e9
Merge branch 'main' into xormigrate
qwerty287 Jul 30, 2024
8f3310d
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Aug 4, 2024
8e3061b
Merge branch 'main' into xormigrate
qwerty287 Aug 5, 2024
b62a5db
Merge branch 'main' into xormigrate
qwerty287 Aug 6, 2024
a88372f
Merge branch 'main' into xormigrate
qwerty287 Aug 8, 2024
02c2362
Merge remote-tracking branch 'upstream/main' into xormigrate
qwerty287 Aug 10, 2024
f0bdbfe
Merge branch 'main' into xormigrate
qwerty287 Aug 13, 2024
a8d4725
Merge branch 'main' into xormigrate
qwerty287 Aug 14, 2024
e3ab8fa
Merge branch 'main' into xormigrate
qwerty287 Aug 18, 2024
614d6f0
Merge branch 'main' into xormigrate
qwerty287 Aug 19, 2024
dfe30fe
Move table drop
qwerty287 Aug 19, 2024
da99ba5
fix lint
qwerty287 Aug 19, 2024
23e81c5
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Aug 23, 2024
7d7cbfc
Merge branch 'main' into xormigrate
qwerty287 Aug 24, 2024
f90a80d
Merge branch 'main' into xormigrate
qwerty287 Aug 25, 2024
aab6535
Merge branch 'main' into xormigrate
qwerty287 Aug 29, 2024
cf069a5
Merge branch 'main' into xormigrate
qwerty287 Aug 30, 2024
6890d89
Merge branch 'main' into xormigrate
qwerty287 Sep 2, 2024
45d1577
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Sep 8, 2024
8899c86
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Sep 11, 2024
52fd484
Merge branch 'main' into xormigrate
qwerty287 Sep 11, 2024
65410c3
Merge branch 'main' into xormigrate
qwerty287 Sep 14, 2024
6f0a056
Merge branch 'main' into xormigrate
qwerty287 Sep 16, 2024
3edbed9
Merge branch 'main' into xormigrate
qwerty287 Sep 17, 2024
9e37adc
Merge branch 'main' into xormigrate
techknowlogick Sep 18, 2024
bf0a25d
Merge branch 'main' into xormigrate
qwerty287 Sep 20, 2024
e6cd5e1
Merge branch 'main' into xormigrate
qwerty287 Sep 21, 2024
3bad064
Merge branch 'main' into xormigrate
qwerty287 Sep 24, 2024
82b2810
Merge branch 'main' into xormigrate
qwerty287 Sep 25, 2024
7c079ca
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Oct 5, 2024
0da63df
fix merge
qwerty287 Oct 5, 2024
e85a925
Merge branch 'main' into xormigrate
qwerty287 Oct 6, 2024
3149ae8
Merge branch 'main' into xormigrate
qwerty287 Oct 8, 2024
7ffcd7f
Update models/migrations/v1_23/v306.go
qwerty287 Oct 8, 2024
dd1fbd4
Merge branch 'main' into xormigrate
qwerty287 Oct 9, 2024
765d113
Merge branch 'main' into xormigrate
qwerty287 Oct 10, 2024
d917463
Merge branch 'main' into xormigrate
qwerty287 Oct 20, 2024
2b7b8b7
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Oct 28, 2024
8f61053
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Nov 8, 2024
0ebc225
Merge branch 'main' into xormigrate
qwerty287 Nov 10, 2024
8588af9
Merge branch 'main' into xormigrate
qwerty287 Nov 13, 2024
a7b457b
Merge remote-tracking branch 'origin/main' into xormigrate
qwerty287 Nov 17, 2024
30811df
Merge branch 'main' into xormigrate
qwerty287 Nov 18, 2024
aa233c8
Merge branch 'main' into xormigrate
qwerty287 Nov 19, 2024
e97c2c9
Merge branch 'main' into xormigrate
qwerty287 Nov 24, 2024
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
5 changes: 5 additions & 0 deletions assets/go-licenses.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ require (
gopkg.in/ini.v1 v1.67.0
gopkg.in/yaml.v3 v3.0.1
mvdan.cc/xurls/v2 v2.5.0
src.techknowlogick.com/xormigrate v1.7.1
strk.kbt.io/projects/go/libravatar v0.0.0-20191008002943-06d1c002b251
xorm.io/builder v0.3.13
xorm.io/xorm v1.3.9
Expand Down
161 changes: 161 additions & 0 deletions go.sum

Large diffs are not rendered by default.

156 changes: 33 additions & 123 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,45 +27,15 @@ import (
"code.gitea.io/gitea/models/migrations/v1_8"
"code.gitea.io/gitea/models/migrations/v1_9"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
"xorm.io/xorm/names"
)

const minDBVersion = 70 // Gitea 1.5.3

// Migration describes on migration from lower version to high version
type Migration interface {
Description() string
Migrate(*xorm.Engine) error
}

type migration struct {
description string
migrate func(*xorm.Engine) error
}

// NewMigration creates a new migration
func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
return &migration{desc, fn}
}

// Description returns the migration's description
func (m *migration) Description() string {
return m.description
}

// Migrate executes the migration
func (m *migration) Migrate(x *xorm.Engine) error {
return m.migrate(x)
}

// Version describes the version table. Should have only one row with id==1
type Version struct {
ID int64 `xorm:"pk autoincr"`
Version int64
func NewMigration(desc string, fn func(*xorm.Engine) error) *xormigrate.Migration {
return &xormigrate.Migration{ID: desc, Migrate: fn}
}

// Use noopMigration when there is a migration that has been no-oped
Expand All @@ -74,7 +44,10 @@ var noopMigration = func(_ *xorm.Engine) error { return nil }
// This is a sequence of migrations. Add new migrations to the bottom of the list.
// If you want to "retire" a migration, remove it from the top of the list and
// update minDBVersion accordingly
var migrations = []Migration{
var migrations = []*xormigrate.Migration{
// Migration to Xormigrate must happen before anything else happens
NewMigration("Migrate to Xormigrate", v1_23.MigrateToXormigrate),

// Gitea 1.5.0 ends at v69

// v70 -> v71
Expand Down Expand Up @@ -157,7 +130,7 @@ var migrations = []Migration{
// Gitea 1.10.0 ends at v102

// v102 -> v103
NewMigration("update migration repositories' service type", v1_11.DropColumnHeadUserNameOnPullRequest),
NewMigration("update migration repositories' service type v2", v1_11.DropColumnHeadUserNameOnPullRequest),
// v103 -> v104
NewMigration("Add WhitelistDeployKeys to protected branch", v1_11.AddWhitelistDeployKeysToBranches),
// v104 -> v105
Expand Down Expand Up @@ -605,112 +578,49 @@ var migrations = []Migration{
NewMigration("Add Repository Licenses", v1_23.AddRepositoryLicenses),
}

// GetCurrentDBVersion returns the current db version
func GetCurrentDBVersion(x *xorm.Engine) (int64, error) {
if err := x.Sync(new(Version)); err != nil {
return -1, fmt.Errorf("sync: %w", err)
}

currentVersion := &Version{ID: 1}
has, err := x.Get(currentVersion)
if err != nil {
return -1, fmt.Errorf("get: %w", err)
}
if !has {
return -1, nil
}
return currentVersion.Version, nil
}

// ExpectedVersion returns the expected db version
func ExpectedVersion() int64 {
return int64(minDBVersion + len(migrations))
}

// EnsureUpToDate will check if the db is at the correct version
func EnsureUpToDate(x *xorm.Engine) error {
currentDB, err := GetCurrentDBVersion(x)
if err != nil {
return err
}

if currentDB < 0 {
return fmt.Errorf("Database has not been initialized")
}

if minDBVersion > currentDB {
return fmt.Errorf("DB version %d (<= %d) is too old for auto-migration. Upgrade to Gitea 1.6.4 first then upgrade to this version", currentDB, minDBVersion)
}

expected := ExpectedVersion()

if currentDB != expected {
return fmt.Errorf(`Current database version %d is not equal to the expected version %d. Please run "gitea [--config /path/to/app.ini] migrate" to update the database version`, currentDB, expected)
for _, m := range migrations {
count, err := x.
In("id", m.ID).
Count(&xormigrate.Migration{})
if err != nil {
return err
}
if count < 1 {
return fmt.Errorf("Database misses migration %s", m.ID)
}
}

return nil
}

// EnsureUpToDate will check if the db is completely new
func IsFreshDB(x *xorm.Engine) (bool, error) {
exist, err := x.IsTableExist(&xormigrate.Migration{})
return !exist, err
}

// Migrate database to current version
func Migrate(x *xorm.Engine) error {
// Set a new clean the default mapper to GonicMapper as that is the default for Gitea.
x.SetMapper(names.GonicMapper{})
if err := x.Sync(new(Version)); err != nil {
return fmt.Errorf("sync: %w", err)
}

currentVersion := &Version{ID: 1}
has, err := x.Get(currentVersion)
if err != nil {
return fmt.Errorf("get: %w", err)
} else if !has {
// If the version record does not exist we think
// it is a fresh installation and we can skip all migrations.
currentVersion.ID = 0
currentVersion.Version = int64(minDBVersion + len(migrations))

if _, err = x.InsertOne(currentVersion); err != nil {
return fmt.Errorf("insert: %w", err)
}
}

v := currentVersion.Version
if minDBVersion > v {
log.Fatal(`Gitea no longer supports auto-migration from your previously installed version.
Please try upgrading to a lower version first (suggested v1.6.4), then upgrade to this version.`)
return nil
}

// Downgrading Gitea's database version not supported
if int(v-minDBVersion) > len(migrations) {
msg := fmt.Sprintf("Your database (migration version: %d) is for a newer Gitea, you can not use the newer database for this old Gitea release (%d).", v, minDBVersion+len(migrations))
msg += "\nGitea will exit to keep your database safe and unchanged. Please use the correct Gitea release, do not change the migration version manually (incorrect manual operation may lose data)."
if !setting.IsProd {
msg += fmt.Sprintf("\nIf you are in development and really know what you're doing, you can force changing the migration version by executing: UPDATE version SET version=%d WHERE id=1;", minDBVersion+len(migrations))
}
log.Fatal("Migration Error: %s", msg)
return nil
}

// Some migration tasks depend on the git command
if git.DefaultContext == nil {
if err = git.InitSimple(context.Background()); err != nil {
if err := git.InitSimple(context.Background()); err != nil {
return err
}
}

// Migrate
for i, m := range migrations[v-minDBVersion:] {
log.Info("Migration[%d]: %s", v+int64(i), m.Description())
// Reset the mapper between each migration - migrations are not supposed to depend on each other
x.SetMapper(names.GonicMapper{})
if err = m.Migrate(x); err != nil {
return fmt.Errorf("migration[%d]: %s failed: %w", v+int64(i), m.Description(), err)
}
currentVersion.Version = v + int64(i) + 1
if _, err = x.ID(1).Update(currentVersion); err != nil {
return err
}
m := xormigrate.New(x, migrations)

if exist, _ := x.IsTableExist("version"); !exist {
// if the version table exists, we still have the old migration system
// and the schema init should not run then
m.InitSchema(noopMigration)
}
return nil

return m.Migrate()
}
Loading
Loading