sqlite.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package sqlite
  2. import (
  3. "context"
  4. "database/sql"
  5. "os"
  6. "github.com/pkg/errors"
  7. "google.golang.org/grpc/codes"
  8. "google.golang.org/grpc/status"
  9. "modernc.org/sqlite"
  10. "github.com/usememos/memos/server/profile"
  11. "github.com/usememos/memos/store"
  12. )
  13. type DB struct {
  14. db *sql.DB
  15. profile *profile.Profile
  16. }
  17. // NewDB opens a database specified by its database driver name and a
  18. // driver-specific data source name, usually consisting of at least a
  19. // database name and connection information.
  20. func NewDB(profile *profile.Profile) (store.Driver, error) {
  21. // Ensure a DSN is set before attempting to open the database.
  22. if profile.DSN == "" {
  23. return nil, errors.New("dsn required")
  24. }
  25. // Connect to the database with some sane settings:
  26. // - No shared-cache: it's obsolete; WAL journal mode is a better solution.
  27. // - No foreign key constraints: it's currently disabled by default, but it's a
  28. // good practice to be explicit and prevent future surprises on SQLite upgrades.
  29. // - Journal mode set to WAL: it's the recommended journal mode for most applications
  30. // as it prevents locking issues.
  31. //
  32. // Notes:
  33. // - When using the `modernc.org/sqlite` driver, each pragma must be prefixed with `_pragma=`.
  34. //
  35. // References:
  36. // - https://pkg.go.dev/modernc.org/sqlite#Driver.Open
  37. // - https://www.sqlite.org/sharedcache.html
  38. // - https://www.sqlite.org/pragma.html
  39. sqliteDB, err := sql.Open("sqlite", profile.DSN+"?_pragma=foreign_keys(0)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)")
  40. if err != nil {
  41. return nil, errors.Wrapf(err, "failed to open db with dsn: %s", profile.DSN)
  42. }
  43. driver := DB{db: sqliteDB, profile: profile}
  44. return &driver, nil
  45. }
  46. func (d *DB) GetDB() *sql.DB {
  47. return d.db
  48. }
  49. func (d *DB) Vacuum(ctx context.Context) error {
  50. tx, err := d.db.BeginTx(ctx, nil)
  51. if err != nil {
  52. return err
  53. }
  54. defer tx.Rollback()
  55. if err := vacuumImpl(ctx, tx); err != nil {
  56. return err
  57. }
  58. if err := tx.Commit(); err != nil {
  59. return err
  60. }
  61. // Vacuum sqlite database file size after deleting resource.
  62. if _, err := d.db.Exec("VACUUM"); err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. func vacuumImpl(ctx context.Context, tx *sql.Tx) error {
  68. if err := vacuumMemo(ctx, tx); err != nil {
  69. return err
  70. }
  71. if err := vacuumResource(ctx, tx); err != nil {
  72. return err
  73. }
  74. if err := vacuumUserSetting(ctx, tx); err != nil {
  75. return err
  76. }
  77. if err := vacuumMemoOrganizer(ctx, tx); err != nil {
  78. return err
  79. }
  80. if err := vacuumMemoRelations(ctx, tx); err != nil {
  81. return err
  82. }
  83. if err := vacuumTag(ctx, tx); err != nil {
  84. // Prevent revive warning.
  85. return err
  86. }
  87. return nil
  88. }
  89. func (d *DB) BackupTo(ctx context.Context, filename string) error {
  90. conn, err := d.db.Conn(ctx)
  91. if err != nil {
  92. return errors.Wrap(err, "fail to open new connection")
  93. }
  94. defer conn.Close()
  95. err = conn.Raw(func(driverConn any) error {
  96. type backuper interface {
  97. NewBackup(string) (*sqlite.Backup, error)
  98. }
  99. backupConn, ok := driverConn.(backuper)
  100. if !ok {
  101. return errors.New("db connection is not a sqlite backuper")
  102. }
  103. bck, err := backupConn.NewBackup(filename)
  104. if err != nil {
  105. return errors.Wrap(err, "fail to create sqlite backup")
  106. }
  107. for more := true; more; {
  108. more, err = bck.Step(-1)
  109. if err != nil {
  110. return errors.Wrap(err, "fail to execute sqlite backup")
  111. }
  112. }
  113. return bck.Finish()
  114. })
  115. if err != nil {
  116. return errors.Wrap(err, "fail to backup")
  117. }
  118. return nil
  119. }
  120. func (d *DB) GetCurrentDBSize(context.Context) (int64, error) {
  121. fi, err := os.Stat(d.profile.DSN)
  122. if err != nil {
  123. return 0, status.Errorf(codes.Internal, "failed to get file info: %v", err)
  124. }
  125. return fi.Size(), nil
  126. }
  127. func (d *DB) Close() error {
  128. return d.db.Close()
  129. }