Hard code keibi postgres schema

This commit is contained in:
Zoe Roux 2025-11-02 18:17:18 +01:00
parent 4dc34641ec
commit 5827cc32e8
No known key found for this signature in database
17 changed files with 77 additions and 84 deletions

View File

@ -43,8 +43,3 @@ PGPORT=5432
# PGSSLROOTCERT=/my/serving.crt
# PGSSLCERT=/my/client.crt
# PGSSLKEY=/my/client.key
# Default is keibi, you can specify "disabled" to use the default search_path of the user.
# If this is not "disabled", the schema will be created (if it does not exists) and
# the search_path of the user will be ignored (only the schema specified will be used).
POSTGRES_SCHEMA=keibi

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// sqlc v1.30.0
// source: apikeys.sql
package dbc
@ -13,7 +13,7 @@ import (
)
const createApiKey = `-- name: CreateApiKey :one
insert into apikeys(name, token, claims, created_by)
insert into keibi.apikeys(name, token, claims, created_by)
values ($1, $2, $3, $4)
returning
pk, id, name, token, claims, created_by, created_at, last_used
@ -48,7 +48,7 @@ func (q *Queries) CreateApiKey(ctx context.Context, arg CreateApiKeyParams) (Api
}
const deleteApiKey = `-- name: DeleteApiKey :one
delete from apikeys
delete from keibi.apikeys
where id = $1
returning
pk, id, name, token, claims, created_by, created_at, last_used
@ -74,7 +74,7 @@ const getApiKey = `-- name: GetApiKey :one
select
pk, id, name, token, claims, created_by, created_at, last_used
from
apikeys
keibi.apikeys
where
name = $1
and token = $2
@ -105,7 +105,7 @@ const listApiKeys = `-- name: ListApiKeys :many
select
pk, id, name, token, claims, created_by, created_at, last_used
from
apikeys
keibi.apikeys
order by
last_used
`
@ -141,7 +141,7 @@ func (q *Queries) ListApiKeys(ctx context.Context) ([]Apikey, error) {
const touchApiKey = `-- name: TouchApiKey :exec
update
apikeys
keibi.apikeys
set
last_used = now()::timestamptz
where

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// sqlc v1.30.0
package dbc

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// sqlc v1.30.0
package dbc

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// sqlc v1.30.0
// source: sessions.sql
package dbc
@ -13,7 +13,7 @@ import (
)
const clearOtherSessions = `-- name: ClearOtherSessions :exec
delete from sessions as s using users as u
delete from keibi.sessions as s using keibi.users as u
where s.user_pk = u.pk
and s.id != $1
and u.id = $2
@ -30,7 +30,7 @@ func (q *Queries) ClearOtherSessions(ctx context.Context, arg ClearOtherSessions
}
const createSession = `-- name: CreateSession :one
insert into sessions(token, user_pk, device)
insert into keibi.sessions(token, user_pk, device)
values ($1, $2, $3)
returning
pk, id, token, user_pk, created_date, last_used, device
@ -58,7 +58,7 @@ func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (S
}
const deleteSession = `-- name: DeleteSession :one
delete from sessions as s using users as u
delete from keibi.sessions as s using keibi.users as u
where s.user_pk = u.pk
and s.id = $1
and u.id = $2
@ -93,8 +93,8 @@ select
s.last_used,
u.pk, u.id, u.username, u.email, u.password, u.claims, u.created_date, u.last_seen
from
users as u
inner join sessions as s on u.pk = s.user_pk
keibi.users as u
inner join keibi.sessions as s on u.pk = s.user_pk
where
s.token = $1
limit 1
@ -130,8 +130,8 @@ const getUserSessions = `-- name: GetUserSessions :many
select
s.pk, s.id, s.token, s.user_pk, s.created_date, s.last_used, s.device
from
sessions as s
inner join users as u on u.pk = s.user_pk
keibi.sessions as s
inner join keibi.users as u on u.pk = s.user_pk
where
u.pk = $1
order by
@ -168,7 +168,7 @@ func (q *Queries) GetUserSessions(ctx context.Context, pk int32) ([]Session, err
const touchSession = `-- name: TouchSession :exec
update
sessions
keibi.sessions
set
last_used = now()::timestamptz
where

View File

@ -1,6 +1,6 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// sqlc v1.30.0
// source: users.sql
package dbc
@ -13,12 +13,12 @@ import (
)
const createUser = `-- name: CreateUser :one
insert into users(username, email, password, claims)
insert into keibi.users(username, email, password, claims)
values ($1, $2, $3, case when not exists (
select
pk, id, username, email, password, claims, created_date, last_seen
from
users) then
keibi.users) then
$4::jsonb
else
$5::jsonb
@ -58,7 +58,7 @@ func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, e
}
const deleteUser = `-- name: DeleteUser :one
delete from users
delete from keibi.users
where id = $1
returning
pk, id, username, email, password, claims, created_date, last_seen
@ -84,7 +84,7 @@ const getAllUsers = `-- name: GetAllUsers :many
select
pk, id, username, email, password, claims, created_date, last_seen
from
users
keibi.users
order by
id
limit $1
@ -123,7 +123,7 @@ const getAllUsersAfter = `-- name: GetAllUsersAfter :many
select
pk, id, username, email, password, claims, created_date, last_seen
from
users
keibi.users
where
id >= $2
order by
@ -173,8 +173,8 @@ select
h.username,
h.profile_url
from
users as u
left join oidc_handle as h on u.pk = h.user_pk
keibi.users as u
left join keibi.oidc_handle as h on u.pk = h.user_pk
where ($1::boolean
and u.id = $2)
or (not $1
@ -232,7 +232,7 @@ const getUserByLogin = `-- name: GetUserByLogin :one
select
pk, id, username, email, password, claims, created_date, last_seen
from
users
keibi.users
where
email = $1
or username = $1
@ -257,7 +257,7 @@ func (q *Queries) GetUserByLogin(ctx context.Context, login string) (User, error
const touchUser = `-- name: TouchUser :exec
update
users
keibi.users
set
last_used = now()::timestamptz
where
@ -271,7 +271,7 @@ func (q *Queries) TouchUser(ctx context.Context, pk int32) error {
const updateUser = `-- name: UpdateUser :one
update
users
keibi.users
set
username = coalesce($2, username),
email = coalesce($3, email),

View File

@ -106,29 +106,20 @@ func OpenDatabase() (*pgxpool.Pool, error) {
config.ConnConfig.RuntimeParams["application_name"] = "keibi"
}
schema := GetenvOr("POSTGRES_SCHEMA", "keibi")
if _, ok := config.ConnConfig.RuntimeParams["search_path"]; !ok {
config.ConnConfig.RuntimeParams["search_path"] = schema
}
db, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
fmt.Printf("Could not connect to database, check your env variables!\n")
return nil, err
}
if schema != "disabled" {
_, err = db.Exec(ctx, fmt.Sprintf("create schema if not exists %s", schema))
if err != nil {
return nil, err
}
}
fmt.Println("Migrating database")
dbi := stdlib.OpenDBFromPool(db)
defer dbi.Close()
driver, err := pgxd.WithInstance(dbi, &pgxd.Config{})
dbi.Exec("create schema if not exists keibi")
driver, err := pgxd.WithInstance(dbi, &pgxd.Config{
SchemaName: "keibi",
})
if err != nil {
return nil, err
}

