forked from cloudreve/Cloudreve
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: execute database script to calibrate user storage
- Loading branch information
Showing
5 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
Submodule assets
updated
4 files
+71 −61 | src/actions/explorer.js | |
+78 −40 | src/component/Download/DownloadingCard.js | |
+81 −70 | src/redux/explorer/action.ts | |
+33 −28 | src/utils/index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"context" | ||
"github.com/cloudreve/Cloudreve/v3/models/scripts" | ||
"github.com/cloudreve/Cloudreve/v3/pkg/util" | ||
) | ||
|
||
func RunScript(name string) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
if err := scripts.RunDBScript(name, ctx); err != nil { | ||
util.Log().Error("数据库脚本执行失败: %s", err) | ||
return | ||
} | ||
|
||
util.Log().Info("数据库脚本 [%s] 执行完毕", name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package scripts | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
) | ||
|
||
type DBScript interface { | ||
Run(ctx context.Context) | ||
} | ||
|
||
var availableScripts = make(map[string]DBScript) | ||
|
||
func RunDBScript(name string, ctx context.Context) error { | ||
if script, ok := availableScripts[name]; ok { | ||
script.Run(ctx) | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("数据库脚本 [%s] 不存在", name) | ||
} | ||
|
||
func register(name string, script DBScript) { | ||
availableScripts[name] = script | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package scripts | ||
|
||
import ( | ||
"context" | ||
model "github.com/cloudreve/Cloudreve/v3/models" | ||
"github.com/cloudreve/Cloudreve/v3/pkg/util" | ||
) | ||
|
||
type UserStorageCalibration int | ||
|
||
func init() { | ||
register("CalibrateUserStorage", UserStorageCalibration(0)) | ||
} | ||
|
||
type storageResult struct { | ||
Total uint64 | ||
} | ||
|
||
// Run 运行脚本校准所有用户容量 | ||
func (script UserStorageCalibration) Run(ctx context.Context) { | ||
// 列出所有用户 | ||
var res []model.User | ||
model.DB.Model(&model.User{}).Find(&res) | ||
|
||
// 逐个检查容量 | ||
for _, user := range res { | ||
// 计算正确的容量 | ||
var total storageResult | ||
model.DB.Model(&model.File{}).Where("user_id = ?", user.ID).Select("sum(size) as total").Scan(&total) | ||
// 更新用户的容量 | ||
if user.Storage != total.Total { | ||
util.Log().Info("将用户 [%s] 的容量由 %d 校准为 %d", user.Email, | ||
user.Storage, total.Total) | ||
model.DB.Model(&user).Update("storage", total.Total) | ||
} | ||
} | ||
} |