Files
miti99bot/cmd/migrate_cf_data/convert_value.go
T
tiennm99 a8ed67a0a3 refactor: audit-driven hygiene pass across modules and infra
Concurrency
- lolschedule: serialize subscriber Get→mutate→Put via state.subscribersMu;
  the single-slot list was previously losing writes under concurrent
  /lolschedule_subscribe.
- trading: PriceClient memoises its default *http.Client so /trade_stats
  reuses TLS connections across held tickers.

Observability
- server/log_middleware: defer the req log line and recover panics so a
  panicking cron handler still emits the structured req entry CloudWatch
  filters on for 5xx alerting.
- server/router (cron): inner recover with cron-name context captures the
  panicking job before the middleware's safety net does.
- telegram/webhook: rune-safe truncation in dispatch logs — Vietnamese,
  Korean, and emoji previews no longer ship as garbled bytes.
- lolschedule/api_client: same rune-safe fix for error-body log truncation.
- telegram/webhook: gate the post-recover WriteHeader(200) so a panicking
  handler that already touched w doesn't trigger superfluous-WriteHeader.

Correctness
- twentyq: clearGame error during solved-relaunch is logged instead of
  silently swallowed (was a permanent deadlock vector on KV failure).
- misc /mstats: KV read failure replies "Could not load stats. Try again
  later." to the user instead of returning into the dispatcher; matches the
  pattern other modules use.
- migrate_cf_data trading-audit-dump: surface f.Close error so a truncated
  JSONL never passes silently as a complete audit dump.

Operator ergonomics
- migrate_cf_data (all 4 subcommands): signal.NotifyContext for SIGINT /
  SIGTERM. Ctrl-C mid-Scan now propagates cleanly instead of leaving a
  half-converted DynamoDB table.
- ai/ratelimit: doc the Lambda-recycle memory bound to match keylock.Map
  so a future reviewer doesn't re-flag the unbounded map.

I/O-changing (user-approved)
- lolschedule daily push auto-prunes subscribers whose Telegram error
  matches a terminal marker (blocked / deactivated / chat gone). Transient
  errors keep the chat on the list. Subscribe message updated to mention
  the auto-cleanup.
- twentyq seed pool grown 50 → 178; repeat-collision threshold moves from
  ~9 plays to ~17 (birthday paradox).
- util /info flipped Public → Protected — chat/thread/sender IDs are no
  longer enumerable by every group member.
- cmd/server WriteTimeout 6min → 75s (cron 60s + 15s slack). No-op on
  Lambda; matters only for local non-Lambda runs.
- webhook + cron rejection paths drop response bodies (no fingerprintable
  text for internet scanners hitting the public Function URL). Status
  codes preserved for CloudWatch metrics; structured log lines carry the
  rejection reason for operator triage.

Tests added: TestTruncateRunes, TestRunDailyPush_PrunesDeadSubscribers,
TestIsTerminalSendError, TestInfo_DeniedToNonOwner,
TestInfo_DeniedToChannelMessageNoFrom, plus owner-allowed counterparts.
2026-05-16 13:35:00 +07:00

116 lines
3.4 KiB
Go

// One-shot rewrite of the table's `value` attribute from Binary (legacy
// shape) to String (current shape). Operator-elective; needed once after the
// runtime swap from MemberB to MemberS in internal/storage/dynamodb_kv.go.
package main
import (
"context"
"flag"
"fmt"
"os"
"strconv"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// signalContext is defined in main.go and gives every subcommand a SIGINT /
// SIGTERM-cancellable context — Ctrl-C mid-scan now propagates as a clean
// context error instead of leaving a half-converted table.
//
// (signature mirrors signal.NotifyContext for documentation purposes; no
// re-declaration here, just a pointer for future readers.)
func runConvertValueToString(args []string) error {
fs := flag.NewFlagSet("convert-value-to-string", flag.ExitOnError)
table := fs.String("table", "", "target DynamoDB table (required)")
dryRun := fs.Bool("dry-run", false, "log actions but do not write")
if err := fs.Parse(args); err != nil {
return err
}
if *table == "" {
return fmt.Errorf("--table is required")
}
ctx, cancel := signalContext()
defer cancel()
cfg, err := awsconfig.LoadDefaultConfig(ctx)
if err != nil {
return fmt.Errorf("aws config: %w", err)
}
client := dynamodb.NewFromConfig(cfg)
converted, alreadyString, skipped, failed := 0, 0, 0, 0
pager := dynamodb.NewScanPaginator(client, &dynamodb.ScanInput{TableName: aws.String(*table)})
for pager.HasMorePages() {
page, err := pager.NextPage(ctx)
if err != nil {
return fmt.Errorf("scan: %w", err)
}
for _, item := range page.Items {
pk, sk, ok := itemPKSK(item)
if !ok {
skipped++
continue
}
valAttr, ok := item["value"]
if !ok {
skipped++
continue
}
binAttr, isBinary := valAttr.(*types.AttributeValueMemberB)
if !isBinary {
alreadyString++
continue
}
if *dryRun {
fmt.Printf(" DRY-RUN would convert pk=%s sk=%s len=%d\n", pk, sk, len(binAttr.Value))
converted++
continue
}
if err := putAsString(ctx, client, *table, pk, sk, binAttr.Value); err != nil {
fmt.Fprintf(os.Stderr, " put %s/%s: %v\n", pk, sk, err)
failed++
continue
}
converted++
}
}
fmt.Printf("\nconvert-value-to-string report\n")
fmt.Printf(" converted (B → S): %d\n", converted)
fmt.Printf(" already String: %d\n", alreadyString)
fmt.Printf(" skipped (no pk/sk/value): %d\n", skipped)
fmt.Printf(" failed: %d\n", failed)
return nil
}
func itemPKSK(item map[string]types.AttributeValue) (string, string, bool) {
pkAttr, ok := item["pk"].(*types.AttributeValueMemberS)
if !ok {
return "", "", false
}
skAttr, ok := item["sk"].(*types.AttributeValueMemberS)
if !ok {
return "", "", false
}
return pkAttr.Value, skAttr.Value, true
}
func putAsString(ctx context.Context, client *dynamodb.Client, table, pk, sk string, val []byte) error {
_, err := client.PutItem(ctx, &dynamodb.PutItemInput{
TableName: aws.String(table),
Item: map[string]types.AttributeValue{
"pk": &types.AttributeValueMemberS{Value: pk},
"sk": &types.AttributeValueMemberS{Value: sk},
"value": &types.AttributeValueMemberS{Value: string(val)},
"updatedAt": &types.AttributeValueMemberN{Value: strconv.FormatInt(time.Now().UTC().UnixNano(), 10)},
},
})
return err
}