sqlite.go 3.3 KB

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