mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-17 16:24:25 +08:00
fix the Postgres database transaction cannot continue to execute after failure (#50)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user