| 448 | } |
| 449 | |
| 450 | func syncDeleteBook(tx *database.DB, bookUUID string) error { |
| 451 | var localUSN int |
| 452 | var dirty bool |
| 453 | err := tx.QueryRow("SELECT usn, dirty FROM books WHERE uuid = ?", bookUUID).Scan(&localUSN, &dirty) |
| 454 | if err != nil && err != sql.ErrNoRows { |
| 455 | return errors.Wrapf(err, "getting local book %s", bookUUID) |
| 456 | } |
| 457 | |
| 458 | // if book does not exist on client, noop |
| 459 | if err == sql.ErrNoRows { |
| 460 | return nil |
| 461 | } |
| 462 | |
| 463 | // if local copy is dirty, noop. it will be uploaded to the server later |
| 464 | if dirty { |
| 465 | return nil |
| 466 | } |
| 467 | |
| 468 | ok, err := checkNotesPristine(tx, bookUUID) |
| 469 | if err != nil { |
| 470 | return errors.Wrap(err, "checking if any notes are dirty in book") |
| 471 | } |
| 472 | // if the local book is not pristine, do not delete but mark it as dirty |
| 473 | // so that it can be uploaded to the server later and become un-deleted |
| 474 | if !ok { |
| 475 | _, err = tx.Exec("UPDATE books SET dirty = ? WHERE uuid = ?", true, bookUUID) |
| 476 | if err != nil { |
| 477 | return errors.Wrapf(err, "marking a book dirty with uuid %s", bookUUID) |
| 478 | } |
| 479 | |
| 480 | return nil |
| 481 | } |
| 482 | |
| 483 | _, err = tx.Exec("DELETE FROM notes WHERE book_uuid = ?", bookUUID) |
| 484 | if err != nil { |
| 485 | return errors.Wrapf(err, "deleting local notes of the book %s", bookUUID) |
| 486 | } |
| 487 | |
| 488 | _, err = tx.Exec("DELETE FROM books WHERE uuid = ?", bookUUID) |
| 489 | if err != nil { |
| 490 | return errors.Wrapf(err, "deleting local book %s", bookUUID) |
| 491 | } |
| 492 | |
| 493 | return nil |
| 494 | } |
| 495 | |
| 496 | func fullSyncBook(tx *database.DB, b client.SyncFragBook) error { |
| 497 | var localUSN int |