feat: track DingTalk delivery outcomes
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
-- 000004_delivery_audit down migration
|
||||
-- Drop in reverse order: index first, then columns.
|
||||
|
||||
DROP INDEX IF EXISTS idx_messages_tenant_status_updated;
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP COLUMN IF EXISTS failure_code;
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP COLUMN IF EXISTS provider_message_id;
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP COLUMN IF EXISTS delivery_method;
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP COLUMN IF EXISTS delivered_at;
|
||||
@@ -0,0 +1,20 @@
|
||||
-- 000004_delivery_audit: delivery audit columns on messages
|
||||
-- Adds delivered_at, delivery_method, provider_message_id, failure_code
|
||||
-- with a tenant+status+updated_at query index.
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN IF NOT EXISTS delivered_at TIMESTAMPTZ;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN IF NOT EXISTS delivery_method TEXT NOT NULL DEFAULT ''
|
||||
CHECK (delivery_method IN ('', 'card', 'markdown'));
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN IF NOT EXISTS provider_message_id TEXT NOT NULL DEFAULT '';
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN IF NOT EXISTS failure_code TEXT NOT NULL DEFAULT ''
|
||||
CHECK (failure_code IN ('', 'channel_delivery_failed'));
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_tenant_status_updated
|
||||
ON messages (tenant_key, status, updated_at DESC);
|
||||
@@ -0,0 +1,200 @@
|
||||
// Package migrations_test contains source-level contract tests for the
|
||||
// 000004_delivery_audit migration. No real database connection is used.
|
||||
package migrations_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func read004UpMigration(t *testing.T) string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(filepath.Join(".", "000004_delivery_audit.up.sql"))
|
||||
if err != nil {
|
||||
t.Fatalf("read up migration: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func read004DownMigration(t *testing.T) string {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(filepath.Join(".", "000004_delivery_audit.down.sql"))
|
||||
if err != nil {
|
||||
t.Fatalf("read down migration: %v", err)
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// TestUp004UsesIfNotExists ensures ALTER TABLE uses IF NOT EXISTS for new columns.
|
||||
func TestUp004UsesIfNotExists(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
if !strings.Contains(upper, "ADD COLUMN IF NOT EXISTS") {
|
||||
t.Error("up migration must use ADD COLUMN IF NOT EXISTS for idempotency")
|
||||
}
|
||||
if !strings.Contains(upper, "CREATE INDEX IF NOT EXISTS") {
|
||||
t.Error("up migration must use CREATE INDEX IF NOT EXISTS for idempotency")
|
||||
}
|
||||
|
||||
// At least 4 new columns: delivered_at, delivery_method, provider_message_id, failure_code
|
||||
if got := countMatches(up, `(?i)ADD\s+COLUMN\s+IF\s+NOT\s+EXISTS`); got < 4 {
|
||||
t.Errorf("expected at least 4 ADD COLUMN IF NOT EXISTS, got %d", got)
|
||||
}
|
||||
// At least 1 new index
|
||||
if got := countMatches(up, `(?i)CREATE\s+INDEX\s+IF\s+NOT\s+EXISTS`); got < 1 {
|
||||
t.Errorf("expected at least 1 CREATE INDEX IF NOT EXISTS, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004AddsDeliveryColumns ensures all 4 delivery audit columns are added to messages.
|
||||
func TestUp004AddsDeliveryColumns(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
columns := []string{
|
||||
"DELIVERED_AT TIMESTAMPTZ",
|
||||
"DELIVERY_METHOD TEXT NOT NULL DEFAULT ''",
|
||||
"PROVIDER_MESSAGE_ID TEXT NOT NULL DEFAULT ''",
|
||||
"FAILURE_CODE TEXT NOT NULL DEFAULT ''",
|
||||
}
|
||||
for _, col := range columns {
|
||||
if !strings.Contains(upper, col) {
|
||||
t.Errorf("up migration must add column %q", col)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004DeliveryMethodCheck ensures delivery_method CHECK constraint.
|
||||
func TestUp004DeliveryMethodCheck(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
// delivery_method must have CHECK constraint
|
||||
if !strings.Contains(upper, "DELIVERY_METHOD") {
|
||||
t.Fatal("up migration must add delivery_method column")
|
||||
}
|
||||
// CHECK (delivery_method IN ('', 'card', 'markdown'))
|
||||
if !strings.Contains(upper, "CARD") || !strings.Contains(upper, "MARKDOWN") {
|
||||
t.Error("delivery_method CHECK must include 'card' and 'markdown'")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUp004FailureCodeCheck(t *testing.T) {
|
||||
up := strings.ToUpper(read004UpMigration(t))
|
||||
if !strings.Contains(up, "CHECK (FAILURE_CODE IN ('', 'CHANNEL_DELIVERY_FAILED'))") {
|
||||
t.Error("failure_code must be constrained to safe enumerated values")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004AddsTenantStatusUpdatedAtIndex ensures the query index is created.
|
||||
func TestUp004AddsTenantStatusUpdatedAtIndex(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
// Must have an index on (tenant_key, status, updated_at DESC)
|
||||
hasDesired := strings.Contains(upper, "TENANT_KEY") &&
|
||||
strings.Contains(upper, "STATUS") &&
|
||||
strings.Contains(upper, "UPDATED_AT") &&
|
||||
strings.Contains(upper, "DESC")
|
||||
if !hasDesired {
|
||||
t.Error("up migration must create index on (tenant_key, status, updated_at DESC)")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004NoForbiddenColumns ensures no secrets/protocol columns are introduced.
|
||||
func TestUp004NoForbiddenColumns(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
forbidden := []string{
|
||||
"RAW_PAYLOAD", "SESSIONWEBHOOK", "DOWNLOAD_CODE",
|
||||
"DATAURI", "DATA_URI", "TOKEN", "RAW_RESPONSE", "RAW_ERROR",
|
||||
"PROVIDER_RESPONSE", "API_RESPONSE",
|
||||
}
|
||||
for _, col := range forbidden {
|
||||
if strings.Contains(upper, col) {
|
||||
t.Errorf("migration must not contain forbidden column %q", col)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004DoesNotModifyOtherTables ensures migration only touches messages.
|
||||
func TestUp004DoesNotModifyOtherTables(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
upper := strings.ToUpper(up)
|
||||
|
||||
// Must not create/drop turns, session_mappings, channel_actors, channel_conversations
|
||||
if strings.Contains(upper, "CREATE TABLE") {
|
||||
t.Error("migration 004 must not CREATE TABLE")
|
||||
}
|
||||
if strings.Contains(upper, "DROP TABLE") {
|
||||
t.Error("migration 004 must not DROP TABLE")
|
||||
}
|
||||
if strings.Contains(upper, "ALTER TABLE TURNS") {
|
||||
t.Error("migration 004 must not ALTER TABLE turns")
|
||||
}
|
||||
if strings.Contains(upper, "ALTER TABLE SESSION_MAPPINGS") {
|
||||
t.Error("migration 004 must not ALTER TABLE session_mappings")
|
||||
}
|
||||
if strings.Contains(upper, "ALTER TABLE CHANNEL_ACTORS") {
|
||||
t.Error("migration 004 must not ALTER TABLE channel_actors")
|
||||
}
|
||||
if strings.Contains(upper, "ALTER TABLE CHANNEL_CONVERSATIONS") {
|
||||
t.Error("migration 004 must not ALTER TABLE channel_conversations")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDown004DropsDeliveryColumns ensures down drops columns in reverse order.
|
||||
func TestDown004DropsDeliveryColumns(t *testing.T) {
|
||||
down := read004DownMigration(t)
|
||||
upper := strings.ToUpper(down)
|
||||
|
||||
columns := []string{
|
||||
"FAILURE_CODE",
|
||||
"PROVIDER_MESSAGE_ID",
|
||||
"DELIVERY_METHOD",
|
||||
"DELIVERED_AT",
|
||||
}
|
||||
for _, col := range columns {
|
||||
if !strings.Contains(upper, "DROP COLUMN IF EXISTS "+col) {
|
||||
t.Errorf("down migration must drop %s with IF EXISTS", col)
|
||||
}
|
||||
}
|
||||
|
||||
// Must drop the index too
|
||||
if !strings.Contains(upper, "DROP INDEX IF EXISTS") {
|
||||
t.Error("down migration must drop the delivery audit index")
|
||||
}
|
||||
}
|
||||
|
||||
// TestDown004DoesNotDropBaselineTable ensures down does not drop messages table.
|
||||
func TestDown004DoesNotDropBaselineTable(t *testing.T) {
|
||||
down := read004DownMigration(t)
|
||||
upper := strings.ToUpper(down)
|
||||
|
||||
if strings.Contains(upper, "DROP TABLE IF EXISTS MESSAGES") {
|
||||
t.Error("down migration must not drop the baseline messages table")
|
||||
}
|
||||
}
|
||||
|
||||
// TestUp004MessagesNoReservedWords ensures no bare reserved words in new columns.
|
||||
func TestUp004MessagesNoReservedWords(t *testing.T) {
|
||||
up := read004UpMigration(t)
|
||||
|
||||
// Check for bare "references" as column name
|
||||
bareRefs := regexp.MustCompile(`(?im)^\s*REFERENCES\s+`)
|
||||
if bareRefs.MatchString(up) {
|
||||
lines := strings.Split(up, "\n")
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
upperLine := strings.ToUpper(trimmed)
|
||||
if strings.HasPrefix(upperLine, "REFERENCES ") && !strings.Contains(upperLine, "FOREIGN KEY") {
|
||||
t.Errorf("reserved word 'references' used as column name: %s", trimmed)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user