-
Notifications
You must be signed in to change notification settings - Fork 676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MF-739 - Add ID to the User entity #1152
Changes from 4 commits
4dd6bab
c8ed990
c2e4ccf
d962fbc
5bc64b9
e0fa715
64a79cc
746d4ff
f6e9002
4cb7327
1f2a2af
8af0e06
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright (c) Mainflux | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package mainflux | ||
|
||
// IdentityProvider specifies an API for generating unique identifiers. | ||
type IdentityProvider interface { | ||
// ID generates the unique identifier. | ||
ID() (string, error) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,9 +49,8 @@ func migrateDB(db *sqlx.DB) error { | |
Id: "users_1", | ||
Up: []string{ | ||
`CREATE TABLE IF NOT EXISTS users ( | ||
email VARCHAR(254) PRIMARY KEY, | ||
password CHAR(60) NOT NULL | ||
)`, | ||
email VARCHAR(254) PRIMARY KEY, | ||
password CHAR(60) NOT NULL)`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move closing brackets in the next line, to mark clearly the block closing. |
||
}, | ||
Down: []string{"DROP TABLE users"}, | ||
}, | ||
|
@@ -61,6 +60,14 @@ func migrateDB(db *sqlx.DB) error { | |
`ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS metadata JSONB`, | ||
}, | ||
}, | ||
{ | ||
Id: "users_3", | ||
Up: []string{ | ||
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; | ||
ALTER TABLE IF EXISTS users ADD COLUMN IF NOT EXISTS | ||
id UUID DEFAULT uuid_generate_v4()`, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ func New(db Database) users.UserRepository { | |
} | ||
|
||
func (ur userRepository) Save(ctx context.Context, user users.User) error { | ||
q := `INSERT INTO users (email, password, metadata) VALUES (:email, :password, :metadata)` | ||
q := `INSERT INTO users (id, email, password, metadata) VALUES (:id, :email, :password, :metadata)` | ||
|
||
dbu := toDBUser(user) | ||
if _, err := ur.db.NamedExecContext(ctx, q, dbu); err != nil { | ||
|
@@ -71,7 +71,7 @@ func (ur userRepository) UpdateUser(ctx context.Context, user users.User) error | |
} | ||
|
||
func (ur userRepository) RetrieveByID(ctx context.Context, email string) (users.User, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If its There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nmarcetic because in Users service we use email as unique ID. I can maybe rename the function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea because we didn't have |
||
q := `SELECT password, metadata FROM users WHERE email = $1` | ||
q := `SELECT id, password, metadata FROM users WHERE email = $1` | ||
|
||
dbu := dbUser{ | ||
Email: email, | ||
|
@@ -121,7 +121,6 @@ func (m *dbMetadata) Scan(value interface{}) error { | |
} | ||
|
||
if err := json.Unmarshal(b, m); err != nil { | ||
m = &dbMetadata{} | ||
return err | ||
} | ||
|
||
|
@@ -142,13 +141,15 @@ func (m dbMetadata) Value() (driver.Value, error) { | |
} | ||
|
||
type dbUser struct { | ||
ID string `db:"id"` | ||
Email string `db:"email"` | ||
Password string `db:"password"` | ||
Metadata dbMetadata `db:"metadata"` | ||
} | ||
|
||
func toDBUser(u users.User) dbUser { | ||
return dbUser{ | ||
ID: u.ID, | ||
Email: u.Email, | ||
Password: u.Password, | ||
Metadata: u.Metadata, | ||
|
@@ -157,6 +158,7 @@ func toDBUser(u users.User) dbUser { | |
|
||
func toUser(dbu dbUser) users.User { | ||
return users.User{ | ||
ID: dbu.ID, | ||
Email: dbu.Email, | ||
Password: dbu.Password, | ||
Metadata: dbu.Metadata, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Mainflux | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we call this top leve package I do not know, does not bother me much, but I'd like to her form @mainflux/maintainers There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not an Identity provider. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we should rename it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make more sense to me @manuio or put it under |
||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Package uuid provides a UUID identity provider. | ||
package uuid | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"github.com/mainflux/mainflux" | ||
"github.com/mainflux/mainflux/errors" | ||
) | ||
|
||
// ErrGeneratingID indicates error in generating UUID | ||
var ErrGeneratingID = errors.New("generating id failed") | ||
|
||
var _ mainflux.IdentityProvider = (*uuidIdentityProvider)(nil) | ||
|
||
type uuidIdentityProvider struct{} | ||
|
||
// New instantiates a UUID identity provider. | ||
func New() mainflux.IdentityProvider { | ||
return &uuidIdentityProvider{} | ||
} | ||
|
||
func (idp *uuidIdentityProvider) ID() (string, error) { | ||
id, err := uuid.NewV4() | ||
if err != nil { | ||
return "", errors.Wrap(ErrGeneratingID, err) | ||
} | ||
|
||
return id.String(), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda liked that this comma was in the new line. I am not saying that this is good solution, but I think it would be worth to check best practices when writing SQL strings in the code...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would follow the standard which is to don't add a new line. We are also following this standard in other services.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@manuio I agree, I just suggested to check where this standard commens and if it widely accepted, not something just invented by us. How other big Go projects handle this problem?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think that there are 2 versions:
or
And I like the one that we are using
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was expecting some research links... And my remark was about comma (
,
) at the end of the statement, not backticks.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@drasko the
,
still thereThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it, but it is not in the new line. So this is what I am trying to figure out - should it be in a new line or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case it's not a good proposition since it doesn't build, Lines are closed with
,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did it build before then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the `,