ЛР7 на 4: HTTPS, mTLS, OpenBao AppRole и ротация leaf.
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Smoke/security/grade4/persistence/failover пройдены; REPORT и DEFENSE.
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
-- name: CreateAttachmentReplay :exec
|
||||
INSERT INTO attachment_replays (
|
||||
attachment_id,
|
||||
attempt,
|
||||
previous_error_code,
|
||||
operator,
|
||||
reason
|
||||
) VALUES (
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(attempt),
|
||||
sqlc.narg(previous_error_code),
|
||||
sqlc.arg(operator),
|
||||
sqlc.arg(reason)
|
||||
);
|
||||
@@ -0,0 +1,28 @@
|
||||
-- name: CreateAttachmentVariant :exec
|
||||
INSERT INTO attachment_variants (
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
variant,
|
||||
s3_key,
|
||||
content_type,
|
||||
size_bytes,
|
||||
width,
|
||||
height,
|
||||
sha256
|
||||
) VALUES (
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(pipeline_version),
|
||||
sqlc.arg(variant),
|
||||
sqlc.arg(s3_key),
|
||||
sqlc.arg(content_type),
|
||||
sqlc.arg(size_bytes),
|
||||
sqlc.arg(width),
|
||||
sqlc.arg(height),
|
||||
sqlc.arg(sha256)
|
||||
);
|
||||
|
||||
-- name: ListAttachmentVariants :many
|
||||
SELECT * FROM attachment_variants
|
||||
WHERE attachment_id = sqlc.arg(attachment_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
ORDER BY variant;
|
||||
@@ -0,0 +1,227 @@
|
||||
-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
staging_key,
|
||||
job_id,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
width,
|
||||
height,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(id)::uuid,
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(owner_id)::uuid,
|
||||
sqlc.arg(s3_key)::text,
|
||||
sqlc.arg(staging_key)::text,
|
||||
sqlc.arg(job_id)::uuid,
|
||||
sqlc.arg(filename)::text,
|
||||
sqlc.arg(content_type)::attachment_content_type,
|
||||
sqlc.arg(size_bytes)::int,
|
||||
sqlc.arg(sha256)::text,
|
||||
sqlc.arg(width)::int,
|
||||
sqlc.arg(height)::int,
|
||||
NULLIF(sqlc.arg(error_code)::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(owner_id)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateMessageAttachment :exec
|
||||
INSERT INTO message_attachments (
|
||||
message_id,
|
||||
attachment_id,
|
||||
position
|
||||
) VALUES (
|
||||
sqlc.arg(message_id),
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(position)
|
||||
);
|
||||
|
||||
-- name: GetAttachmentByID :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = $1::uuid;
|
||||
|
||||
-- name: LockAttachmentByStagingKey :one
|
||||
SELECT * FROM attachments
|
||||
WHERE staging_key = sqlc.arg(staging_key)::text
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: ListAttachmentsByIDs :many
|
||||
SELECT status
|
||||
FROM attachments
|
||||
WHERE chat_id = sqlc.arg(chat_id)::uuid
|
||||
AND id = ANY(sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: ListMessageAttachmentIDs :many
|
||||
SELECT attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = sqlc.arg(message_id)::uuid
|
||||
ORDER BY position;
|
||||
|
||||
-- name: ListMessageAttachmentsByMessageIDs :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
||||
ORDER BY message_id, position;
|
||||
|
||||
-- name: ListAttachmentsForReconcile :many
|
||||
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
|
||||
FROM attachments;
|
||||
|
||||
-- name: ListFailedAttachments :many
|
||||
SELECT id, job_id, error_code, attempts, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'failed'
|
||||
ORDER BY updated_at, id
|
||||
LIMIT sqlc.arg(batch_size)::int;
|
||||
|
||||
-- name: LockAttachmentJob :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now()
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT * FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: LockFailedAttachmentForReplay :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockAttachmentForUpgrade :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: ClearAttachmentStagingKey :exec
|
||||
UPDATE attachments
|
||||
SET staging_key = NULL
|
||||
WHERE id = sqlc.arg(id)::uuid;
|
||||
|
||||
-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'ready';
|
||||
|
||||
-- name: ClaimAttachmentJob :one
|
||||
UPDATE attachments
|
||||
SET status = 'processing',
|
||||
attempts = attempts + 1,
|
||||
lease_until = sqlc.arg(lease_until)::timestamptz,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = sqlc.arg(attempt)::int
|
||||
RETURNING *;
|
||||
|
||||
-- name: CompleteAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: UpgradeAttachmentPipeline :execrows
|
||||
UPDATE attachments
|
||||
SET pipeline_version = 2,
|
||||
status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL;
|
||||
|
||||
-- name: ReplayAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
);
|
||||
|
||||
-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RecoverExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: FailExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = 'retry_exhausted',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
@@ -0,0 +1,73 @@
|
||||
-- name: CreateChat :one
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(title)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: AddChatMembers :exec
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
unnest(sqlc.arg(user_ids)::uuid[]);
|
||||
|
||||
-- name: IsChatMember :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
);
|
||||
|
||||
-- name: GetChat :one
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
WHERE c.id = sqlc.arg(id)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = c.id
|
||||
AND m.user_id = sqlc.arg(user_id)
|
||||
);
|
||||
|
||||
-- name: ListChatsFirstPage :many
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = sqlc.arg(user_id)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT sqlc.arg(lim);
|
||||
|
||||
-- name: ListChatsAfter :many
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = sqlc.arg(user_id)
|
||||
AND (c.created_at, c.id) > (
|
||||
sqlc.arg(after_created_at)::timestamptz,
|
||||
sqlc.arg(after_id)::uuid
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT sqlc.arg(lim);
|
||||
@@ -0,0 +1,36 @@
|
||||
-- name: CreateIdempotencyKey :execrows
|
||||
INSERT INTO idempotency_keys (
|
||||
user_id,
|
||||
method,
|
||||
path,
|
||||
key,
|
||||
request_hash,
|
||||
message_id
|
||||
) VALUES (
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(method),
|
||||
sqlc.arg(path),
|
||||
sqlc.arg(key),
|
||||
sqlc.arg(request_hash),
|
||||
sqlc.arg(message_id)
|
||||
)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: GetIdempotentMessage :one
|
||||
SELECT
|
||||
k.request_hash,
|
||||
m.id,
|
||||
m.chat_id,
|
||||
m.sender_id,
|
||||
m.text,
|
||||
m.created_at
|
||||
FROM idempotency_keys AS k
|
||||
JOIN messages AS m ON m.id = k.message_id
|
||||
WHERE k.user_id = sqlc.arg(user_id)
|
||||
AND k.method = sqlc.arg(method)
|
||||
AND k.path = sqlc.arg(path)
|
||||
AND k.key = sqlc.arg(key);
|
||||
|
||||
-- name: DeleteExpiredIdempotencyKeys :execrows
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE created_at < sqlc.arg(cutoff);
|
||||
@@ -0,0 +1,34 @@
|
||||
-- name: CreateMessage :one
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(id)::uuid,
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(sender_id)::uuid,
|
||||
sqlc.arg(text)::text
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(sender_id)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListMessagesFirstPage :many
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
ORDER BY created_at, id
|
||||
LIMIT sqlc.arg(lim);
|
||||
|
||||
-- name: ListMessagesAfter :many
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND (created_at, id) > (
|
||||
sqlc.arg(after_created_at)::timestamptz,
|
||||
sqlc.arg(after_id)::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT sqlc.arg(lim);
|
||||
@@ -0,0 +1,36 @@
|
||||
-- name: CreateOutboxEntry :one
|
||||
INSERT INTO outbox (
|
||||
job_id,
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
attempt,
|
||||
available_at,
|
||||
traceparent,
|
||||
tracestate
|
||||
)
|
||||
SELECT
|
||||
job_id,
|
||||
id,
|
||||
pipeline_version,
|
||||
sqlc.arg(attempt)::int,
|
||||
sqlc.arg(available_at)::timestamptz,
|
||||
sqlc.arg(traceparent)::text,
|
||||
sqlc.arg(tracestate)::text
|
||||
FROM attachments
|
||||
WHERE id = sqlc.arg(attachment_id)::uuid
|
||||
AND job_id IS NOT NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: LockPendingOutboxEntries :many
|
||||
SELECT * FROM outbox
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= now()
|
||||
ORDER BY available_at, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: MarkOutboxPublished :execrows
|
||||
UPDATE outbox
|
||||
SET published_at = now()
|
||||
WHERE id = sqlc.arg(id)::bigint
|
||||
AND published_at IS NULL;
|
||||
@@ -0,0 +1,24 @@
|
||||
-- name: CreateSession :exec
|
||||
INSERT INTO sessions (
|
||||
token_hash,
|
||||
user_id,
|
||||
expires_at
|
||||
) VALUES (
|
||||
sqlc.arg(token_hash),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(expires_at)
|
||||
);
|
||||
|
||||
-- name: GetActiveSession :one
|
||||
SELECT user_id, expires_at FROM sessions
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
AND expires_at > now();
|
||||
|
||||
-- name: DeleteSession :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
AND expires_at > now();
|
||||
|
||||
-- name: DeleteExpiredSessions :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at <= now();
|
||||
@@ -0,0 +1,24 @@
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(username),
|
||||
sqlc.arg(display_name),
|
||||
sqlc.arg(password_hash)
|
||||
)
|
||||
RETURNING id, username, display_name, created_at;
|
||||
|
||||
-- name: GetUser :one
|
||||
SELECT id, username, display_name, created_at FROM users
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: GetUserByUsername :one
|
||||
SELECT * FROM users
|
||||
WHERE username = sqlc.arg(username);
|
||||
|
||||
-- name: UpdatePasswordHash :exec
|
||||
UPDATE users SET password_hash = sqlc.arg(password_hash) WHERE id = sqlc.arg(id);
|
||||
Reference in New Issue
Block a user