Skip to content

Commit

Permalink
database: set Handle direcetly during initialization (#7699)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon committed Mar 20, 2024
1 parent dd49412 commit 8d2386b
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 12 deletions.
6 changes: 1 addition & 5 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func NewConnection(w logger.Writer) (*gorm.DB, error) {
TwoFactors = &twoFactorsStore{DB: db}
Users = NewUsersStore(db)

Handle = &DB{db: db}
return db, nil
}

Expand All @@ -148,11 +149,6 @@ type DB struct {
// single-thread process).
var Handle *DB

// SetHandle updates the global database handle with the given connection.
func SetHandle(db *gorm.DB) {
Handle = &DB{db: db}
}

func (db *DB) AccessTokens() *AccessTokensStore {
return newAccessTokensStore(db.db)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ func SetEngine() (*gorm.DB, error) {
return NewConnection(gormLogger)
}

func NewEngine() (*gorm.DB, error) {
func NewEngine() error {
db, err := SetEngine()
if err != nil {
return nil, err
return err
}

if err = migrations.Migrate(db); err != nil {
return nil, fmt.Errorf("migrate: %v", err)
return fmt.Errorf("migrate: %v", err)
}

if err = x.StoreEngine("InnoDB").Sync2(legacyTables...); err != nil {
return nil, errors.Wrap(err, "sync tables")
return errors.Wrap(err, "sync tables")
}

return db, nil
return nil
}

type Statistic struct {
Expand Down
3 changes: 1 addition & 2 deletions internal/route/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ func GlobalInit(customConf string) error {
if conf.Security.InstallLock {
highlight.NewContext()
markup.NewSanitizer()
db, err := database.NewEngine()
err := database.NewEngine()
if err != nil {
log.Fatal("Failed to initialize ORM engine: %v", err)
}
database.SetHandle(db)
database.HasEngine = true

database.LoadRepoConfig()
Expand Down

0 comments on commit 8d2386b

Please sign in to comment.