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;
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,
email varchar(320) not null unique,
password text,
@ -12,7 +13,7 @@ create table users(
);
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,
id text not null,
@ -23,7 +24,7 @@ create table oidc_handle(
refresh_token text,
expire_at timestamptz,
constraint oidc_handle_pk primary key (user_id, provider)
constraint oidc_handle_pk primary key (user_pk, provider)
);
commit;

View File

@ -1,9 +1,10 @@
begin;
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,
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,
last_used timestamptz not null default now()::timestamptz,
device varchar(1024)

View File

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

View File

@ -21,10 +21,13 @@ limit $1;
-- name: GetUser :many
select
sqlc.embed(u),
sqlc.embed(h)
h.provider,
h.id,
h.username,
h.profile_url
from
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
u.id = $1;