Move pk to int autogen and uuid as handle

This commit is contained in:
Zoe Roux 2024-10-19 18:06:31 +02:00
parent 200087f2f6
commit a2df0ae305
No known key found for this signature in database
4 changed files with 23 additions and 16 deletions

View File

@ -1,7 +1,8 @@
begin; begin;
create table users( create table users(
id uuid not null primary key, pk serial primary key,
id uuid not null default gen_random_uuid(),
username varchar(256) not null unique, username varchar(256) not null unique,
email varchar(320) not null unique, email varchar(320) not null unique,
password text, password text,
@ -12,7 +13,7 @@ create table users(
); );
create table oidc_handle( create table oidc_handle(
user_id uuid not null references users(id) on delete cascade, user_pk integer not null references users(pk) on delete cascade,
provider varchar(256) not null, provider varchar(256) not null,
id text not null, id text not null,
@ -23,7 +24,7 @@ create table oidc_handle(
refresh_token text, refresh_token text,
expire_at timestamptz, expire_at timestamptz,
constraint oidc_handle_pk primary key (user_id, provider) constraint oidc_handle_pk primary key (user_pk, provider)
); );
commit; commit;

View File

@ -1,9 +1,10 @@
begin; begin;
create table sessions( create table sessions(
id uuid not null primary key, pk serial primary key,
id uuid not null default gen_random_uuid(),
token varchar(128) not null unique, token varchar(128) not null unique,
user_id uuid not null references users(id) on delete cascade, user_pk integer not null references users(pk) on delete cascade,
created_date timestamptz not null default now()::timestamptz, created_date timestamptz not null default now()::timestamptz,
last_used timestamptz not null default now()::timestamptz, last_used timestamptz not null default now()::timestamptz,
device varchar(1024) device varchar(1024)

View File

@ -5,7 +5,7 @@ select
sqlc.embed(u) sqlc.embed(u)
from from
users as u users as u
inner join sessions as s on u.id = s.user_id inner join sessions as s on u.pk = s.user_pk
where where
s.token = $1 s.token = $1
limit 1; limit 1;
@ -20,24 +20,26 @@ where
-- name: GetUserSessions :many -- name: GetUserSessions :many
select select
* s.*
from from
sessions sessions as s
inner join users as u on u.pk = s.user_pk
where where
user_id = $1 u.pk = $1
order by order by
last_used; last_used;
-- name: CreateSession :one -- name: CreateSession :one
insert into sessions(token, user_id, device) insert into sessions(token, user_pk, device)
values ($1, $2, $3) values ($1, $2, $3)
returning returning
*; *;
-- name: DeleteSession :one -- name: DeleteSession :one
delete from sessions delete from sessions as s using users as u
where id = $1 where s.user_pk = u.pk
and user_id = $2 and s.id = $1
and u.id = sqlc.arg(user_id)
returning returning
*; s.*;

View File

@ -21,10 +21,13 @@ limit $1;
-- name: GetUser :many -- name: GetUser :many
select select
sqlc.embed(u), sqlc.embed(u),
sqlc.embed(h) h.provider,
h.id,
h.username,
h.profile_url
from from
users as u users as u
left join oidc_handle as h on u.id = h.user_id left join oidc_handle as h on u.pk = h.user_pk
where where
u.id = $1; u.id = $1;