feat(watchlist): add company identity catalog

Embed a pinned FinanceDatabase equity snapshot, add authenticated company search and identity-based follows, preserve legacy assets, and map selected Yahoo symbols for collection.

Refs #19

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:08:22 +01:00
parent 1123360914
commit d65222169a
50 changed files with 49897 additions and 193 deletions

View File

@ -0,0 +1,3 @@
-- No destructive downgrade: removing company identity would collapse independent
-- catalogue and legacy follows. Restore a backup or explicitly reconcile first.
DO $$ BEGIN RAISE EXCEPTION '000017 cannot be reverted without explicit company/legacy reconciliation'; END $$;

View File

@ -0,0 +1,13 @@
BEGIN;
ALTER TABLE user_assets ADD COLUMN company_id TEXT NULL,
ADD COLUMN yahoo_symbol TEXT NULL,
ADD COLUMN company_exchange TEXT NULL,
ADD COLUMN company_country TEXT NULL,
ADD COLUMN company_mic TEXT NULL;
CREATE UNIQUE INDEX user_assets_company_unique ON user_assets(user_id,company_id) WHERE company_id IS NOT NULL;
-- Legacy identities remain independent of catalogue follows, even for the same FIGI.
DROP INDEX IF EXISTS user_assets_legacy_symbol_unique;
DROP INDEX IF EXISTS user_assets_figi_unique;
CREATE UNIQUE INDEX user_assets_legacy_symbol_unique ON user_assets(user_id,symbol) WHERE figi IS NULL AND company_id IS NULL;
CREATE UNIQUE INDEX user_assets_figi_unique ON user_assets(user_id,figi) WHERE figi IS NOT NULL AND company_id IS NULL;
COMMIT;

View File

