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 all 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 @@ -134,6 +134,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.

161 changes: 31 additions & 130 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,57 +21,41 @@
"code.gitea.io/gitea/models/migrations/v1_20"
"code.gitea.io/gitea/models/migrations/v1_21"
"code.gitea.io/gitea/models/migrations/v1_22"
"code.gitea.io/gitea/models/migrations/v1_23"

Check failure on line 24 in models/migrations/migrations.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

could not import code.gitea.io/gitea/models/migrations/v1_23 (-: # code.gitea.io/gitea/models/migrations/v1_23

Check failure on line 24 in models/migrations/migrations.go

View workflow job for this annotation

GitHub Actions / lint-backend

could not import code.gitea.io/gitea/models/migrations/v1_23 (-: # code.gitea.io/gitea/models/migrations/v1_23

Check failure on line 24 in models/migrations/migrations.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

could not import code.gitea.io/gitea/models/migrations/v1_23 (-: # code.gitea.io/gitea/models/migrations/v1_23
"code.gitea.io/gitea/models/migrations/v1_6"
"code.gitea.io/gitea/models/migrations/v1_7"
"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

type migration struct {
idNumber int64 // DB version is "the last migration's idNumber" + 1
description string
migrate func(*xorm.Engine) error
}

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

// 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 // DB version is "the last migration's idNumber" + 1
func newMigration(idNumber int64, desc string, fn func(*xorm.Engine) error) *xormigrate.Migration {
return &xormigrate.Migration{ID: fmt.Sprint(idNumber), Description: desc, Migrate: fn}
}

// Use noopMigration when there is a migration that has been no-oped
var noopMigration = func(_ *xorm.Engine) error { return nil }

var preparedMigrations []*migration
var preparedMigrations []*xormigrate.Migration

// 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
func prepareMigrationTasks() []*migration {
func prepareMigrationTasks() []*xormigrate.Migration {
if preparedMigrations != nil {
return preparedMigrations
}
preparedMigrations = []*migration{
preparedMigrations = []*xormigrate.Migration{
// Gitea 1.5.0 ends at database version 69
// Migration to Xormigrate must happen before anything else happens
newMigration(307, "Migrate to Xormigrate", v1_23.MigrateToXormigrate),

newMigration(70, "add issue_dependencies", v1_6.AddIssueDependencies),
newMigration(71, "protect each scratch token", v1_6.AddScratchHash),
Expand Down Expand Up @@ -371,134 +355,51 @@
return preparedMigrations
}

// 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
}

func calcDBVersion(migrations []*migration) int64 {
dbVer := int64(minDBVersion + len(migrations))
if migrations[0].idNumber != minDBVersion {
panic("migrations should start at minDBVersion")
}
if dbVer != migrations[len(migrations)-1].idNumber+1 {
panic("migrations are not in order")
}
return dbVer
}

// ExpectedDBVersion returns the expected db version
func ExpectedDBVersion() int64 {
return calcDBVersion(prepareMigrationTasks())
}

// 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)
}

expectedDB := ExpectedDBVersion()

if currentDB != expectedDB {
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, expectedDB)
for _, m := range prepareMigrationTasks() {
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
}

func getPendingMigrations(curDBVer int64, migrations []*migration) []*migration {
return migrations[curDBVer-minDBVersion:]
}

func migrationIDNumberToDBVersion(idNumber int64) int64 {
return idNumber + 1
// 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 {
migrations := prepareMigrationTasks()
maxDBVer := calcDBVersion(migrations)

// 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, it is a fresh installation, and we can skip all migrations.
// XORM model framework will create all tables when initializing.
currentVersion.ID = 0
currentVersion.Version = maxDBVer
if _, err = x.InsertOne(currentVersion); err != nil {
return fmt.Errorf("insert: %w", err)
}
}

curDBVer := currentVersion.Version
// Outdated Gitea database version is not supported
if curDBVer < minDBVersion {
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 maxDBVer < curDBVer {
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).", curDBVer, maxDBVer)
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;", maxDBVer)
}
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 _, m := range getPendingMigrations(curDBVer, migrations) {
log.Info("Migration[%d]: %s", m.idNumber, 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", m.idNumber, m.description, err)
}
currentVersion.Version = migrationIDNumberToDBVersion(m.idNumber)
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()
}
1 change: 1 addition & 0 deletions models/migrations/v1_23/v309.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@

func ImproveNotificationTableIndices(x *xorm.Engine) error {
return x.Sync(&improveNotificationTableIndicesAction{})
>>>>>>> origin/main

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-sqlite

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-mssql

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / backend

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-pgsql

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-unit

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-mysql

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected statement, found '>>'

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected statement, found '>>'

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / test-e2e

syntax error: unexpected >>, expected }

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected >>, expected }) (typecheck)

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

syntax error: unexpected >>, expected } (typecheck)

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected >>, expected }) (typecheck)

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-backend

syntax error: unexpected >>, expected } (typecheck)

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected >>, expected }) (typecheck)

Check failure on line 77 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

syntax error: unexpected >>, expected } (typecheck)
}

Check failure on line 78 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected '}', found 'EOF'

Check failure on line 78 in models/migrations/v1_23/v309.go

View workflow job for this annotation

GitHub Actions / checks-backend

expected '}', found 'EOF'
Loading
Loading