Add db uuid4 func and uuid insert message param
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sync"
|
"sync"
|
||||||
|
"uuid"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
@@ -55,6 +56,13 @@ func Connect(ctx context.Context, cfg Config) (*SQLite, error) {
|
|||||||
driverOnce.Do(func() {
|
driverOnce.Do(func() {
|
||||||
sql.Register("odidere_sqlite", &sqlite3.SQLiteDriver{
|
sql.Register("odidere_sqlite", &sqlite3.SQLiteDriver{
|
||||||
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
|
||||||
|
if err := conn.RegisterFunc(
|
||||||
|
"uuid4",
|
||||||
|
func() string { return uuid.New().String() },
|
||||||
|
false,
|
||||||
|
); err != nil {
|
||||||
|
return fmt.Errorf("failed to register uuid4: %w", err)
|
||||||
|
}
|
||||||
if err := conn.RegisterFunc(
|
if err := conn.RegisterFunc(
|
||||||
"regexp",
|
"regexp",
|
||||||
func(re, s string) (bool, error) {
|
func(re, s string) (bool, error) {
|
||||||
|
|||||||
@@ -313,6 +313,8 @@ type InsertMessageParams struct {
|
|||||||
Content string
|
Content string
|
||||||
// ConversationId identifies the conversation that owns the new row.
|
// ConversationId identifies the conversation that owns the new row.
|
||||||
ConversationId string
|
ConversationId string
|
||||||
|
// Id is the new row's UUID. Nil generates a version 4 UUID.
|
||||||
|
Id *uuid.UUID
|
||||||
// Metadata is inference JSON converted to JSONB for storage.
|
// Metadata is inference JSON converted to JSONB for storage.
|
||||||
// Nil stores SQL NULL; malformed JSON causes insertion to fail.
|
// Nil stores SQL NULL; malformed JSON causes insertion to fail.
|
||||||
Metadata jsontext.Value
|
Metadata jsontext.Value
|
||||||
@@ -324,6 +326,12 @@ type InsertMessageParams struct {
|
|||||||
|
|
||||||
// InsertMessage creates a new message.
|
// InsertMessage creates a new message.
|
||||||
func (q *Queries) InsertMessage(ctx context.Context, arg InsertMessageParams) (*Message, error) {
|
func (q *Queries) InsertMessage(ctx context.Context, arg InsertMessageParams) (*Message, error) {
|
||||||
|
var id uuid.UUID
|
||||||
|
if arg.Id == nil {
|
||||||
|
id = uuid.NewV4()
|
||||||
|
} else {
|
||||||
|
id = *arg.Id
|
||||||
|
}
|
||||||
var metadataArg any
|
var metadataArg any
|
||||||
if arg.Metadata != nil {
|
if arg.Metadata != nil {
|
||||||
metadataArg = string(arg.Metadata)
|
metadataArg = string(arg.Metadata)
|
||||||
@@ -331,7 +339,7 @@ func (q *Queries) InsertMessage(ctx context.Context, arg InsertMessageParams) (*
|
|||||||
row := q.db.QueryRowContext(
|
row := q.db.QueryRowContext(
|
||||||
ctx,
|
ctx,
|
||||||
insertMessage,
|
insertMessage,
|
||||||
uuid.New().String(),
|
id.String(),
|
||||||
arg.Content,
|
arg.Content,
|
||||||
arg.ConversationId,
|
arg.ConversationId,
|
||||||
metadataArg,
|
metadataArg,
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"uuid"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
)
|
)
|
||||||
@@ -344,6 +345,53 @@ func testMessaging(t *testing.T, ctx context.Context, db *SQLite) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
suppliedID := uuid.NewV4()
|
||||||
|
for _, tt := range []struct {
|
||||||
|
name string
|
||||||
|
id *uuid.UUID
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{name: "generated ID"},
|
||||||
|
{name: "supplied v4 ID", id: &suppliedID},
|
||||||
|
{name: "duplicate ID", id: &suppliedID, wantErr: true},
|
||||||
|
} {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
seq, err := qw.NextSequence(ctx, c.Id)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
inserted, err := qw.InsertMessage(ctx, InsertMessageParams{
|
||||||
|
Content: `{"role":"assistant","content":"answer"}`,
|
||||||
|
ConversationId: c.Id,
|
||||||
|
Id: tt.id,
|
||||||
|
Role: MessageRoleAssistant,
|
||||||
|
Sequence: seq,
|
||||||
|
})
|
||||||
|
if tt.wantErr {
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("duplicate ID was accepted")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := uuid.Parse(inserted.Id); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if tt.id != nil && inserted.Id != tt.id.String() {
|
||||||
|
t.Errorf("inserted ID = %s, want %s", inserted.Id, tt.id.String())
|
||||||
|
}
|
||||||
|
if tt.id == nil {
|
||||||
|
if err := qw.DeleteMessageId(ctx, inserted.Id); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if err := qw.DeleteMessageId(ctx, suppliedID.String()); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
tx, err := db.Writer.BeginTx(ctx, nil)
|
tx, err := db.Writer.BeginTx(ctx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
|||||||
Reference in New Issue
Block a user