Skip to content

Commit

Permalink
feat(datamodel): add profile_avatar and profile_data fields (#144)
Browse files Browse the repository at this point in the history
Because

- we need to store the user avatar and profile data

This commit

- add profile avatar and data fields
  • Loading branch information
donch1989 authored Nov 29, 2023
1 parent 343040b commit fa8a18a
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 64 deletions.
2 changes: 1 addition & 1 deletion config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ database:
host: pg-sql
port: 5432
name: mgmt
version: 3
version: 4
timezone: Etc/UTC
pool:
idleconnections: 5
Expand Down
19 changes: 8 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0
github.com/iancoleman/strcase v0.2.0
github.com/influxdata/influxdb-client-go/v2 v2.12.3
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20231129081700-eed8b0ad53ba
github.com/instill-ai/protogen-go v0.3.3-alpha.0.20231129095217-f8d4e5951d35
github.com/instill-ai/usage-client v0.2.4-alpha.0.20231019203021-70410a0a8061
github.com/instill-ai/x v0.3.0-alpha
github.com/jackc/pgx/v5 v5.2.0
github.com/jackc/pgx/v5 v5.3.0
github.com/knadh/koanf v1.4.4
github.com/mennanov/fieldmask-utils v0.5.0
github.com/openfga/go-sdk v0.2.3
Expand All @@ -39,8 +39,9 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc
google.golang.org/grpc v1.56.0
google.golang.org/protobuf v1.30.0
gorm.io/driver/postgres v1.4.5
gorm.io/gorm v1.24.2
gorm.io/datatypes v1.2.0
gorm.io/driver/postgres v1.5.0
gorm.io/gorm v1.24.7-0.20230306060331-85eaf9eeda11
)

require (
Expand All @@ -58,6 +59,7 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -67,14 +69,8 @@ require (
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/influxdata/line-protocol/v2 v2.2.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.13.0 // indirect
github.com/jackc/pgx/v4 v4.17.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
Expand Down Expand Up @@ -106,4 +102,5 @@ require (
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/mysql v1.4.7 // indirect
)
91 changes: 47 additions & 44 deletions go.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions pkg/datamodel/datamodel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gofrs/uuid"
"gorm.io/datatypes"
"gorm.io/gorm"

mgmtPB "github.com/instill-ai/protogen-go/core/mgmt/v1alpha"
Expand Down Expand Up @@ -49,6 +50,8 @@ type Owner struct {
Role sql.NullString
NewsletterSubscription bool `gorm:"default:false"`
CookieToken sql.NullString
ProfileAvatar sql.NullString
ProfileData datatypes.JSON `gorm:"type:jsonb"`
}

type Password struct {
Expand Down
1 change: 0 additions & 1 deletion pkg/db/migration/000003_init.down.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
BEGIN;
DROP TABLE IF EXISTS public.user;
COMMIT;
2 changes: 2 additions & 0 deletions pkg/db/migration/000004_init.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BEGIN;
COMMIT;
4 changes: 4 additions & 0 deletions pkg/db/migration/000004_init.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BEGIN;
ALTER TABLE public.owner ADD COLUMN "profile_avatar" TEXT DEFAULT NULL;
ALTER TABLE public.owner ADD COLUMN "profile_data" JSONB DEFAULT '{}';
COMMIT;
63 changes: 56 additions & 7 deletions pkg/service/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/gofrs/uuid"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/instill-ai/mgmt-backend/pkg/datamodel"
Expand Down Expand Up @@ -54,6 +55,15 @@ func (s *service) DBUser2PBUser(ctx context.Context, dbUser *datamodel.Owner) (*
Role: &dbUser.Role.String,
NewsletterSubscription: dbUser.NewsletterSubscription,
CookieToken: &dbUser.CookieToken.String,
ProfileAvatar: &dbUser.ProfileAvatar.String,
ProfileData: func() *structpb.Struct {
str := &structpb.Struct{}
err := str.UnmarshalJSON(dbUser.ProfileData)
if err != nil {
return &structpb.Struct{}
}
return str
}(),
}, nil
}

Expand All @@ -76,6 +86,7 @@ func (s *service) PBUser2DBUser(pbUser *mgmtPB.User) (*datamodel.Owner, error) {
orgName := pbUser.GetOrgName()
role := pbUser.GetRole()
cookieToken := pbUser.GetCookieToken()
profileAvatar := pbUser.GetProfileAvatar()

return &datamodel.Owner{
Base: datamodel.Base{
Expand Down Expand Up @@ -109,6 +120,20 @@ func (s *service) PBUser2DBUser(pbUser *mgmtPB.User) (*datamodel.Owner, error) {
String: cookieToken,
Valid: len(cookieToken) > 0,
},
ProfileAvatar: sql.NullString{
String: profileAvatar,
Valid: len(profileAvatar) > 0,
},
ProfileData: func() []byte {
if pbUser.GetProfileData() != nil {
b, err := pbUser.GetProfileData().MarshalJSON()
if err != nil {
return nil
}
return b
}
return []byte{}
}(),
}, nil
}

Expand Down Expand Up @@ -138,13 +163,22 @@ func (s *service) DBOrg2PBOrg(ctx context.Context, dbOrg *datamodel.Owner) (*mgm
uid := dbOrg.Base.UID.String()

return &mgmtPB.Organization{
Name: fmt.Sprintf("organizations/%s", id),
Uid: uid,
Id: id,
CreateTime: timestamppb.New(dbOrg.Base.CreateTime),
UpdateTime: timestamppb.New(dbOrg.Base.UpdateTime),
CustomerId: dbOrg.CustomerId,
OrgName: &dbOrg.OrgName.String,
Name: fmt.Sprintf("organizations/%s", id),
Uid: uid,
Id: id,
CreateTime: timestamppb.New(dbOrg.Base.CreateTime),
UpdateTime: timestamppb.New(dbOrg.Base.UpdateTime),
CustomerId: dbOrg.CustomerId,
OrgName: &dbOrg.OrgName.String,
ProfileAvatar: &dbOrg.ProfileAvatar.String,
ProfileData: func() *structpb.Struct {
str := &structpb.Struct{}
err := str.UnmarshalJSON(dbOrg.ProfileData)
if err != nil {
return &structpb.Struct{}
}
return str
}(),
}, nil
}

Expand All @@ -162,6 +196,7 @@ func (s *service) PBOrg2DBOrg(pbOrg *mgmtPB.Organization) (*datamodel.Owner, err
userType := "organization"
customerId := pbOrg.GetCustomerId()
orgName := pbOrg.GetOrgName()
profileAvatar := pbOrg.GetProfileAvatar()

return &datamodel.Owner{
Base: datamodel.Base{
Expand All @@ -177,6 +212,20 @@ func (s *service) PBOrg2DBOrg(pbOrg *mgmtPB.Organization) (*datamodel.Owner, err
String: orgName,
Valid: len(orgName) > 0,
},
ProfileAvatar: sql.NullString{
String: profileAvatar,
Valid: len(profileAvatar) > 0,
},
ProfileData: func() []byte {
if pbOrg.GetProfileData() != nil {
b, err := pbOrg.GetProfileData().MarshalJSON()
if err != nil {
return nil
}
return b
}
return []byte{}
}(),
}, nil
}

Expand Down

0 comments on commit fa8a18a

Please sign in to comment.