@ -0,0 +1,126 @@
package migrations
import (
"database/sql"
"fmt"
"os"
"strings"
"testing"
"time"
"github.com/lib/pq"
)
func TestTicket19CompanyCatalogMigrationIsAdditiveAndKeepsLegacyIndexAssetsPostgres(t *testing.T) {
dsn := os.Getenv("TEST_DATABASE_URL")
if dsn == "" {
if os.Getenv("TEST_DATABASE_REQUIRED") == "1" {
t.Fatal("TEST_DATABASE_REQUIRED=1 but TEST_DATABASE_URL is empty")
}
t.Skip("TEST_DATABASE_URL absent: isolated PostgreSQL required")
}
db, err := sql.Open("postgres", dsn)
if err != nil {
t.Fatal(err)
}
db.SetMaxOpenConns(1)
schema := fmt.Sprintf("ticket19_company_migration_%d", time.Now().UnixNano())
if _, err := db.Exec(`CREATE SCHEMA ` + pq.QuoteIdentifier(schema)); err != nil {
db.Close()
t.Fatal(err)
}
if _, err := db.Exec(`SET search_path TO ` + pq.QuoteIdentifier(schema)); err != nil {
_, _ = db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`)
db.Close()
t.Fatal(err)
}
t.Cleanup(func() {
_, _ = db.Exec(`DROP SCHEMA ` + pq.QuoteIdentifier(schema) + ` CASCADE`)
_ = db.Close()
})
const owner = "00000000-0000-0000-0000-000000000191"
for _, statement := range []string{
`CREATE TABLE users (id UUID PRIMARY KEY)`,
`CREATE TABLE user_assets (id UUID PRIMARY KEY, user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, symbol VARCHAR(20) NOT NULL, name TEXT NOT NULL DEFAULT '', figi TEXT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW())`,
`CREATE UNIQUE INDEX user_assets_legacy_symbol_unique ON user_assets(user_id,symbol) WHERE figi IS NULL`,
`CREATE UNIQUE INDEX user_assets_figi_unique ON user_assets(user_id,figi) WHERE figi IS NOT NULL`,
`INSERT INTO users(id) VALUES ('` + owner + `')`,
`INSERT INTO users(id) VALUES ('00000000-0000-0000-0000-000000000193')`,
`INSERT INTO user_assets(id,user_id,symbol,name,figi) VALUES ('00000000-0000-0000-0000-000000000191','` + owner + `','LEGACY','Historic company asset',NULL)`,
`INSERT INTO user_assets(id,user_id,symbol,name,figi) VALUES ('00000000-0000-0000-0000-000000000192','` + owner + `','SPX','Historic index asset','BBG000SPX001')`,
} {
if _, err := db.Exec(statement); err != nil {
t.Fatal(err)
}
}
up, err := os.ReadFile("000017_company_catalog.up.sql")
if err != nil {
t.Fatalf("read additive company-catalog migration 000017: %v", err)
}
upSQL := strings.ToLower(string(up))
if !strings.Contains(upSQL, "company_id") {
t.Fatalf("migration must add string company_id for new catalog-backed follow rows: %s", upSQL)
}
for _, destructive := range []string{"delete from user_assets", "truncate user_assets", "drop table user_assets", "create table companies"} {
if strings.Contains(upSQL, destructive) {
t.Errorf("company-catalog migration must preserve assets and avoid a dynamic companies table; found %q", destructive)
}
}
if _, err := db.Exec(string(up)); err != nil {
t.Fatalf("apply company-catalog migration to legacy schema: %v", err)
}
var companyIDType string
if err := db.QueryRow(`SELECT data_type FROM information_schema.columns WHERE table_schema=$1 AND table_name='user_assets' AND column_name='company_id'`, schema).Scan(&companyIDType); err != nil {
t.Fatalf("read company_id column type: %v", err)
}
if companyIDType != "text" && companyIDType != "character varying" {
t.Fatalf("company_id data type=%q; want an opaque string, not UUID", companyIDType)
}
rows, err := db.Query(`SELECT id::text,user_id::text,symbol,name,figi,company_id FROM user_assets ORDER BY id`)
if err != nil {
t.Fatal(err)
}
type assetRow struct {
id, userID, symbol, name string
figi, companyID sql.NullString
}
var got []assetRow
for rows.Next() {
var row assetRow
if err := rows.Scan(&row.id, &row.userID, &row.symbol, &row.name, &row.figi, &row.companyID); err != nil {
rows.Close()
t.Fatal(err)
}
got = append(got, row)
}
if err := rows.Err(); err != nil {
rows.Close()
t.Fatal(err)
}
rows.Close()
if len(got) != 2 {
t.Fatalf("legacy/index asset rows=%d, want both rows preserved: %+v", len(got), got)
}
if got[0].id != "00000000-0000-0000-0000-000000000191" || got[0].userID != owner || got[0].symbol != "LEGACY" || got[0].name != "Historic company asset" || got[0].figi.Valid || got[0].companyID.Valid {
t.Errorf("legacy asset was changed by migration: %+v", got[0])
}
if got[1].id != "00000000-0000-0000-0000-000000000192" || got[1].userID != owner || got[1].symbol != "SPX" || got[1].name != "Historic index asset" || !got[1].figi.Valid || got[1].figi.String != "BBG000SPX001" || got[1].companyID.Valid {
t.Errorf("historical index asset was changed or converted by migration: %+v", got[1])
}
for i, identity := range []string{"isin:KYG070341048", "figi:BBG000BBJQV0"} {
id := fmt.Sprintf("00000000-0000-0000-0000-00000000019%d", 3+i)
symbol := fmt.Sprintf("catalog-%d", i)
if _, err := db.Exec(`INSERT INTO user_assets(id,user_id,symbol,name,figi,company_id) VALUES ($1,$2,$3,'catalog asset',NULL,$4)`, id, owner, symbol, identity); err != nil {
t.Fatalf("insert opaque company identity %q: %v", identity, err)
}
}
if _, err := db.Exec(`INSERT INTO user_assets(id,user_id,symbol,name,figi,company_id) VALUES ('00000000-0000-0000-0000-000000000195',$1,'duplicate','duplicate catalog identity',NULL,'isin:KYG070341048')`, owner); err == nil {
t.Fatal("duplicate owner/company_id must be rejected for idempotent application upserts")
}
if _, err := db.Exec(`INSERT INTO user_assets(id,user_id,symbol,name,figi,company_id) VALUES ('00000000-0000-0000-0000-000000000196','00000000-0000-0000-0000-000000000193','other-owner','same company other owner',NULL,'isin:KYG070341048')`); err != nil {
t.Fatalf("same company identity should be allowed for another owner: %v", err)
}
}