fix: keying SQLite migration

Also run the keying migration when upgrading from Gitea.

Add type change support for SQLite field from TEXT to BLOB.
This commit is contained in:
Earl Warren 2024-12-23 10:15:41 +01:00
parent 2dc167cbb9
commit 9524d6d430
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 26 additions and 0 deletions

View file

@ -20,9 +20,34 @@ import (
func MigrateTwoFactorToKeying(x *xorm.Engine) error {
var err error
// When upgrading from Forgejo v9 to v10, this migration will already be
// called from models/migrations/migrations.go migration 304 and must not
// be run twice.
var version int
_, err = x.Table("version").Where("`id` = 1").Select("version").Get(&version)
if err != nil {
// the version table does not exist when a test environment only applies Forgejo migrations
} else if version > 304 {
return nil
}
switch x.Dialect().URI().DBType {
case schemas.MYSQL:
_, err = x.Exec("ALTER TABLE `two_factor` MODIFY `secret` BLOB")
case schemas.SQLITE:
_, err = x.Exec("ALTER TABLE `two_factor` RENAME COLUMN `secret` TO `secret_backup`")
if err != nil {
return err
}
_, err = x.Exec("ALTER TABLE `two_factor` ADD COLUMN `secret` BLOB")
if err != nil {
return err
}
_, err = x.Exec("UPDATE `two_factor` SET `secret` = `secret_backup`")
if err != nil {
return err
}
_, err = x.Exec("ALTER TABLE `two_factor` DROP COLUMN `secret_backup`")
case schemas.POSTGRES:
_, err = x.Exec("ALTER TABLE `two_factor` ALTER COLUMN `secret` SET DATA TYPE bytea USING secret::text::bytea")
}

View file

@ -365,6 +365,7 @@ func prepareMigrationTasks() []*migration {
// Migration to Forgejo v10
newMigration(303, "Gitea last drop", v1_23.GiteaLastDrop),
newMigration(304, "Migrate `secret` column to store keying material", forgejo_migrations.MigrateTwoFactorToKeying),
}
return preparedMigrations
}