fix the Postgres database transaction cannot continue to execute after failure (#50)

This commit is contained in:
MaysWind
2025-02-17 00:32:53 +08:00
parent 8f55bd0df1
commit a9a37b0c97
4 changed files with 69 additions and 8 deletions
+23 -1
View File
@@ -4,11 +4,13 @@ import (
"xorm.io/xorm"
"github.com/mayswind/ezbookkeeping/pkg/core"
"github.com/mayswind/ezbookkeeping/pkg/settings"
)
// Database represents a database instance
type Database struct {
engineGroup *xorm.EngineGroup
databaseType string
engineGroup *xorm.EngineGroup
}
// NewSession starts a new session with the specified context
@@ -41,3 +43,23 @@ func (db *Database) DoTransaction(c core.Context, fn func(sess *xorm.Session) er
return nil
}
// SetSavePoint sets a save point in the current transaction for Postgres
func (db *Database) SetSavePoint(sess *xorm.Session, savePointName string) error {
if db.databaseType == settings.PostgresDbType {
_, err := sess.Exec("SAVEPOINT " + savePointName)
return err
}
return nil
}
// RollbackToSavePoint rolls back to the specified save point in the current transaction for Postgres
func (db *Database) RollbackToSavePoint(sess *xorm.Session, savePointName string) error {
if db.databaseType == settings.PostgresDbType {
_, err := sess.Exec("ROLLBACK TO SAVEPOINT " + savePointName)
return err
}
return nil
}