working commit

This commit is contained in:
2026-06-05 18:25:03 +02:00
commit 7d8abba003
82 changed files with 21863 additions and 0 deletions
+107
View File
@@ -0,0 +1,107 @@
package logic
import (
"context"
"fmt"
"time"
"mbase/app/descriptor"
"mbase/pkg/mbctl"
"go.yaml.in/yaml/v4"
)
func (lg *Logic) GetDump(ctx context.Context, accountID int64, params *mbctl.GetDumpParams) (*mbctl.GetDumpResult, error) {
var err error
res := &mbctl.GetDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyDatabase)
if err != nil {
return res, err
}
if !grantExists {
err := fmt.Errorf("Operation not allowed for the user")
return res, err
}
lg.WaitRestoring()
lg.WaitDumping()
lg.DumpingSemUp()
defer lg.DumpingSemDown()
listAccounts, err := lg.db.CompletedListAccounts(ctx)
if err != nil {
return res, err
}
listGrants, err := lg.db.ListGrants(ctx)
if err != nil {
return res, err
}
lg.DumpingSemDown()
dump := descriptor.Dump{
Timestamp: time.Now().Format(time.RFC3339),
Accounts: listAccounts,
Grants: listGrants,
}
dumpBytes, err := yaml.Marshal(dump)
if err != nil {
return res, err
}
res.Dump = string(dumpBytes)
return res, err
}
func (lg *Logic) RestoreDump(ctx context.Context, accountID int64, params *mbctl.RestoreDumpParams) (*mbctl.RestoreDumpResult, error) {
var err error
res := &mbctl.RestoreDumpResult{}
grantExists, _, err := lg.db.GetGrant(ctx, accountID, descriptor.GrantModifyDatabase)
if err != nil {
return res, err
}
if !grantExists {
err := fmt.Errorf("Operation not allowed for the user")
return res, err
}
lg.WaitDumping()
lg.WaitRestoring()
var dump descriptor.Dump
err = yaml.Unmarshal([]byte(params.Dump), &dump)
if err != nil {
return res, err
}
lg.RestoringSemUp()
defer lg.RestoringSemDown()
if params.DeleteAllRecords {
err = lg.db.CleanDatabase(ctx)
if err != nil {
return res, err
}
}
for _, account := range dump.Accounts {
lg.log.Infof("Insert account %s", account.Username)
err = lg.db.InsertAccount(ctx, &account)
if err != nil {
lg.log.Errorf("Insert account error: %v", err)
}
}
for _, grant := range dump.Grants {
lg.log.Infof("Insert grant %s for account %d", grant.Operation, grant.AccountID)
err = lg.db.InsertGrant(ctx, &grant)
if err != nil {
lg.log.Errorf("Insert account error: %v", err)
}
}
return res, err
}