qnote/server/internal/data/note_versions.sql.go
ae 62b1a58e56
feat!: trimming & logic/schema improvements
- build: somewhat polished dockerization setup
- build: io/fs migrations with `golang-migrate`
- feat: automatic init. admin account creation (.env creds)
- feat(routers): combined user & token routers into single auth router
- feat(routers): improved route layouts (`Routes`)
- feat(middlewares): removed redundant `userCtx` middleware
- fix(schema): note <-> note_versions relation (versioning)
- feat(queries): removed redundant rollback functionality
- feat(queries): combined duplicate version check & insertion/creation
- tests: decreased redundancy by removing 'unnecessary' unit tests
- refactor: hid internal packages behind `server/internal`
- docs: notes & auth handler comments
2025-04-09 01:58:38 +03:00

157 lines
3.6 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.28.0
// source: note_versions.sql
package data
import (
"context"
"time"
"github.com/google/uuid"
)
const createNoteVersion = `-- name: CreateNoteVersion :exec
WITH potential_duplicate AS (
SELECT version_number
FROM note_versions
WHERE
note_id = $1
AND content_hash = $2
ORDER BY version_number DESC
LIMIT 1
),
note_update AS (
UPDATE notes
SET
current_version = COALESCE(
(SELECT version_number FROM potential_duplicate),
latest_version + 1 -- increment only if we don't jump into a historical version
),
latest_version = CASE
WHEN (SELECT version_number FROM potential_duplicate) IS NULL
THEN latest_version + 1
ELSE latest_version
END,
updated_at = NOW()
WHERE id = $1
RETURNING current_version, latest_version
)
INSERT INTO note_versions (
note_id, title, content, version_number, content_hash
)
SELECT
$1, -- note_id
$3, -- title
$4, -- content
current_version,
$2 -- content_hash
FROM note_update
WHERE NOT EXISTS (SELECT 1 FROM potential_duplicate)
`
type CreateNoteVersionParams struct {
NoteID uuid.UUID `json:"note_id"`
ContentHash string `json:"content_hash"`
Title string `json:"title"`
Content string `json:"content"`
}
func (q *Queries) CreateNoteVersion(ctx context.Context, arg CreateNoteVersionParams) error {
_, err := q.db.Exec(ctx, createNoteVersion,
arg.NoteID,
arg.ContentHash,
arg.Title,
arg.Content,
)
return err
}
const getVersion = `-- name: GetVersion :one
SELECT
id AS version_id,
title,
content,
version_number,
created_at
FROM note_versions
WHERE note_id = $1 AND id = $2
`
type GetVersionParams struct {
NoteID uuid.UUID `json:"note_id"`
ID uuid.UUID `json:"id"`
}
type GetVersionRow struct {
VersionID uuid.UUID `json:"version_id"`
Title string `json:"title"`
Content string `json:"content"`
VersionNumber int32 `json:"version_number"`
CreatedAt *time.Time `json:"created_at"`
}
func (q *Queries) GetVersion(ctx context.Context, arg GetVersionParams) (GetVersionRow, error) {
row := q.db.QueryRow(ctx, getVersion, arg.NoteID, arg.ID)
var i GetVersionRow
err := row.Scan(
&i.VersionID,
&i.Title,
&i.Content,
&i.VersionNumber,
&i.CreatedAt,
)
return i, err
}
const getVersionHistory = `-- name: GetVersionHistory :many
SELECT
id AS version_id,
title,
version_number,
created_at
FROM note_versions
WHERE note_id = $1
ORDER BY version_number DESC
LIMIT $2 OFFSET $3
`
type GetVersionHistoryParams struct {
NoteID uuid.UUID `json:"note_id"`
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
type GetVersionHistoryRow struct {
VersionID uuid.UUID `json:"version_id"`
Title string `json:"title"`
VersionNumber int32 `json:"version_number"`
CreatedAt *time.Time `json:"created_at"`
}
func (q *Queries) GetVersionHistory(ctx context.Context, arg GetVersionHistoryParams) ([]GetVersionHistoryRow, error) {
rows, err := q.db.Query(ctx, getVersionHistory, arg.NoteID, arg.Limit, arg.Offset)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetVersionHistoryRow
for rows.Next() {
var i GetVersionHistoryRow
if err := rows.Scan(
&i.VersionID,
&i.Title,
&i.VersionNumber,
&i.CreatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}