Skip to content
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

Closed
weloe opened this issue Apr 12, 2023 · 6 comments · Fixed by #208
Closed

why not use Transaction to SavePolicy #207

weloe opened this issue Apr 12, 2023 · 6 comments · Fixed by #208
Assignees

Comments

@weloe
Copy link
Contributor

weloe commented Apr 12, 2023

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 lost

// SavePolicy saves policy to database.
func (a *Adapter) SavePolicy(model model.Model) error {
	err := a.truncateTable()
	if err != nil {
		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 := a.db.Create(&lines).Error; err != nil {
					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 := a.db.Create(&lines).Error; err != nil {
					return err
				}
				lines = nil
			}
		}
	}
	if len(lines) > 0 {
		if err := a.db.Create(&lines).Error; err != nil {
			return err
		}
	}

	return nil
}
@casbin-bot
Copy link
Member

@weloe
Copy link
Contributor Author

weloe commented Apr 12, 2023

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
}

@hsluoyz
Copy link
Member

hsluoyz commented Apr 12, 2023

@weloe good idea. Can you make a PR to add the transaction?

@weloe
Copy link
Contributor Author

weloe commented Apr 12, 2023

@weloe good idea. Can you make a PR to add the transaction?

yes, I'll try

@weloe
Copy link
Contributor Author

weloe commented Apr 12, 2023

@weloe good idea. Can you make a PR to add the transaction?

pr #208

@shaolei
Copy link

shaolei commented Apr 13, 2023

也许这个能解决之前一些权限莫名其妙失效的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants