mirror of
https://github.com/h44z/wg-portal.git
synced 2025-08-09 15:02:24 +00:00
do not start with V1 database
This commit is contained in:
parent
05f96b3776
commit
f3a3a60c80
@ -37,6 +37,7 @@ type GormLogger struct {
|
|||||||
SourceField string
|
SourceField string
|
||||||
IgnoreErrRecordNotFound bool
|
IgnoreErrRecordNotFound bool
|
||||||
Debug bool
|
Debug bool
|
||||||
|
Silent bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogger(slowThreshold time.Duration, debug bool) *GormLogger {
|
func NewLogger(slowThreshold time.Duration, debug bool) *GormLogger {
|
||||||
@ -44,27 +45,46 @@ func NewLogger(slowThreshold time.Duration, debug bool) *GormLogger {
|
|||||||
SlowThreshold: slowThreshold,
|
SlowThreshold: slowThreshold,
|
||||||
Debug: debug,
|
Debug: debug,
|
||||||
IgnoreErrRecordNotFound: true,
|
IgnoreErrRecordNotFound: true,
|
||||||
|
Silent: false,
|
||||||
SourceField: "src",
|
SourceField: "src",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *GormLogger) LogMode(logger.LogLevel) logger.Interface {
|
func (l *GormLogger) LogMode(level logger.LogLevel) logger.Interface {
|
||||||
|
if level == logger.Silent {
|
||||||
|
l.Silent = true
|
||||||
|
} else {
|
||||||
|
l.Silent = false
|
||||||
|
}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *GormLogger) Info(ctx context.Context, s string, args ...interface{}) {
|
func (l *GormLogger) Info(ctx context.Context, s string, args ...interface{}) {
|
||||||
|
if l.Silent {
|
||||||
|
return
|
||||||
|
}
|
||||||
logrus.WithContext(ctx).Infof(s, args...)
|
logrus.WithContext(ctx).Infof(s, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *GormLogger) Warn(ctx context.Context, s string, args ...interface{}) {
|
func (l *GormLogger) Warn(ctx context.Context, s string, args ...interface{}) {
|
||||||
|
if l.Silent {
|
||||||
|
return
|
||||||
|
}
|
||||||
logrus.WithContext(ctx).Warnf(s, args...)
|
logrus.WithContext(ctx).Warnf(s, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *GormLogger) Error(ctx context.Context, s string, args ...interface{}) {
|
func (l *GormLogger) Error(ctx context.Context, s string, args ...interface{}) {
|
||||||
|
if l.Silent {
|
||||||
|
return
|
||||||
|
}
|
||||||
logrus.WithContext(ctx).Errorf(s, args...)
|
logrus.WithContext(ctx).Errorf(s, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
|
||||||
|
if l.Silent {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
elapsed := time.Since(begin)
|
elapsed := time.Since(begin)
|
||||||
sql, rows := fc()
|
sql, rows := fc()
|
||||||
fields := logrus.Fields{
|
fields := logrus.Fields{
|
||||||
@ -156,14 +176,37 @@ func NewSqlRepository(db *gorm.DB) (*SqlRepo, error) {
|
|||||||
db: db,
|
db: db,
|
||||||
}
|
}
|
||||||
|
|
||||||
err := repo.migrate()
|
if err := repo.preCheck(); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := repo.migrate(); err != nil {
|
||||||
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
return nil, fmt.Errorf("failed to initialize database: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return repo, nil
|
return repo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *SqlRepo) preCheck() error {
|
||||||
|
// WireGuard Portal v1 database migration table
|
||||||
|
type DatabaseMigrationInfo struct {
|
||||||
|
Version string `gorm:"primaryKey"`
|
||||||
|
Applied time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
// temporarily disable logger as the next request might fail (intentionally)
|
||||||
|
r.db.Logger.LogMode(logger.Silent)
|
||||||
|
defer func() { r.db.Logger.LogMode(logger.Info) }()
|
||||||
|
|
||||||
|
lastVersion := DatabaseMigrationInfo{}
|
||||||
|
err := r.db.Order("applied desc, version desc").FirstOrInit(&lastVersion).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil // we probably don't have a V1 database =)
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("detected a WireGuard Portal V1 database (version: %s) - please migrate first", lastVersion.Version)
|
||||||
|
}
|
||||||
|
|
||||||
func (r *SqlRepo) migrate() error {
|
func (r *SqlRepo) migrate() error {
|
||||||
logrus.Tracef("sysstat migration: %v", r.db.AutoMigrate(&SysStat{}))
|
logrus.Tracef("sysstat migration: %v", r.db.AutoMigrate(&SysStat{}))
|
||||||
logrus.Tracef("user migration: %v", r.db.AutoMigrate(&domain.User{}))
|
logrus.Tracef("user migration: %v", r.db.AutoMigrate(&domain.User{}))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user