make merge compatible with an existing transaction
This commit is contained in:
parent
9c8262b702
commit
99a3a1d091
1 changed files with 29 additions and 18 deletions
|
@ -4,32 +4,42 @@
|
||||||
{{- else -}}
|
{{- else -}}
|
||||||
{{- $dot := . }}
|
{{- $dot := . }}
|
||||||
// Merge combines two {{$tableNamePlural}} into one. The primary record will be kept, and the secondary will be deleted.
|
// Merge combines two {{$tableNamePlural}} into one. The primary record will be kept, and the secondary will be deleted.
|
||||||
func Merge{{$tableNamePlural}}(exec boil.Executor, primaryID uint64, secondaryID uint64) error {
|
func Merge{{$tableNamePlural}}(exec boil.Executor, primaryID uint64, secondaryID uint64) (err error) {
|
||||||
txdb, ok := exec.(boil.Beginner)
|
tx, ok := exec.(boil.Transactor)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("database does not support transactions")
|
txdb, ok := exec.(boil.Beginner)
|
||||||
}
|
if !ok {
|
||||||
|
return errors.New("database does not support transactions")
|
||||||
|
}
|
||||||
|
|
||||||
tx, txErr := txdb.Begin()
|
tx, err = txdb.Begin()
|
||||||
if txErr != nil {
|
if err != nil {
|
||||||
return txErr
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if p := recover(); p != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
panic(p) // Rollback, then propagate panic
|
||||||
|
} else if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
} else {
|
||||||
|
err = tx.Commit()
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
primary, err := Find{{$tableNameSingular}}(tx, primaryID)
|
primary, err := Find{{$tableNameSingular}}(tx, primaryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
} else if primary == nil {
|
||||||
if primary == nil {
|
|
||||||
return errors.New("Primary {{$tableNameSingular}} not found")
|
return errors.New("Primary {{$tableNameSingular}} not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
secondary, err := Find{{$tableNameSingular}}(tx, secondaryID)
|
secondary, err := Find{{$tableNameSingular}}(tx, secondaryID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
} else if secondary == nil {
|
||||||
if secondary == nil {
|
|
||||||
return errors.New("Secondary {{$tableNameSingular}} not found")
|
return errors.New("Secondary {{$tableNameSingular}} not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,9 +69,13 @@ func Merge{{$tableNamePlural}}(exec boil.Executor, primaryID uint64, secondaryID
|
||||||
{{- end }}
|
{{- end }}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = mergeModels(tx, primaryID, secondaryID, foreignKeys, conflictingKeys)
|
sqlTx, ok := tx.(*sql.Tx)
|
||||||
|
if !ok {
|
||||||
|
return errors.New("tx must be an sql.Tx")
|
||||||
|
}
|
||||||
|
|
||||||
|
err = mergeModels(sqlTx, primaryID, secondaryID, foreignKeys, conflictingKeys)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -80,17 +94,14 @@ func Merge{{$tableNamePlural}}(exec boil.Executor, primaryID uint64, secondaryID
|
||||||
|
|
||||||
err = primary.Update(tx)
|
err = primary.Update(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = secondary.Delete(tx)
|
err = secondary.Delete(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tx.Rollback()
|
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Commit()
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue