diff options
author | Grail Finder <wohilas@gmail.com> | 2024-12-11 18:43:04 +0300 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2024-12-11 18:43:04 +0300 |
commit | ed5cb75134019a7ba063a13b5ff6f9cd296c80dd (patch) | |
tree | b42c92890ed315c149af39c09b211c874e5c7ead /storage | |
parent | 5f780287aecedf08f94b6a1e0ae2a8822683a3fc (diff) |
Refactor: sql on conflict; fix unittest; page names to vars
Diffstat (limited to 'storage')
-rw-r--r-- | storage/storage.go | 6 | ||||
-rw-r--r-- | storage/storage_test.go | 1 |
2 files changed, 6 insertions, 1 deletions
diff --git a/storage/storage.go b/storage/storage.go index b876dbc..0853328 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -53,11 +53,15 @@ func (p ProviderSQL) GetLastChatByAgent(agent string) (*models.Chat, error) { return &resp, err } +// https://sqlite.org/lang_upsert.html +// on conflict was added func (p ProviderSQL) UpsertChat(chat *models.Chat) (*models.Chat, error) { // Prepare the SQL statement query := ` - INSERT OR REPLACE INTO chats (id, name, msgs, agent, created_at, updated_at) + INSERT INTO chats (id, name, msgs, agent, created_at, updated_at) VALUES (:id, :name, :msgs, :agent, :created_at, :updated_at) + ON CONFLICT(id) DO UPDATE SET msgs=excluded.msgs, + updated_at=excluded.updated_at RETURNING *;` stmt, err := p.db.PrepareNamed(query) if err != nil { diff --git a/storage/storage_test.go b/storage/storage_test.go index ad1f1bf..8373ab0 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -95,6 +95,7 @@ func TestChatHistory(t *testing.T) { id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, msgs TEXT NOT NULL, + agent TEXT NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP );`) |