add db migrations

This commit is contained in:
Adora Laura Kalb 2024-05-06 21:38:51 +02:00
parent 568d8fd117
commit a019e55c74
Signed by: adoralaura
SSH key fingerprint: SHA256:3XrkbR8ikAZJVtYfaUliX1MhmJYVAe/ocIb/MiDHBJ8
4 changed files with 27 additions and 4 deletions

View file

@ -27,7 +27,7 @@ func CleanupLogins() {
log.Printf("[CleanupLogins] Error deleting login transactions: %v\n", err)
}
_, err = models.DB.NewDelete().Table("multifactor").Where("expiresat < NOW()").Where("active = false").Exec(context.Background())
_, err = models.DB.NewDelete().Table("multifactor").Where("expires_at < NOW()").Where("active = false").Exec(context.Background())
if err != nil {
log.Printf("[CleanupLogins] Error deleting login transactions: %v\n", err)
}

View file

@ -0,0 +1,23 @@
package migrations
import (
"context"
"github.com/uptrace/bun"
)
func init() {
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
db.ExecContext(context.Background(), `ALTER TABLE multifactor RENAME COLUMN expiresat TO expires_at`)
db.ExecContext(context.Background(), `ALTER TABLE multifactor RENAME COLUMN totpurl TO totp_secret`)
db.ExecContext(context.Background(), `ALTER TABLE users RENAME COLUMN lastlogin TO last_login`)
db.ExecContext(context.Background(), `ALTER TABLE multifactor_scratchcodes RENAME COLUMN isused TO is_used`)
return nil
}, func(ctx context.Context, db *bun.DB) error {
db.ExecContext(context.Background(), `ALTER TABLE multifactor RENAME COLUMN expires_at TO expiresat`)
db.ExecContext(context.Background(), `ALTER TABLE multifactor RENAME COLUMN totp_secret TO totpurl`)
db.ExecContext(context.Background(), `ALTER TABLE users RENAME COLUMN last_login TO lastlogin`)
db.ExecContext(context.Background(), `ALTER TABLE multifactor_scratchcodes RENAME COLUMN is_used TO isused`)
return nil
})
}

View file

@ -25,8 +25,8 @@ type MFAConfig struct {
bun.BaseModel `bun:"table:multifactor"`
ID int64 `bun:"id,pk,autoincrement"`
UserName string `bun:"username,notnull"`
TOTPSecret string `bun:"totpurl,notnull"`
ExpiresAt time.Time `bun:"expiresat,notnull"`
TOTPSecret string `bun:"totp_secret,notnull"`
ExpiresAt time.Time `bun:"expires_at,notnull"`
Active bool `bun:"active,notnull"`
}

View file

@ -10,7 +10,7 @@ type User struct {
bun.BaseModel `bun:"table:users"`
UserName string `bun:"username,pk" json:"username"`
Created time.Time `bun:"created,notnull,default:now()" json:"created"`
LastLogin time.Time `bun:"lastlogin" json:"last_login"`
LastLogin time.Time `bun:"last_login" json:"last_login"`
PasswordSalt string `bun:"salt" json:"password_salt"`
PasswordHash string `bun:"password" json:"password_hash"`
}