View File

@ -1,6 +1,6 @@
begin;
drop table oidc_handle;
drop table users;
drop table keibi.oidc_handle;
drop table keibi.users;
commit;

View File

@ -1,6 +1,8 @@
begin;
create table users(
create schema if not exists keibi;
create table keibi.users(
pk serial primary key,
id uuid not null default gen_random_uuid(),
username varchar(256) not null unique,
@ -12,8 +14,8 @@ create table users(
last_seen timestamptz not null default now()::timestamptz
);
create table oidc_handle(
user_pk integer not null references users(pk) on delete cascade,
create table keibi.oidc_handle(
user_pk integer not null references keibi.users(pk) on delete cascade,
provider varchar(256) not null,
id text not null,

View File

@ -1,5 +1,5 @@
begin;
drop table sessions;
drop table keibi.sessions;
commit;

View File

@ -1,10 +1,10 @@
begin;
create table sessions(
create table keibi.sessions(
pk serial primary key,
id uuid not null default gen_random_uuid(),
token varchar(128) not null unique,
user_pk integer not null references users(pk) on delete cascade,
user_pk integer not null references keibi.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

@ -1,5 +1,5 @@
begin;
drop table apikeys;
drop table keibi.apikeys;
commit;

View File

@ -1,13 +1,13 @@
begin;
create table apikeys(
create table keibi.apikeys(
pk serial primary key,
id uuid not null default gen_random_uuid(),
name varchar(256) not null unique,
token varchar(128) not null unique,
claims jsonb not null,
created_by integer references users(pk) on delete cascade,
created_by integer references keibi.users(pk) on delete cascade,
created_at timestamptz not null default now()::timestamptz,
last_used timestamptz not null default now()::timestamptz
);

View File

@ -2,14 +2,14 @@
select
*
from
apikeys
keibi.apikeys
where
name = $1
and token = $2;
-- name: TouchApiKey :exec
update
apikeys
keibi.apikeys
set
last_used = now()::timestamptz
where
@ -19,18 +19,18 @@ where
select
*
from
apikeys
keibi.apikeys
order by
last_used;
-- name: CreateApiKey :one
insert into apikeys(name, token, claims, created_by)
insert into keibi.apikeys(name, token, claims, created_by)
values ($1, $2, $3, $4)
returning
*;
-- name: DeleteApiKey :one
delete from apikeys
delete from keibi.apikeys
where id = $1
returning
*;

View File

@ -5,15 +5,15 @@ select
s.last_used,
sqlc.embed(u)
from
users as u
inner join sessions as s on u.pk = s.user_pk
keibi.users as u
inner join keibi.sessions as s on u.pk = s.user_pk
where
s.token = $1
limit 1;
-- name: TouchSession :exec
update
sessions
keibi.sessions
set
last_used = now()::timestamptz
where
@ -23,21 +23,21 @@ where
select
s.*
from
sessions as s
inner join users as u on u.pk = s.user_pk
keibi.sessions as s
inner join keibi.users as u on u.pk = s.user_pk
where
u.pk = $1
order by
last_used;
-- name: CreateSession :one
insert into sessions(token, user_pk, device)
insert into keibi.sessions(token, user_pk, device)
values ($1, $2, $3)
returning
*;
-- name: DeleteSession :one
delete from sessions as s using users as u
delete from keibi.sessions as s using keibi.users as u
where s.user_pk = u.pk
and s.id = $1
and u.id = sqlc.arg(user_id)
@ -45,7 +45,7 @@ returning
s.*;
-- name: ClearOtherSessions :exec
delete from sessions as s using users as u
delete from keibi.sessions as s using keibi.users as u
where s.user_pk = u.pk
and s.id != @session_id
and u.id = @user_id;

View File

@ -2,7 +2,7 @@
select
*
from
users
keibi.users
order by
id
limit $1;
@ -11,7 +11,7 @@ limit $1;
select
*
from
users
keibi.users
where
id >= sqlc.arg(after_id)
order by
@ -26,8 +26,8 @@ select
h.username,
h.profile_url
from
users as u
left join oidc_handle as h on u.pk = h.user_pk
keibi.users as u
left join keibi.oidc_handle as h on u.pk = h.user_pk
where (@use_id::boolean
and u.id = @id)
or (not @use_id
@ -37,7 +37,7 @@ where (@use_id::boolean
select
*
from
users
keibi.users
where
email = sqlc.arg(login)
or username = sqlc.arg(login)
@ -45,19 +45,19 @@ limit 1;
-- name: TouchUser :exec
update
users
keibi.users
set
last_used = now()::timestamptz
where
pk = $1;
-- name: CreateUser :one
insert into users(username, email, password, claims)
insert into keibi.users(username, email, password, claims)
values ($1, $2, $3, case when not exists (
select
*
from
users) then
keibi.users) then
sqlc.arg(first_claims)::jsonb
else
sqlc.arg(claims)::jsonb
@ -67,7 +67,7 @@ returning
-- name: UpdateUser :one
update
users
keibi.users
set
username = coalesce(sqlc.narg(username), username),
email = coalesce(sqlc.narg(email), email),
@ -79,7 +79,7 @@ returning
*;
-- name: DeleteUser :one
delete from users
delete from keibi.users
where id = $1
returning
*;

View File

@ -30,15 +30,20 @@ sql:
- db_type: "jsonb"
go_type:
type: "interface{}"
- column: "users.claims"
- column: "keibi.users.claims"
go_type:
import: "github.com/golang-jwt/jwt/v5"
package: "jwt"
type: "MapClaims"
- column: "apikeys.claims"
- column: "keibi.apikeys.claims"
go_type:
import: "github.com/golang-jwt/jwt/v5"
package: "jwt"
type: "MapClaims"
overrides:
go:
rename:
keibi_apikey: Apikey
keibi_oidc_handle: OidcHandle
keibi_session: Session
keibi_user: User