sqlite.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // Import the SQLite driver.
  10. _ "modernc.org/sqlite"
  11. "github.com/usememos/memos/server/profile"
  12. "github.com/usememos/memos/store"
  13. )
  14. type DB struct {
  15. db *sql.DB
  16. profile *profile.Profile
  17. }
  18. // NewDB opens a database specified by its database driver name and a
  19. // driver-specific data source name, usually consisting of at least a
  20. // database name and connection information.
  21. func NewDB(profile *profile.Profile) (store.Driver, error) {
  22. // Ensure a DSN is set before attempting to open the database.
  23. if profile.DSN == "" {
  24. return nil, errors.New("dsn required")
  25. }
  26. // Connect to the database with some sane settings:
  27. // - No shared-cache: it's obsolete; WAL journal mode is a better solution.
  28. // - No foreign key constraints: it's currently disabled by default, but it's a
  29. // good practice to be explicit and prevent future surprises on SQLite upgrades.
  30. // - Journal mode set to WAL: it's the recommended journal mode for most applications
  31. // as it prevents locking issues.
  32. //
  33. // Notes:
  34. // - When using the `modernc.org/sqlite` driver, each pragma must be prefixed with `_pragma=`.
  35. //
  36. // References:
  37. // - https://pkg.go.dev/modernc.org/sqlite#Driver.Open
  38. // - https://www.sqlite.org/sharedcache.html
  39. // - https://www.sqlite.org/pragma.html
  40. sqliteDB, err := sql.Open("sqlite", profile.DSN+"?_pragma=foreign_keys(0)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)")
  41. if err != nil {
  42. return nil, errors.Wrapf(err, "failed to open db with dsn: %s", profile.DSN)
  43. }
  44. driver := DB{db: sqliteDB, profile: profile}
  45. return &driver, nil
  46. }
  47. func (d *DB) GetDB() *sql.DB {
  48. return d.db
  49. }
  50. func (d *DB) GetCurrentDBSize(context.Context) (int64, error) {
  51. fi, err := os.Stat(d.profile.DSN)
  52. if err != nil {
  53. return 0, status.Errorf(codes.Internal, "failed to get file info: %v", err)
  54. }
  55. return fi.Size(), nil
  56. }
  57. func (d *DB) Close() error {
  58. return d.db.Close()
  59. }