From ad512dd6750170765e08f1762ade6a1c54a703c4 Mon Sep 17 00:00:00 2001 From: "Chang, Hui-Tang" Date: Fri, 22 Sep 2023 15:19:54 +0800 Subject: [PATCH] fix(init): fix default user creation bug --- cmd/init/main.go | 13 ++++--------- pkg/db/migration/000002_init.up.sql | 2 ++ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/cmd/init/main.go b/cmd/init/main.go index 98ee1a1..d9d3fb8 100644 --- a/cmd/init/main.go +++ b/cmd/init/main.go @@ -63,25 +63,20 @@ func createDefaultUser(ctx context.Context, db *gorm.DB) error { CookieToken: sql.NullString{String: "", Valid: false}, } - user, err := r.GetUser(defaultUserUID) + user, err := r.GetUserByID(constant.DefaultUserID) // Default user already exists if err == nil { - user.ID = constant.DefaultUserID - passwordHash, _, err := r.GetUserPasswordHash(ctx, defaultUserUID) + passwordHash, _, err := r.GetUserPasswordHash(ctx, user.UID) if err != nil { return err } if passwordHash == "" { - err = r.UpdateUserPasswordHash(ctx, defaultUserUID, string(passwordBytes), time.Now()) + err = r.UpdateUserPasswordHash(ctx, user.UID, string(passwordBytes), time.Now()) if err != nil { return err } } - err = r.UpdateUser(ctx, defaultUserUID, user) - if err != nil { - return err - } return nil } @@ -94,7 +89,7 @@ func createDefaultUser(ctx context.Context, db *gorm.DB) error { if err != nil { return err } - err = r.UpdateUserPasswordHash(ctx, defaultUserUID, string(passwordBytes), time.Now()) + err = r.UpdateUserPasswordHash(ctx, defaultUser.UID, string(passwordBytes), time.Now()) if err != nil { return err } diff --git a/pkg/db/migration/000002_init.up.sql b/pkg/db/migration/000002_init.up.sql index 5afe50e..cd3aca7 100644 --- a/pkg/db/migration/000002_init.up.sql +++ b/pkg/db/migration/000002_init.up.sql @@ -23,4 +23,6 @@ CREATE TABLE IF NOT EXISTS public.token( ); CREATE UNIQUE INDEX unique_owner_id_delete_time ON public.token (owner, id); +UPDATE public.user SET id = 'admin' WHERE id = 'instill-ai'; + COMMIT;