forked from mtusi2026/mtusi2026lab5
Prepare verified laboratory 5 implementation and evidence
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
-- name: ChargeAttachment :one
|
||||
UPDATE users SET attachment_count=attachment_count+1 WHERE id=$1 AND attachment_count<10000 RETURNING id;
|
||||
-- name: InsertAttachment :one
|
||||
INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,status)
|
||||
VALUES($1,$2,$3,$4,$5,$6,$7,'uploading') RETURNING *;
|
||||
-- name: GetAttachment :one
|
||||
SELECT * FROM attachments WHERE id=$1 FOR SHARE;
|
||||
-- name: SetAttachmentStatus :execrows
|
||||
UPDATE attachments SET status=$2,updated_at=clock_timestamp() WHERE id=$1 AND status=$3 AND pipeline_version=0;
|
||||
-- name: InsertMessageAttachment :exec
|
||||
INSERT INTO message_attachments(message_id,attachment_id,ordinal) VALUES($1,$2,$3);
|
||||
-- name: MessageAttachments :many
|
||||
SELECT attachment_id FROM message_attachments WHERE message_id=$1 ORDER BY ordinal LIMIT 10;
|
||||
-- name: ReconcileAttachments :many
|
||||
SELECT * FROM attachments WHERE id>$1 ORDER BY id LIMIT 101;
|
||||
@@ -0,0 +1,42 @@
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4)
|
||||
RETURNING id,username,display_name,created_at;
|
||||
-- name: GetUser :one
|
||||
SELECT id,username,display_name,created_at FROM users WHERE id=$1;
|
||||
-- name: Credentials :one
|
||||
SELECT id,password_hash FROM users WHERE username=$1;
|
||||
-- name: ChargeChat :one
|
||||
UPDATE users SET chat_count=chat_count+1 WHERE id=$1 AND chat_count<1000 RETURNING id;
|
||||
-- name: ChargeMessage :one
|
||||
UPDATE users SET message_count=message_count+1 WHERE id=$1 AND message_count<1000000 RETURNING id;
|
||||
-- name: InsertChat :one
|
||||
INSERT INTO chats(id,title) VALUES($1,$2) RETURNING id,title,created_at;
|
||||
-- name: InsertMembers :exec
|
||||
INSERT INTO chat_members(chat_id,user_id) SELECT $1,unnest($2::uuid[]);
|
||||
-- name: GetChat :one
|
||||
SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id)::uuid[] AS members
|
||||
FROM chats c WHERE c.id=$1;
|
||||
-- name: ListChats :many
|
||||
SELECT c.id,c.title,c.created_at,ARRAY(SELECT user_id FROM chat_members WHERE chat_id=c.id ORDER BY user_id)::uuid[] AS members
|
||||
FROM chats c JOIN chat_members m ON m.chat_id=c.id
|
||||
WHERE m.user_id=sqlc.arg(member_id) AND (c.created_at,c.id)>(sqlc.arg(after_time)::timestamptz,sqlc.arg(after_id)::uuid)
|
||||
ORDER BY c.created_at,c.id LIMIT sqlc.arg(page_limit);
|
||||
-- name: InsertMessage :one
|
||||
INSERT INTO messages(id,chat_id,sender_id,text) VALUES($1,$2,$3,$4) RETURNING id,chat_id,sender_id,text,created_at;
|
||||
-- name: ListMessages :many
|
||||
SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,
|
||||
ARRAY(SELECT attachment_id FROM message_attachments WHERE message_id=m.id ORDER BY ordinal LIMIT 10)::uuid[] AS attachment_ids
|
||||
FROM messages m WHERE m.chat_id=$1 AND (m.created_at,m.id)>(sqlc.arg(after_time)::timestamptz,sqlc.arg(after_id)::uuid)
|
||||
ORDER BY m.created_at,m.id LIMIT sqlc.arg(page_limit);
|
||||
-- name: LockSessionOwner :one
|
||||
SELECT id FROM users WHERE id=$1 FOR UPDATE;
|
||||
-- name: DeleteExpiredSessions :exec
|
||||
DELETE FROM sessions WHERE user_id=$1 AND expires_at<=clock_timestamp();
|
||||
-- name: InsertSession :one
|
||||
INSERT INTO sessions(token_hash,user_id,expires_at)
|
||||
SELECT $1,$2,$3 WHERE (SELECT count(*) FROM sessions WHERE user_id=$2)<100
|
||||
RETURNING user_id;
|
||||
-- name: AuthenticateSession :one
|
||||
SELECT user_id FROM sessions WHERE token_hash=$1 AND expires_at>clock_timestamp();
|
||||
-- name: RevokeSession :exec
|
||||
DELETE FROM sessions WHERE token_hash=$1;
|
||||
@@ -0,0 +1,12 @@
|
||||
-- name: ReserveKey :one
|
||||
INSERT INTO idempotency(user_id,chat_id,key,payload_hash,message_id,expires_at)
|
||||
VALUES($1,$2,$3,$4,$5,clock_timestamp()+interval '24 hours')
|
||||
ON CONFLICT(user_id,chat_id,key) DO UPDATE SET payload_hash=excluded.payload_hash,
|
||||
message_id=excluded.message_id,expires_at=excluded.expires_at
|
||||
WHERE idempotency.expires_at<=clock_timestamp()
|
||||
RETURNING message_id;
|
||||
-- name: ReplayMessage :one
|
||||
SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,i.payload_hash,
|
||||
ARRAY(SELECT attachment_id FROM message_attachments WHERE message_id=m.id ORDER BY ordinal LIMIT 10)::uuid[] AS attachment_ids
|
||||
FROM idempotency i JOIN messages m ON m.id=i.message_id
|
||||
WHERE i.user_id=$1 AND i.chat_id=$2 AND i.key=$3;
|
||||
@@ -0,0 +1,50 @@
|
||||
-- name: InsertJob :exec
|
||||
INSERT INTO attachment_jobs(id,attachment_id,pipeline_version,traceparent,tracestate)
|
||||
VALUES(sqlc.arg(job_id),sqlc.arg(attachment_id),sqlc.arg(pipeline_version),sqlc.arg(traceparent),sqlc.arg(tracestate));
|
||||
-- name: QueueAttachment :one
|
||||
UPDATE attachments SET status='queued',updated_at=clock_timestamp() WHERE id=$1 AND status='uploading' AND NOT staging_expired RETURNING *;
|
||||
-- name: PendingJobs :many
|
||||
SELECT j.* FROM attachment_jobs j JOIN attachments a ON a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version
|
||||
WHERE NOT j.completed AND a.status IN ('queued','processing') AND j.next_attempt<=clock_timestamp()
|
||||
AND j.lease_until<clock_timestamp() AND j.published_at<clock_timestamp()-interval '2 seconds'
|
||||
ORDER BY j.created_at,j.id LIMIT 100;
|
||||
-- name: Published :exec
|
||||
UPDATE attachment_jobs SET published_at=clock_timestamp() WHERE id=$1;
|
||||
-- name: AcquireJob :one
|
||||
UPDATE attachment_jobs j SET attempt=j.attempt+1,lease_id=sqlc.arg(lease_id),lease_until=clock_timestamp()+sqlc.arg(lease_seconds)::int*interval '1 second'
|
||||
WHERE j.id=sqlc.arg(job_id) AND j.pipeline_version=sqlc.arg(pipeline_version) AND NOT j.completed
|
||||
AND j.next_attempt<=clock_timestamp() AND j.lease_until<clock_timestamp()
|
||||
AND EXISTS(SELECT 1 FROM attachments a WHERE a.id=j.attachment_id AND a.pipeline_version=j.pipeline_version AND a.status IN ('queued','processing'))
|
||||
RETURNING j.*;
|
||||
-- name: ProcessingAttachment :one
|
||||
UPDATE attachments SET status='processing',updated_at=clock_timestamp()
|
||||
WHERE id=sqlc.arg(attachment_id) AND pipeline_version=sqlc.arg(pipeline_version) AND status IN ('queued','processing') RETURNING *;
|
||||
-- name: CompleteAttachment :execrows
|
||||
UPDATE attachments a SET status='ready',variants=sqlc.arg(variants)::jsonb,error_code='',updated_at=clock_timestamp()
|
||||
FROM attachment_jobs j WHERE j.id=sqlc.arg(job_id) AND j.attachment_id=a.id AND j.pipeline_version=a.pipeline_version
|
||||
AND j.lease_id=sqlc.arg(lease_id) AND j.lease_until>clock_timestamp() AND NOT j.completed AND a.status='processing';
|
||||
-- name: CompleteJob :exec
|
||||
UPDATE attachment_jobs SET completed=true,lease_until='epoch' WHERE id=sqlc.arg(job_id) AND lease_id=sqlc.arg(lease_id);
|
||||
-- name: RetryAttachment :execrows
|
||||
UPDATE attachments a SET status=sqlc.arg(next_status),error_code=sqlc.arg(error_code),updated_at=clock_timestamp()
|
||||
FROM attachment_jobs j WHERE j.id=sqlc.arg(job_id) AND j.attachment_id=a.id AND j.pipeline_version=a.pipeline_version
|
||||
AND j.lease_id=sqlc.arg(lease_id) AND j.lease_until>clock_timestamp() AND NOT j.completed AND a.status='processing';
|
||||
-- name: RetryJob :exec
|
||||
UPDATE attachment_jobs SET lease_until='epoch',next_attempt=clock_timestamp()+sqlc.arg(delay_ms)::int*interval '1 millisecond',completed=sqlc.arg(completed)
|
||||
WHERE id=sqlc.arg(job_id) AND lease_id=sqlc.arg(lease_id);
|
||||
-- name: ReplayAttachment :one
|
||||
UPDATE attachments SET status='queued',error_code='',updated_at=clock_timestamp() WHERE id=$1 AND status='failed' AND NOT staging_expired RETURNING *;
|
||||
-- name: ReplayJobs :exec
|
||||
UPDATE attachment_jobs SET replay_count=replay_count+1,replayed_at=clock_timestamp(),completed=false,attempt=0,lease_until='epoch',next_attempt='epoch',published_at='epoch' WHERE attachment_id=$1 AND pipeline_version=$2;
|
||||
-- name: AdvancePipeline :one
|
||||
UPDATE attachments SET status='queued',error_code='',variants='[]',pipeline_version=pipeline_version+1,staging_expired=false,updated_at=clock_timestamp() WHERE id=$1 AND (status='ready' OR (status='failed' AND NOT staging_expired)) RETURNING *;
|
||||
-- name: StagingCleanup :many
|
||||
WITH candidates AS (
|
||||
SELECT id FROM attachments WHERE NOT staging_cleaned AND (status='ready' OR (status IN ('failed','uploading') AND updated_at<clock_timestamp()-interval '24 hours'))
|
||||
ORDER BY updated_at,id LIMIT 20
|
||||
)
|
||||
UPDATE attachments a SET staging_expired=(a.status<>'ready') FROM candidates c WHERE a.id=c.id AND NOT a.staging_cleaned
|
||||
AND (a.status='ready' OR (a.status IN ('failed','uploading') AND a.updated_at<clock_timestamp()-interval '24 hours')) RETURNING a.*;
|
||||
-- name: StagingCleaned :exec
|
||||
UPDATE attachments SET staging_cleaned=true WHERE id=$1 AND (staging_expired OR status='ready');
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
version: '2'
|
||||
sql:
|
||||
- schema: ../migrations
|
||||
queries: queries
|
||||
engine: postgresql
|
||||
gen:
|
||||
go:
|
||||
package: queries
|
||||
out: ../../internal/adapter/repository/postgres/queries
|
||||
sql_package: pgx/v5
|
||||
emit_empty_slices: true
|
||||
overrides:
|
||||
- db_type: uuid
|
||||
go_type: github.com/google/uuid.UUID
|
||||
- db_type: timestamptz
|
||||
go_type: time.Time
|
||||
Reference in New Issue
Block a user