From 41b957f21bdee4fd4ff0a84d00f83b77c546e2cd Mon Sep 17 00:00:00 2001 From: gearnode Date: Tue, 4 Feb 2025 15:47:57 -0800 Subject: [PATCH] Update usrmgr schema Signed-off-by: gearnode --- .../coredata/migrations/20250202T110500Z.sql | 8 ++++---- .../coredata/migrations/20250202T141800Z.sql | 5 +++++ .../coredata/migrations/20250202T180400Z.sql | 1 + .../coredata/migrations/20250202T201400Z.sql | 1 + pkg/usrmgr/coredata/session.go | 3 +++ pkg/usrmgr/coredata/user.go | 15 +++++++++------ 6 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 pkg/usrmgr/coredata/migrations/20250202T141800Z.sql create mode 100644 pkg/usrmgr/coredata/migrations/20250202T180400Z.sql create mode 100644 pkg/usrmgr/coredata/migrations/20250202T201400Z.sql diff --git a/pkg/usrmgr/coredata/migrations/20250202T110500Z.sql b/pkg/usrmgr/coredata/migrations/20250202T110500Z.sql index b5dff7f25..7a59c3d8f 100644 --- a/pkg/usrmgr/coredata/migrations/20250202T110500Z.sql +++ b/pkg/usrmgr/coredata/migrations/20250202T110500Z.sql @@ -1,12 +1,12 @@ CREATE TABLE usrmgr_users ( id TEXT PRIMARY KEY, - created_at TIMESTAMP NOT NULL DEFAULT now(), - updated_at TIMESTAMP NOT NULL DEFAULT now() + created_at TIMESTAMP NOT NULL, + updated_at TIMESTAMP NOT NULL ); CREATE TABLE usrmgr_sessions ( id TEXT PRIMARY KEY, user_id TEXT NOT NULL REFERENCES usrmgr_users(id), - created_at TIMESTAMP NOT NULL DEFAULT now(), - updated_at TIMESTAMP NOT NULL DEFAULT now() + created_at TIMESTAMP NOT NULL, + updated_at TIMESTAMP NOT NULL ); diff --git a/pkg/usrmgr/coredata/migrations/20250202T141800Z.sql b/pkg/usrmgr/coredata/migrations/20250202T141800Z.sql new file mode 100644 index 000000000..65060835e --- /dev/null +++ b/pkg/usrmgr/coredata/migrations/20250202T141800Z.sql @@ -0,0 +1,5 @@ +CREATE EXTENSION citext; + +ALTER TABLE usrmgr_users + ADD COLUMN email_address CITEXT NOT NULL, + ADD COLUMN hashed_password BYTEA NOT NULL; diff --git a/pkg/usrmgr/coredata/migrations/20250202T180400Z.sql b/pkg/usrmgr/coredata/migrations/20250202T180400Z.sql new file mode 100644 index 000000000..e24c0c9f2 --- /dev/null +++ b/pkg/usrmgr/coredata/migrations/20250202T180400Z.sql @@ -0,0 +1 @@ +ALTER TABLE usrmgr_users ADD UNIQUE (email_address); diff --git a/pkg/usrmgr/coredata/migrations/20250202T201400Z.sql b/pkg/usrmgr/coredata/migrations/20250202T201400Z.sql new file mode 100644 index 000000000..ec1375b95 --- /dev/null +++ b/pkg/usrmgr/coredata/migrations/20250202T201400Z.sql @@ -0,0 +1 @@ +ALTER TABLE usrmgr_sessions ADD COLUMN expired_at TIMESTAMP WITH TIME ZONE NOT NULL; diff --git a/pkg/usrmgr/coredata/session.go b/pkg/usrmgr/coredata/session.go index 321de7942..82d0ebbe3 100644 --- a/pkg/usrmgr/coredata/session.go +++ b/pkg/usrmgr/coredata/session.go @@ -30,6 +30,7 @@ type ( Session struct { ID gid.GID UserID gid.GID + ExpiredAt time.Time CreatedAt time.Time UpdatedAt time.Time } @@ -42,6 +43,8 @@ func (s Session) CursorKey() page.CursorKey { func (s *Session) scan(r pgx.Row) error { return r.Scan( &s.ID, + &s.UserID, + &s.ExpiredAt, &s.CreatedAt, &s.UpdatedAt, ) diff --git a/pkg/usrmgr/coredata/user.go b/pkg/usrmgr/coredata/user.go index ce07550f9..9c30004a1 100644 --- a/pkg/usrmgr/coredata/user.go +++ b/pkg/usrmgr/coredata/user.go @@ -16,7 +16,6 @@ package coredata import ( "context" - "fmt" "time" "github.com/getprobo/probo/pkg/gid" @@ -28,9 +27,11 @@ import ( type ( User struct { - ID gid.GID - CreatedAt time.Time - UpdatedAt time.Time + ID gid.GID + EmailAddress string + HashedPassword []byte + CreatedAt time.Time + UpdatedAt time.Time } ) @@ -41,6 +42,8 @@ func (u User) CursorKey() page.CursorKey { func (u *User) scan(r pgx.Row) error { return r.Scan( &u.ID, + &u.EmailAddress, + &u.HashedPassword, &u.CreatedAt, &u.UpdatedAt, ) @@ -54,6 +57,8 @@ func (u *User) LoadByID( q := ` SELECT id, + email_address, + hashed_password, created_at, updated_at FROM @@ -63,8 +68,6 @@ WHERE LIMIT 1; ` - q = fmt.Sprintf(q) - args := pgx.NamedArgs{"user_id": userID} r := conn.QueryRow(ctx, q, args)