wocao
This commit is contained in:
+40
-41
@@ -1,49 +1,33 @@
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
package state
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"xorm.io/xorm"
|
||||
"xorm.io/xorm/names"
|
||||
)
|
||||
|
||||
|
||||
|
||||
type BaseModel struct {
|
||||
ID int64 `xorm:"pk autoincr" json:"id"`
|
||||
CreatedAt time.Time `xorm:"created" json:"created_at"`
|
||||
UpdatedAt time.Time `xorm:"updated" json:"updated_at"`
|
||||
DeletedAt *time.Time `xorm:"deleted" json:"deleted_at,omitempty"`
|
||||
ID int64 `xorm:"pk autoincr" json:"id"`
|
||||
CreatedAt time.Time `xorm:"created" json:"created_at"`
|
||||
UpdatedAt time.Time `xorm:"updated" json:"updated_at"`
|
||||
// DeletedAt *time.Time `xorm:"deleted" json:"deleted_at,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
type Namespace struct {
|
||||
BaseModel `xorm:"extends"`
|
||||
Name string `xorm:"varchar(255) unique notnull" json:"name"`
|
||||
PasswordHash string `xorm:"varchar(255)" json:"-"`
|
||||
}
|
||||
|
||||
type Repo struct {
|
||||
BaseModel `xorm:"extends"`
|
||||
NamespaceID int64 `xorm:"notnull unique(cred)" json:"namespace_id"`
|
||||
Namespace string `xorm:"varchar(255) notnull index" json:"namespace"`
|
||||
NameID int64 `xorm:"notnull unique(cred)" json:"name_id"`
|
||||
Name string `xorm:"varchar(255) notnull" json:"name"`
|
||||
Name string `xorm:"varchar(255) notnull" json:"reponame"`
|
||||
IsPrivate bool `xorm:"default false" json:"is_private"`
|
||||
}
|
||||
|
||||
|
||||
type SSHKey struct {
|
||||
BaseModel `xorm:"extends"`
|
||||
PublicKey string `xorm:"text notnull" json:"-"`
|
||||
@@ -53,9 +37,6 @@ type SSHKey struct {
|
||||
LastUsedAt *time.Time `xorm:"" json:"last_used_at,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
type PAT struct {
|
||||
BaseModel `xorm:"extends"`
|
||||
TokenHash string `xorm:"varchar(64) unique notnull" json:"-"`
|
||||
@@ -65,11 +46,6 @@ type PAT struct {
|
||||
LastUsedAt *time.Time `xorm:"" json:"last_used_at,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
type ACL struct {
|
||||
BaseModel `xorm:"extends"`
|
||||
CredType int `xorm:"notnull unique(cred) index" json:"cred_type"`
|
||||
@@ -81,10 +57,10 @@ type ACL struct {
|
||||
|
||||
type CredType int
|
||||
|
||||
|
||||
const (
|
||||
CredTypePAT CredType = 0
|
||||
CredTypeSSH CredType = 1
|
||||
CredTypePAT CredType = 0
|
||||
CredTypeSSH CredType = 1
|
||||
CredTypeUserPswd CredType = 2
|
||||
)
|
||||
|
||||
type TargetType int
|
||||
@@ -94,9 +70,6 @@ const (
|
||||
TargetTypeNS TargetType = 1
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
func InitEngine(driverName, dataSourceName string) (*xorm.Engine, error) {
|
||||
engine, err := xorm.NewEngine(driverName, dataSourceName)
|
||||
if err != nil {
|
||||
@@ -104,7 +77,14 @@ func InitEngine(driverName, dataSourceName string) (*xorm.Engine, error) {
|
||||
}
|
||||
engine.SetTableMapper(names.GonicMapper{})
|
||||
engine.SetColumnMapper(names.GonicMapper{})
|
||||
if driverName == sqliteDriver {
|
||||
if err := migrateLegacySQLite(engine); err != nil {
|
||||
_ = engine.Close()
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err := engine.Sync2(
|
||||
new(Namespace),
|
||||
new(Repo),
|
||||
new(SSHKey),
|
||||
new(PAT),
|
||||
@@ -114,3 +94,22 @@ func InitEngine(driverName, dataSourceName string) (*xorm.Engine, error) {
|
||||
}
|
||||
return engine, nil
|
||||
}
|
||||
|
||||
// migrateLegacySQLite handles model field renames that Sync2 otherwise treats
|
||||
// as new NOT NULL columns. The migration is idempotent and preserves old repos.
|
||||
func migrateLegacySQLite(engine *xorm.Engine) error {
|
||||
rows, err := engine.QueryString("PRAGMA table_info(repo)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("inspect legacy repo schema: %w", err)
|
||||
}
|
||||
columns := make(map[string]bool, len(rows))
|
||||
for _, row := range rows {
|
||||
columns[row["name"]] = true
|
||||
}
|
||||
if columns["repo_name"] && !columns["name"] {
|
||||
if _, err := engine.Exec("ALTER TABLE repo RENAME COLUMN repo_name TO name"); err != nil {
|
||||
return fmt.Errorf("migrate repo column repo_name to name: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user