2023-09-26 19:07:14 +08:00
|
|
|
package sqlite
|
2023-09-26 17:16:58 +08:00
|
|
|
|
|
|
|
import (
|
2023-09-27 09:27:31 +08:00
|
|
|
"context"
|
2023-09-26 17:16:58 +08:00
|
|
|
"database/sql"
|
2023-10-16 21:07:21 +08:00
|
|
|
"os"
|
2023-09-26 17:16:58 +08:00
|
|
|
|
2023-09-27 09:27:31 +08:00
|
|
|
"github.com/pkg/errors"
|
2023-10-16 21:07:21 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-12-19 22:34:06 +08:00
|
|
|
|
|
|
|
// Import the SQLite driver.
|
|
|
|
_ "modernc.org/sqlite"
|
2023-09-27 09:27:31 +08:00
|
|
|
|
2023-09-27 11:56:20 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-09-26 17:16:58 +08:00
|
|
|
"github.com/usememos/memos/store"
|
|
|
|
)
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
type DB struct {
|
2023-09-27 11:56:20 +08:00
|
|
|
db *sql.DB
|
|
|
|
profile *profile.Profile
|
2023-09-26 17:16:58 +08:00
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
// NewDB opens a database specified by its database driver name and a
|
2023-09-27 11:56:20 +08:00
|
|
|
// driver-specific data source name, usually consisting of at least a
|
|
|
|
// database name and connection information.
|
2023-10-05 23:11:29 +08:00
|
|
|
func NewDB(profile *profile.Profile) (store.Driver, error) {
|
2023-09-27 11:56:20 +08:00
|
|
|
// Ensure a DSN is set before attempting to open the database.
|
|
|
|
if profile.DSN == "" {
|
|
|
|
return nil, errors.New("dsn required")
|
2023-09-27 00:51:16 +08:00
|
|
|
}
|
2023-09-27 11:56:20 +08:00
|
|
|
|
|
|
|
// Connect to the database with some sane settings:
|
|
|
|
// - No shared-cache: it's obsolete; WAL journal mode is a better solution.
|
|
|
|
// - No foreign key constraints: it's currently disabled by default, but it's a
|
|
|
|
// good practice to be explicit and prevent future surprises on SQLite upgrades.
|
|
|
|
// - Journal mode set to WAL: it's the recommended journal mode for most applications
|
|
|
|
// as it prevents locking issues.
|
|
|
|
//
|
|
|
|
// Notes:
|
|
|
|
// - When using the `modernc.org/sqlite` driver, each pragma must be prefixed with `_pragma=`.
|
|
|
|
//
|
|
|
|
// References:
|
|
|
|
// - https://pkg.go.dev/modernc.org/sqlite#Driver.Open
|
|
|
|
// - https://www.sqlite.org/sharedcache.html
|
|
|
|
// - https://www.sqlite.org/pragma.html
|
|
|
|
sqliteDB, err := sql.Open("sqlite", profile.DSN+"?_pragma=foreign_keys(0)&_pragma=busy_timeout(10000)&_pragma=journal_mode(WAL)")
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to open db with dsn: %s", profile.DSN)
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
driver := DB{db: sqliteDB, profile: profile}
|
2023-09-27 11:56:20 +08:00
|
|
|
|
|
|
|
return &driver, nil
|
2023-09-26 17:16:58 +08:00
|
|
|
}
|
2023-09-27 09:27:31 +08:00
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) GetDB() *sql.DB {
|
2023-09-29 09:15:54 +08:00
|
|
|
return d.db
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) Vacuum(ctx context.Context) error {
|
2023-09-27 09:27:31 +08:00
|
|
|
tx, err := d.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
if err := vacuumImpl(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vacuum sqlite database file size after deleting resource.
|
|
|
|
if _, err := d.db.Exec("VACUUM"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func vacuumImpl(ctx context.Context, tx *sql.Tx) error {
|
|
|
|
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 := vacuumMemoRelations(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-11 21:29:22 +08:00
|
|
|
if err := vacuumInbox(ctx, tx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-27 09:27:31 +08:00
|
|
|
if err := vacuumTag(ctx, tx); err != nil {
|
|
|
|
// Prevent revive warning.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-16 21:07:21 +08:00
|
|
|
func (d *DB) GetCurrentDBSize(context.Context) (int64, error) {
|
|
|
|
fi, err := os.Stat(d.profile.DSN)
|
|
|
|
if err != nil {
|
|
|
|
return 0, status.Errorf(codes.Internal, "failed to get file info: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fi.Size(), nil
|
|
|
|
}
|
|
|
|
|
2023-10-05 23:11:29 +08:00
|
|
|
func (d *DB) Close() error {
|
2023-09-27 09:27:31 +08:00
|
|
|
return d.db.Close()
|
|
|
|
}
|