init
Some checks failed
Build and Release / release (push) Failing after 51s

This commit is contained in:
2026-03-25 22:29:07 +07:00
parent eedd300861
commit 79199f627d
65 changed files with 3215 additions and 689 deletions

View File

@@ -3,7 +3,7 @@ CREATE EXTENSION IF NOT EXISTS postgis;
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuidv7(),
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
password_hash TEXT,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN NOT NULL DEFAULT false,

View File

@@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS user_verifications (
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
verify_type SMALLINT NOT NULL, -- 1 = ID_CARD, 2 = EDUCATION, 3 = EXPERT
document_url TEXT NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false,
status SMALLINT NOT NULL DEFAULT 1, -- 1 pending, 2 approved, 3 rejected
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
@@ -10,9 +11,18 @@ CREATE TABLE IF NOT EXISTS user_verifications (
);
CREATE INDEX idx_user_verifications_user_id ON user_verifications(user_id);
CREATE INDEX idx_user_verifications_user_type ON user_verifications(user_id, verify_type);
CREATE INDEX idx_user_verifications_status ON user_verifications(status);
CREATE INDEX idx_user_verifications_user_id
ON user_verifications(user_id)
WHERE is_deleted = false;
CREATE INDEX idx_user_verifications_user_type
ON user_verifications(user_id, verify_type)
WHERE is_deleted = false;
CREATE INDEX idx_user_verifications_status
ON user_verifications(status)
WHERE is_deleted = false;
CREATE INDEX idx_user_verifications_status_created
ON user_verifications(status, created_at DESC);
ON user_verifications(status, created_at DESC)
WHERE is_deleted = false;

View File

@@ -3,18 +3,23 @@ CREATE TABLE IF NOT EXISTS user_tokens (
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE,
token_type SMALLINT NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX idx_user_tokens_token
ON user_tokens(token);
ON user_tokens(token)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_user_id
ON user_tokens(user_id);
ON user_tokens(user_id)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_type
ON user_tokens(token_type);
ON user_tokens(token_type)
WHERE is_deleted = false;
CREATE INDEX idx_user_tokens_expires_at
ON user_tokens(expires_at);
ON user_tokens(expires_at)
WHERE is_deleted = false;

View File

@@ -11,11 +11,38 @@ CREATE TABLE IF NOT EXISTS entities (
description TEXT,
thumbnail_url TEXT,
status SMALLINT DEFAULT 1, -- 1 draft, 2 published
is_deleted BOOLEAN NOT NULL DEFAULT false,
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE INDEX idx_entities_slug ON entities(slug);
CREATE UNIQUE INDEX uniq_entities_slug_active
ON entities(slug)
WHERE is_deleted = false;
CREATE INDEX idx_entities_type
ON entities(type_id)
WHERE is_deleted = false;
CREATE INDEX idx_entities_status_created
ON entities(status, created_at DESC)
WHERE is_deleted = false;
CREATE INDEX idx_entities_type_status
ON entities(type_id, status)
WHERE is_deleted = false;
CREATE INDEX idx_entities_reviewed_by
ON entities(reviewed_by)
WHERE is_deleted = false;
CREATE INDEX idx_entities_name_search
ON entities USING gin (name gin_trgm_ops);
CREATE TRIGGER trigger_entities_updated_at
BEFORE UPDATE ON entities

View File

@@ -1,25 +1,20 @@
CREATE TABLE IF NOT EXISTS wiki_pages (
CREATE TABLE IF NOT EXISTS wikis (
id UUID PRIMARY KEY DEFAULT uuidv7(),
entity_id UUID REFERENCES entities(id) ON DELETE CASCADE,
user_id UUID REFERENCES users(id),
title TEXT,
is_deleted BOOLEAN NOT NULL DEFAULT false,
note TEXT,
content TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS wiki_versions (
id UUID PRIMARY KEY DEFAULT uuidv7(),
wiki_id UUID REFERENCES wiki_pages(id) ON DELETE CASCADE,
created_user UUID REFERENCES users(id),
note TEXT,
content TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
approved_at TIMESTAMPTZ
);
CREATE INDEX idx_wiki_entity
ON wikis(entity_id)
WHERE is_deleted = false;
CREATE INDEX idx_wiki_entity ON wiki_pages(entity_id);
CREATE TRIGGER trigger_wiki_pages_updated_at
BEFORE UPDATE ON wiki_pages
CREATE TRIGGER trigger_wikis_updated_at
BEFORE UPDATE ON wikis
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();

View File

@@ -1,8 +1,11 @@
CREATE EXTENSION IF NOT EXISTS btree_gist;
CREATE TABLE IF NOT EXISTS geometries (
id UUID PRIMARY KEY DEFAULT uuidv7(),
geom GEOMETRY, -- point / polygon / line
time_start INT,
time_end INT,
is_deleted BOOLEAN NOT NULL DEFAULT false,
bbox GEOMETRY, -- optional
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
@@ -13,9 +16,11 @@ CREATE TABLE IF NOT EXISTS geo_versions (
geo_id UUID REFERENCES geometries(id) ON DELETE CASCADE,
created_user UUID REFERENCES users(id),
geom GEOMETRY,
is_deleted BOOLEAN NOT NULL DEFAULT false,
note TEXT,
created_at TIMESTAMPTZ DEFAULT now(),
approved_at TIMESTAMPTZ
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS entity_geometries (
@@ -24,8 +29,33 @@ CREATE TABLE IF NOT EXISTS entity_geometries (
PRIMARY KEY (entity_id, geometry_id)
);
CREATE INDEX idx_geo_time ON geometries(time_start, time_end);
CREATE INDEX idx_geom_spatial ON geometries USING GIST (geom);
CREATE INDEX idx_geom_spatial_active
ON geometries USING GIST (geom)
WHERE is_deleted = false;
CREATE INDEX idx_geom_bbox
ON geometries USING GIST (bbox)
WHERE is_deleted = false;
CREATE INDEX idx_geom_time_range
ON geometries
USING GIST (int4range(time_start, time_end))
WHERE is_deleted = false;
CREATE INDEX idx_geo_versions_geo_id
ON geo_versions(geo_id)
WHERE is_deleted = false;
CREATE INDEX idx_geo_versions_reviewed_by
ON geo_versions(reviewed_by)
WHERE is_deleted = false;
CREATE INDEX idx_geo_versions_created_at
ON geo_versions(created_at DESC)
WHERE is_deleted = false;
CREATE INDEX idx_entity_geometries_geometry
ON entity_geometries(geometry_id);
CREATE TRIGGER trigger_geometries_updated_at
BEFORE UPDATE ON geometries

View File

@@ -1,42 +1,45 @@
-- name: CreateUser :one
-- name: UpsertUser :one
INSERT INTO users (
name,
email,
password_hash,
google_id,
auth_provider,
is_verified
) VALUES (
$1, $2, $3, $4, $5
)
ON CONFLICT (email)
DO UPDATE SET
google_id = EXCLUDED.google_id,
auth_provider = EXCLUDED.auth_provider,
is_verified = users.is_verified OR EXCLUDED.is_verified,
updated_at = now()
RETURNING *;
-- name: CreateUserProfile :one
INSERT INTO user_profiles (
user_id,
display_name,
avatar_url
) VALUES (
$1, $2, $3, $4
$1, $2, $3
)
RETURNING *;
-- name: UpdateUser :one
UPDATE users
SET
name = $1,
avatar_url = $2,
is_active = $3,
is_verified = $4,
-- name: UpdateUserProfile :one
UPDATE user_profiles
SET
display_name = $1,
full_name = $2,
avatar_url = $3,
bio = $4,
location = $5,
website = $6,
country_code = $7,
phone = $8,
updated_at = now()
WHERE users.id = $5 AND users.is_deleted = false
RETURNING
users.id,
users.name,
users.email,
users.password_hash,
users.avatar_url,
users.is_active,
users.is_verified,
users.token_version,
users.refresh_token,
users.is_deleted,
users.created_at,
users.updated_at,
(
SELECT COALESCE(json_agg(json_build_object('id', roles.id, 'name', roles.name)), '[]')::json
FROM user_roles
JOIN roles ON user_roles.role_id = roles.id
WHERE user_roles.user_id = users.id
) AS roles;
WHERE user_id = $9
RETURNING *;
-- name: UpdateUserPassword :exec
UPDATE users
@@ -52,7 +55,6 @@ SET
WHERE id = $1
AND is_deleted = false;
-- name: VerifyUser :exec
UPDATE users
SET
@@ -72,86 +74,133 @@ SET
is_deleted = false
WHERE id = $1;
-- name: ExistsUserByEmail :one
SELECT EXISTS (
SELECT 1 FROM users
WHERE email = $1
AND is_deleted = false
);
-- name: GetUsers :many
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.is_deleted = false
GROUP BY u.id;
-- name: GetUserByID :one
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
-- profile JSON
(
SELECT json_build_object(
'display_name', p.display_name,
'full_name', p.full_name,
'avatar_url', p.avatar_url,
'bio', p.bio,
'location', p.location,
'website', p.website,
'country_code', p.country_code,
'phone', p.phone
)
FROM user_profiles p
WHERE p.user_id = u.id
) AS profile,
-- roles JSON
(
SELECT COALESCE(
json_agg(json_build_object('id', r.id, 'name', r.name)),
'[]'
)::json
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = u.id
) AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.id = $1 AND u.is_deleted = false
GROUP BY u.id;
WHERE u.id = $1 AND u.is_deleted = false;
-- name: GetTokenVersion :one
SELECT token_version
FROM users
WHERE id = $1 AND is_deleted = false;
-- name: UpdateTokenVersion :exec
UPDATE users
SET token_version = $2
WHERE id = $1 AND is_deleted = false;
-- name: GetUserByEmail :one
SELECT
u.id,
u.name,
u.email,
u.password_hash,
u.avatar_url,
u.is_active,
u.is_verified,
u.token_version,
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.is_deleted,
u.created_at,
u.created_at,
u.updated_at,
COALESCE(
json_agg(
json_build_object('id', r.id, 'name', r.name)
) FILTER (WHERE r.id IS NOT NULL),
'[]'
)::json AS roles
(
SELECT json_build_object(
'display_name', p.display_name,
'full_name', p.full_name,
'avatar_url', p.avatar_url,
'bio', p.bio,
'location', p.location,
'website', p.website,
'country_code', p.country_code,
'phone', p.phone
)
FROM user_profiles p
WHERE p.user_id = u.id
) AS profile,
(
SELECT COALESCE(
json_agg(json_build_object('id', r.id, 'name', r.name)),
'[]'
)::json
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = u.id
) AS roles
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id
WHERE u.email = $1 AND u.is_deleted = false
GROUP BY u.id;
WHERE u.email = $1 AND u.is_deleted = false;
-- name: GetUsers :many
SELECT
u.id,
u.email,
u.password_hash,
u.is_verified,
u.token_version,
u.refresh_token,
u.is_deleted,
u.created_at,
u.updated_at,
(
SELECT json_build_object(
'display_name', p.display_name,
'full_name', p.full_name,
'avatar_url', p.avatar_url,
'bio', p.bio,
'location', p.location,
'website', p.website,
'country_code', p.country_code,
'phone', p.phone
)
FROM user_profiles p
WHERE p.user_id = u.id
) AS profile,
(
SELECT COALESCE(
json_agg(json_build_object('id', r.id, 'name', r.name)),
'[]'
)::json
FROM user_roles ur
JOIN roles r ON ur.role_id = r.id
WHERE ur.user_id = u.id
) AS roles
FROM users u
WHERE u.is_deleted = false;

View File

@@ -1,18 +1,18 @@
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT uuidv7(),
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
password_hash TEXT,
google_id VARCHAR(255) UNIQUE,
auth_provider VARCHAR(50) NOT NULL DEFAULT 'local',
is_verified BOOLEAN DEFAULT false,
is_verified BOOLEAN NOT NULL DEFAULT false,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_version INT NOT NULL DEFAULT 1,
refresh_token TEXT,
is_deleted BOOLEAN DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_profiles (
CREATE TABLE IF NOT EXISTS user_profiles (
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
display_name TEXT,
full_name TEXT,
@@ -26,21 +26,10 @@ CREATE TABLE user_profiles (
updated_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE user_verifications (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
verify_type SMALLINT NOT NULL, -- 1 = ID_CARD, 2 = EDUCATION, 3 = EXPERT
document_url TEXT NOT NULL,
status SMALLINT NOT NULL DEFAULT 1, -- 1 pending, 2 approved, 3 rejected
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS roles (
id UUID PRIMARY KEY DEFAULT uuidv7(),
name TEXT UNIQUE NOT NULL,
is_deleted BOOLEAN DEFAULT false,
is_deleted BOOLEAN NOT NULL DEFAULT false,
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
@@ -51,11 +40,23 @@ CREATE TABLE IF NOT EXISTS user_roles (
PRIMARY KEY (user_id, role_id)
);
CREATE TABLE IF NOT EXISTS user_verifications (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
verify_type SMALLINT NOT NULL, -- 1 = ID_CARD, 2 = EDUCATION, 3 = EXPERT
document_url TEXT NOT NULL,
status SMALLINT NOT NULL DEFAULT 1, -- 1 pending, 2 approved, 3 rejected
reviewed_by UUID REFERENCES users(id),
reviewed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS user_tokens (
id UUID PRIMARY KEY DEFAULT uuidv7(),
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(255) NOT NULL UNIQUE, --1 = PasswordReset, 2 = EmailVerify, 3 = MagicLink, 4 = RefreshToken
token VARCHAR(255) NOT NULL UNIQUE,
is_deleted BOOLEAN NOT NULL DEFAULT false,
token_type SMALLINT NOT NULL,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
);