From a019e55c74a984251c4004dbeafc6cc97de66339 Mon Sep 17 00:00:00 2001 From: Adora Laura Kalb Date: Mon, 6 May 2024 21:38:51 +0200 Subject: [PATCH] add db migrations --- internal/app/routines.go | 2 +- .../20240506000001_mfa_rename_tables.go | 23 +++++++++++++++++++ models/multifactor.go | 4 ++-- models/user.go | 2 +- 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 migrations/20240506000001_mfa_rename_tables.go diff --git a/internal/app/routines.go b/internal/app/routines.go index 3f2b873..51372eb 100644 --- a/internal/app/routines.go +++ b/internal/app/routines.go @@ -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) } diff --git a/migrations/20240506000001_mfa_rename_tables.go b/migrations/20240506000001_mfa_rename_tables.go new file mode 100644 index 0000000..903cabe --- /dev/null +++ b/migrations/20240506000001_mfa_rename_tables.go @@ -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 + }) +} diff --git a/models/multifactor.go b/models/multifactor.go index 19e2983..af2f452 100644 --- a/models/multifactor.go +++ b/models/multifactor.go @@ -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"` } diff --git a/models/user.go b/models/user.go index 9727f0b..6d1cb6f 100644 --- a/models/user.go +++ b/models/user.go @@ -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"` }