notatest/server/sql/queries/note_versions.sql

28 lines
749 B
SQL

-- name: CreateNoteVersion :one
INSERT INTO note_versions (note_id, title, content, version_number, content_hash)
VALUES (
$1,
$2,
$3,
(SELECT COALESCE(MAX(version_number), 0) + 1 FROM note_versions WHERE note_id = $1),
encode(sha256($2::bytea || '\n'::bytea || $3::bytea), 'hex')
)
RETURNING *;
-- name: GetNoteVersions :many
SELECT * FROM note_versions
WHERE note_id = $1
ORDER BY version_number DESC
LIMIT $2 OFFSET $3;
-- name: GetNoteVersion :one
SELECT * FROM note_versions
WHERE note_id = $1 AND version_number = $2 LIMIT 1;
-- name: FindDuplicateContent :one
SELECT EXISTS(
SELECT 1 FROM note_versions
WHERE note_id = $1
AND content_hash = encode(sha256($2::bytea || '\n'::bytea || $3::bytea), 'hex')
);