mirror of
https://github.com/usememos/memos.git
synced 2024-11-28 14:23:15 +03:00
fix: storage setting changed don't take effect (#2385)
* fix: Storage setting changed don't take effect * fix: Storage setting changed don't take effect * fix: Storage setting changed don't take effect
This commit is contained in:
parent
c999d71455
commit
73f59eaf09
@ -2,7 +2,6 @@ package v2
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"google.golang.org/grpc/codes"
|
||||
@ -37,11 +36,11 @@ func (s *SystemService) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemI
|
||||
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
|
||||
}
|
||||
if currentUser != nil && currentUser.Role == store.RoleHost {
|
||||
fi, err := os.Stat(s.Profile.DSN)
|
||||
size, err := s.Store.GetCurrentDBSize(ctx)
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Internal, "failed to get file info: %v", err)
|
||||
return nil, status.Errorf(codes.Internal, "failed to get db size: %v", err)
|
||||
}
|
||||
defaultSystemInfo.DbSize = fi.Size()
|
||||
defaultSystemInfo.DbSize = size
|
||||
}
|
||||
|
||||
response := &apiv2pb.GetSystemInfoResponse{
|
||||
|
@ -5,8 +5,10 @@ import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/usememos/memos/common/log"
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
)
|
||||
@ -14,18 +16,28 @@ import (
|
||||
type DB struct {
|
||||
db *sql.DB
|
||||
profile *profile.Profile
|
||||
config *mysql.Config
|
||||
}
|
||||
|
||||
func NewDB(profile *profile.Profile) (store.Driver, error) {
|
||||
// Open MySQL connection with parameter.
|
||||
// multiStatements=true is required for migration.
|
||||
// See more in: https://github.com/go-sql-driver/mysql#multistatements
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s?multiStatements=true", profile.DSN))
|
||||
dsn := fmt.Sprintf("%s?multiStatements=true", profile.DSN)
|
||||
|
||||
var err error
|
||||
driver := DB{profile: profile}
|
||||
driver.config, err = mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("DSN parse error: %s", dsn))
|
||||
return nil, errors.New("Parse DSN eroor")
|
||||
}
|
||||
|
||||
driver.db, err = sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "failed to open db: %s", profile.DSN)
|
||||
}
|
||||
|
||||
driver := DB{db: db, profile: profile}
|
||||
return &driver, nil
|
||||
}
|
||||
|
||||
@ -67,6 +79,32 @@ func (*DB) BackupTo(context.Context, string) error {
|
||||
return errors.New("Please use mysqldump to backup")
|
||||
}
|
||||
|
||||
func (d *DB) GetCurrentDBSize(ctx context.Context) (int64, error) {
|
||||
query := "SELECT SUM(`data_length` + `index_length`) AS `size` " +
|
||||
" FROM information_schema.TABLES" +
|
||||
" WHERE `table_schema` = ?" +
|
||||
" GROUP BY `table_schema`"
|
||||
rows, err := d.db.QueryContext(ctx, query, d.config.DBName)
|
||||
if err != nil {
|
||||
log.Error("Query db size error, make sure you have enough privilege")
|
||||
return 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var size int64
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(&size); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
}
|
||||
|
||||
if rows.Err() != nil {
|
||||
return 0, rows.Err()
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
||||
|
@ -3,8 +3,11 @@ package sqlite
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"modernc.org/sqlite"
|
||||
|
||||
"github.com/usememos/memos/server/profile"
|
||||
@ -137,6 +140,15 @@ func (d *DB) BackupTo(ctx context.Context, filename string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (d *DB) Close() error {
|
||||
return d.db.Close()
|
||||
}
|
||||
|
@ -17,6 +17,9 @@ type Driver interface {
|
||||
Vacuum(ctx context.Context) error
|
||||
BackupTo(ctx context.Context, filename string) error
|
||||
|
||||
// current file is driver
|
||||
GetCurrentDBSize(ctx context.Context) (int64, error)
|
||||
|
||||
// Activity model related methods.
|
||||
CreateActivity(ctx context.Context, create *Activity) (*Activity, error)
|
||||
ListActivity(ctx context.Context, find *FindActivity) ([]*Activity, error)
|
||||
|
@ -36,3 +36,7 @@ func (s *Store) Vacuum(ctx context.Context) error {
|
||||
func (s *Store) Close() error {
|
||||
return s.driver.Close()
|
||||
}
|
||||
|
||||
func (s *Store) GetCurrentDBSize(ctx context.Context) (int64, error) {
|
||||
return s.driver.GetCurrentDBSize(ctx)
|
||||
}
|
||||
|
@ -32,7 +32,11 @@ const StorageSection = () => {
|
||||
name: "storage-service-id",
|
||||
value: JSON.stringify(storageId),
|
||||
});
|
||||
await globalStore.fetchSystemStatus();
|
||||
try {
|
||||
await globalStore.fetchSystemStatus();
|
||||
} catch (error: any) {
|
||||
console.error(error);
|
||||
}
|
||||
setStorageServiceId(storageId);
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user