fix: make core migration PostgreSQL-safe
This commit is contained in:
@@ -84,14 +84,14 @@ func (s *Store) FinishTurn(ctx context.Context, turn core.Turn) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.pool.Exec(ctx, `UPDATE turns SET weknora_session_id=$2,weknora_request_id=$3,answer=$4,references=$5,status=$6,error_message=$7,updated_at=NOW() WHERE id=$1`, turn.ID, turn.WeKnoraSessionID, turn.WeKnoraRequestID, turn.Answer, references, turn.Status, turn.ErrorMessage)
|
||||
_, err = s.pool.Exec(ctx, `UPDATE turns SET weknora_session_id=$2,weknora_request_id=$3,answer=$4,references_json=$5,status=$6,error_message=$7,updated_at=NOW() WHERE id=$1`, turn.ID, turn.WeKnoraSessionID, turn.WeKnoraRequestID, turn.Answer, references, turn.Status, turn.ErrorMessage)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Store) loadTurn(ctx context.Context, tenantKey, channel, messageID string) (core.Turn, error) {
|
||||
var turn core.Turn
|
||||
var references []byte
|
||||
err := s.pool.QueryRow(ctx, `SELECT id,tenant_key,channel,external_message_id,session_key,weknora_session_id,weknora_request_id,question,answer,references,status,error_message FROM turns WHERE tenant_key=$1 AND channel=$2 AND external_message_id=$3`, tenantKey, channel, messageID).Scan(&turn.ID, &turn.TenantKey, &turn.Channel, &turn.ExternalMessageID, &turn.SessionKey, &turn.WeKnoraSessionID, &turn.WeKnoraRequestID, &turn.Question, &turn.Answer, &references, &turn.Status, &turn.ErrorMessage)
|
||||
err := s.pool.QueryRow(ctx, `SELECT id,tenant_key,channel,external_message_id,session_key,weknora_session_id,weknora_request_id,question,answer,references_json,status,error_message FROM turns WHERE tenant_key=$1 AND channel=$2 AND external_message_id=$3`, tenantKey, channel, messageID).Scan(&turn.ID, &turn.TenantKey, &turn.Channel, &turn.ExternalMessageID, &turn.SessionKey, &turn.WeKnoraSessionID, &turn.WeKnoraRequestID, &turn.Question, &turn.Answer, &references, &turn.Status, &turn.ErrorMessage)
|
||||
if err != nil {
|
||||
return core.Turn{}, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
// Source-level contract tests: the store's SQL must use the renamed
|
||||
// references_json column (the bare `references` identifier is a reserved
|
||||
// word in PostgreSQL 16), while the Go domain field stays turn.References.
|
||||
// No database connection is required.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func readStoreSource(t *testing.T) string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(filepath.Join(".", "store.go"))
|
||||
if err != nil {
|
||||
t.Fatalf("read store.go: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func extractSQLLiterals(src string) []string {
|
||||
var sqls []string
|
||||
re := regexp.MustCompile("`([^`]*)`")
|
||||
for _, m := range re.FindAllStringSubmatch(src, -1) {
|
||||
upper := strings.ToUpper(m[1])
|
||||
if strings.Contains(upper, "SELECT") || strings.Contains(upper, "UPDATE") ||
|
||||
strings.Contains(upper, "INSERT") || strings.Contains(upper, "DELETE") {
|
||||
sqls = append(sqls, m[1])
|
||||
}
|
||||
}
|
||||
return sqls
|
||||
}
|
||||
|
||||
func TestStoreSQLUsesReferencesJSONColumn(t *testing.T) {
|
||||
src := readStoreSource(t)
|
||||
sqls := extractSQLLiterals(src)
|
||||
if len(sqls) == 0 {
|
||||
t.Fatal("no SQL literals found in store.go")
|
||||
}
|
||||
|
||||
var update, selectTurns string
|
||||
for _, sql := range sqls {
|
||||
upper := strings.ToUpper(sql)
|
||||
if strings.HasPrefix(strings.TrimSpace(upper), "UPDATE TURNS") {
|
||||
update = sql
|
||||
}
|
||||
if strings.Contains(upper, "SELECT") && strings.Contains(upper, "FROM TURNS") {
|
||||
selectTurns = sql
|
||||
}
|
||||
}
|
||||
if update == "" {
|
||||
t.Fatal("FinishTurn UPDATE turns statement not found")
|
||||
}
|
||||
if selectTurns == "" {
|
||||
t.Fatal("loadTurn SELECT FROM turns statement not found")
|
||||
}
|
||||
if !strings.Contains(update, "references_json=$5") {
|
||||
t.Error("FinishTurn UPDATE must write to references_json column")
|
||||
}
|
||||
if !strings.Contains(selectTurns, "answer,references_json,status") {
|
||||
t.Error("loadTurn SELECT must read references_json column")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreSQLAvoidsBareReferencesIdentifier(t *testing.T) {
|
||||
src := readStoreSource(t)
|
||||
|
||||
// Inside the actual SQL literals, the bare word `references` is a
|
||||
// reserved word in PostgreSQL 16 and must not appear as a column
|
||||
// identifier. The Go variable/field names are irrelevant here because
|
||||
// we only inspect SQL string literals.
|
||||
bare := regexp.MustCompile(`(?i)\breferences\b`)
|
||||
for _, sql := range extractSQLLiterals(src) {
|
||||
// references_json is fine; strip it before checking for the bare word.
|
||||
scrubbed := regexp.MustCompile(`(?i)\breferences_json\b`).ReplaceAllString(sql, "")
|
||||
if bare.MatchString(scrubbed) {
|
||||
t.Errorf("SQL literal uses reserved word `references` as a column name; use references_json:\n%s", sql)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreKeepsDomainFieldReferences(t *testing.T) {
|
||||
src := readStoreSource(t)
|
||||
|
||||
if !regexp.MustCompile(`turn\.References`).MatchString(src) {
|
||||
t.Error("store must keep using the domain field turn.References (API unchanged)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user