2022-05-16 02:37:23 +03:00
|
|
|
package store
|
|
|
|
|
2022-05-21 19:59:22 +03:00
|
|
|
import (
|
2022-11-06 07:21:58 +03:00
|
|
|
"context"
|
2022-05-21 19:59:22 +03:00
|
|
|
"database/sql"
|
2023-02-11 09:19:26 +03:00
|
|
|
"sync"
|
2022-06-27 17:09:06 +03:00
|
|
|
|
2023-07-14 15:05:07 +03:00
|
|
|
"modernc.org/sqlite"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
"github.com/usememos/memos/server/profile"
|
2022-05-21 19:59:22 +03:00
|
|
|
)
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
// Store provides database access to all raw objects.
|
2022-05-16 02:37:23 +03:00
|
|
|
type Store struct {
|
2023-05-09 04:02:59 +03:00
|
|
|
Profile *profile.Profile
|
|
|
|
db *sql.DB
|
2023-09-26 12:16:58 +03:00
|
|
|
driver Driver
|
2023-07-02 13:56:25 +03:00
|
|
|
systemSettingCache sync.Map // map[string]*SystemSetting
|
|
|
|
userCache sync.Map // map[int]*User
|
2023-06-29 17:55:03 +03:00
|
|
|
userSettingCache sync.Map // map[string]*UserSetting
|
2023-06-26 18:46:01 +03:00
|
|
|
idpCache sync.Map // map[int]*IdentityProvider
|
2022-05-16 02:37:23 +03:00
|
|
|
}
|
|
|
|
|
2022-08-24 16:53:12 +03:00
|
|
|
// New creates a new instance of Store.
|
2023-09-26 12:16:58 +03:00
|
|
|
func New(db *sql.DB, driver Driver, profile *profile.Profile) *Store {
|
2022-05-16 02:37:23 +03:00
|
|
|
return &Store{
|
2023-05-09 04:02:59 +03:00
|
|
|
Profile: profile,
|
2022-05-21 19:59:22 +03:00
|
|
|
db: db,
|
2023-09-26 12:16:58 +03:00
|
|
|
driver: driver,
|
2022-05-16 02:37:23 +03:00
|
|
|
}
|
|
|
|
}
|
2022-11-06 07:21:58 +03:00
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
func (s *Store) GetDB() *sql.DB {
|
|
|
|
return s.db
|
|
|
|
}
|
|
|
|
|
2023-07-14 15:05:07 +03:00
|
|
|
func (s *Store) BackupTo(ctx context.Context, filename string) error {
|
|
|
|
conn, err := s.db.Conn(ctx)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Errorf("fail to get conn %s", err)
|
2023-07-14 15:05:07 +03:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
err = conn.Raw(func(driverConn any) error {
|
|
|
|
type backuper interface {
|
|
|
|
NewBackup(string) (*sqlite.Backup, error)
|
|
|
|
}
|
|
|
|
backupConn, ok := driverConn.(backuper)
|
|
|
|
if !ok {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Errorf("db connection is not a sqlite backuper")
|
2023-07-14 15:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bck, err := backupConn.NewBackup(filename)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Errorf("fail to create sqlite backup %s", err)
|
2023-07-14 15:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for more := true; more; {
|
|
|
|
more, err = bck.Step(-1)
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Errorf("fail to execute sqlite backup %s", err)
|
2023-07-14 15:05:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bck.Finish()
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-09-17 17:55:13 +03:00
|
|
|
return errors.Errorf("fail to backup %s", err)
|
2023-07-14 15:05:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-06 07:21:58 +03:00
|
|
|
func (s *Store) Vacuum(ctx context.Context) error {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return err
|
2022-11-06 07:21:58 +03:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2023-05-09 04:02:59 +03:00
|
|
|
if err := s.vacuumImpl(ctx, tx); err != nil {
|
2022-11-06 07:21:58 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
2023-07-02 13:56:25 +03:00
|
|
|
return err
|
2022-11-06 07:21:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vacuum sqlite database file size after deleting resource.
|
|
|
|
if _, err := s.db.Exec("VACUUM"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-20 05:16:19 +03:00
|
|
|
func (*Store) vacuumImpl(ctx context.Context, tx *sql.Tx) error {
|
2022-11-06 07:21:58 +03:00
|
|
|
if err := vacuumMemo(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vacuumResource(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vacuumUserSetting(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vacuumMemoOrganizer(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := vacuumMemoResource(ctx, tx); err != nil {
|
2022-12-21 14:22:32 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-05-20 05:16:19 +03:00
|
|
|
if err := vacuumMemoRelations(ctx, tx); err != nil {
|
|
|
|
return err
|
2023-04-25 18:27:38 +03:00
|
|
|
}
|
2022-12-21 14:22:32 +03:00
|
|
|
if err := vacuumTag(ctx, tx); err != nil {
|
2022-11-06 07:21:58 +03:00
|
|
|
// Prevent revive warning.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|