Download this file
| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "amadan.net/oficina/sheets/internal/sheets/seal" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | // InitializeTab writes initial cells inside the caller's creation transaction. |
| 11 | // Its handle is never registered or returned: it holds tx and must be discarded |
| 12 | // on either commit or rollback. The caller opens a canonical handle after commit. |
| 13 | func InitializeTab(tx *gorm.DB, sealer *seal.Sealer, tabID uint64, writes []CellWrite, actor uint64) error { |
| 14 | if tx == nil || tx.Statement == nil { |
| 15 | return errors.New("store: creation transaction required") |
| 16 | } |
| 17 | if _, ok := tx.Statement.ConnPool.(gorm.TxCommitter); !ok { |
| 18 | return errors.New("store: creation transaction required") |
| 19 | } |
| 20 | var tab Tab |
| 21 | if err := tx.Select("id", "content_version").First(&tab, tabID).Error; err != nil { |
| 22 | return err |
| 23 | } |
| 24 | var count int64 |
| 25 | if err := tx.Model(&Cell{}).Where("tab_id=?", tabID).Count(&count).Error; err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if tab.ContentVersion != 0 || count != 0 { |
| 29 | return errors.New("store: initial tab is not empty") |
| 30 | } |
| 31 | h, err := buildHandle(tx, sealer, tabID) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | if len(writes) == 0 { |
| 36 | return nil |
| 37 | } |
| 38 | _, err = h.SetBatch(writes, actor) |
| 39 | return err |
| 40 | } |
| 41 | |