-
-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
why not use Transaction to SavePolicy #207
Labels
Comments
like this // SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
var err error
tx := a.db.Begin()
if a.db.Config.Name() == sqlite.DriverName {
err = tx.Exec(fmt.Sprintf("delete from %s", a.getFullTableName())).Error
} else {
err = tx.Exec(fmt.Sprintf("truncate table %s", a.getFullTableName())).Error
}
if err != nil {
tx.Rollback()
return err
}
var lines []CasbinRule
flushEvery := 1000
for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
lines = append(lines, a.savePolicyLine(ptype, rule))
if len(lines) > flushEvery {
if err := tx.Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
lines = nil
}
}
}
for ptype, ast := range model["g"] {
for _, rule := range ast.Policy {
lines = append(lines, a.savePolicyLine(ptype, rule))
if len(lines) > flushEvery {
if err := tx.Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
lines = nil
}
}
}
if len(lines) > 0 {
if err := tx.Create(&lines).Error; err != nil {
tx.Rollback()
return err
}
}
tx.Commit()
return nil
} |
@weloe good idea. Can you make a PR to add the transaction? |
yes, I'll try |
也许这个能解决之前一些权限莫名其妙失效的问题 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a beginner i am a little confused,
SavePolicy
delete all data from table and add data after, If there is a erro in them, data can be lostThe text was updated successfully, but these errors were encountered: