fix(server): skip initing users if inited
This commit is contained in:
parent
4f6683de03
commit
61757aff4a
4 changed files with 37 additions and 22 deletions
|
@ -95,7 +95,7 @@ func (st *SQLiteStore) InitUserTable(ctx context.Context, rootName, rootPwd stri
|
|||
|
||||
_, err = st.db.ExecContext(
|
||||
ctx,
|
||||
`create index i_user_name on t_user (name)`,
|
||||
`create index if not exists i_user_name on t_user (name)`,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -157,7 +157,7 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
|
|||
|
||||
_, err = st.db.ExecContext(
|
||||
ctx,
|
||||
`create index t_file_share on t_file_info (share_id, location)`,
|
||||
`create index if not exists t_file_share on t_file_info (share_id, location)`,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -180,7 +180,7 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
|
|||
|
||||
_, err = st.db.ExecContext(
|
||||
ctx,
|
||||
`create index t_file_uploading_path on t_file_uploading (real_path, user)`,
|
||||
`create index if not exists t_file_uploading_path on t_file_uploading (real_path, user)`,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -188,7 +188,7 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
|
|||
|
||||
_, err = st.db.ExecContext(
|
||||
ctx,
|
||||
`create index t_file_uploading_user on t_file_uploading (user)`,
|
||||
`create index if not exists t_file_uploading_user on t_file_uploading (user)`,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -170,6 +170,18 @@ func (h *MultiUsersSvc) Init(ctx context.Context, adminName string) (string, err
|
|||
return "", fmt.Errorf("predefined user is invalid: %s", err)
|
||||
}
|
||||
for _, userCfg := range userCfgs {
|
||||
_, err := h.deps.Users().GetUserByName(ctx, userCfg.Name)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrUserNotFound) {
|
||||
// no op, need initing
|
||||
} else {
|
||||
return "", err
|
||||
}
|
||||
} else {
|
||||
h.deps.Log().Warn("warning: users exists, skip initing(%s)", userCfg.Name)
|
||||
continue
|
||||
}
|
||||
|
||||
// TODO: following operations must be atomic
|
||||
// TODO: check if the folders already exists
|
||||
fsRootFolder := q.FsRootPath(userCfg.Name, "/")
|
||||
|
|
|
@ -179,7 +179,17 @@ func initDB(cfg gocfg.ICfg, filesystem fs.ISimpleFS) (db.IDBQuickshare, string,
|
|||
return nil, "", fmt.Errorf("failed to create path for db: %w", err)
|
||||
}
|
||||
|
||||
sqliteDB, err := sqlite.NewSQLite(dbPath)
|
||||
inited := true
|
||||
_, err = filesystem.Stat(dbPath)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
inited = false
|
||||
} else {
|
||||
return nil, "", fmt.Errorf("failed to stat db: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
sqliteDB, err := sqlite.NewSQLite(path.Join(filesystem.Root(), dbPath))
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to create path for db: %w", err)
|
||||
}
|
||||
|
@ -191,7 +201,7 @@ func initDB(cfg gocfg.ICfg, filesystem fs.ISimpleFS) (db.IDBQuickshare, string,
|
|||
var ok bool
|
||||
var adminName string
|
||||
var pwdHash []byte
|
||||
if cfg.BoolOr("Users.EnableAuth", true) {
|
||||
if !inited && cfg.BoolOr("Users.EnableAuth", true) {
|
||||
adminName, ok = cfg.String("ENV.DEFAULTADMIN")
|
||||
if !ok || adminName == "" {
|
||||
fmt.Println("Please input admin name: ")
|
||||
|
@ -213,13 +223,8 @@ func initDB(cfg gocfg.ICfg, filesystem fs.ISimpleFS) (db.IDBQuickshare, string,
|
|||
}
|
||||
}
|
||||
|
||||
err = dbQuickshare.InitUserTable(context.TODO(), adminName, string(pwdHash))
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to init user table: %w", err)
|
||||
}
|
||||
err = dbQuickshare.InitConfigTable(
|
||||
context.TODO(),
|
||||
&db.SiteConfig{
|
||||
if !inited {
|
||||
siteCfg := &db.SiteConfig{
|
||||
ClientCfg: &db.ClientConfig{
|
||||
SiteName: cfg.StringOr("Site.ClientCfg.SiteName", "Quickshare"),
|
||||
SiteDesc: cfg.StringOr("Site.ClientCfg.SiteDesc", "Quick and simple file sharing"),
|
||||
|
@ -231,15 +236,13 @@ func initDB(cfg gocfg.ICfg, filesystem fs.ISimpleFS) (db.IDBQuickshare, string,
|
|||
BgColor: cfg.StringOr("Site.ClientCfg.Bg.BgColor", ""),
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to init config table: %w", err)
|
||||
}
|
||||
err = dbQuickshare.InitFileTables(context.TODO())
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to init files tables: %w", err)
|
||||
}
|
||||
err = dbQuickshare.Init(context.TODO(), adminName, string(pwdHash), siteCfg)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("failed to init tables: %w %s", err, dbPath)
|
||||
}
|
||||
}
|
||||
|
||||
return dbQuickshare, adminName, nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue