@@ -23,8 +23,12 @@ type PostgresDB struct {
2323
2424// Init initializes the PostgreSQL database connection pool
2525func (p * PostgresDB ) Init () error {
26- // Parse PostgreSQL connection string if provided
27- dbHostEnv := os .Getenv ("DB_HOST" )
26+ // Parse PostgreSQL connection string if provided.
27+ // Prefer DB_HOST; fall back to DATABASE_URL (common in hosting / copied .env files).
28+ dbHostEnv := strings .TrimSpace (os .Getenv ("DB_HOST" ))
29+ if dbHostEnv == "" {
30+ dbHostEnv = strings .TrimSpace (os .Getenv ("DATABASE_URL" ))
31+ }
2832 var connStr string
2933
3034 if strings .HasPrefix (dbHostEnv , "postgresql://" ) || strings .HasPrefix (dbHostEnv , "postgres://" ) {
@@ -387,6 +391,10 @@ func (p *PostgresDB) InitSchema() error {
387391 return fmt .Errorf ("failed to migrate scans.result_url: %v" , err )
388392 }
389393
394+ // Migrate scan_artifacts table: add module and category columns
395+ _ , _ = p .pool .Exec (p .ctx , `ALTER TABLE scan_artifacts ADD COLUMN IF NOT EXISTS module TEXT` )
396+ _ , _ = p .pool .Exec (p .ctx , `ALTER TABLE scan_artifacts ADD COLUMN IF NOT EXISTS category TEXT` )
397+
390398 // Deduplicate legacy artifact rows before enforcing uniqueness.
391399 _ , _ = p .pool .Exec (p .ctx , `
392400 DELETE FROM scan_artifacts a
@@ -1477,17 +1485,19 @@ func (p *PostgresDB) AppendScanArtifact(artifact *ScanArtifact) error {
14771485 }
14781486 _ , err := p .pool .Exec (p .ctx , `
14791487 INSERT INTO scan_artifacts (
1480- scan_id, file_name, local_path, r2_key, public_url, size_bytes, line_count, content_type
1481- ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
1488+ scan_id, file_name, local_path, r2_key, public_url, size_bytes, line_count, content_type, module, category
1489+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10 )
14821490 ON CONFLICT (scan_id, r2_key) DO UPDATE SET
14831491 file_name=EXCLUDED.file_name,
14841492 local_path=EXCLUDED.local_path,
14851493 public_url=EXCLUDED.public_url,
14861494 size_bytes=EXCLUDED.size_bytes,
14871495 line_count=EXCLUDED.line_count,
14881496 content_type=EXCLUDED.content_type,
1497+ module=EXCLUDED.module,
1498+ category=EXCLUDED.category,
14891499 created_at=NOW()
1490- ` , artifact .ScanID , artifact .FileName , artifact .LocalPath , artifact .R2Key , artifact .PublicURL , artifact .SizeBytes , artifact .LineCount , artifact .ContentType )
1500+ ` , artifact .ScanID , artifact .FileName , artifact .LocalPath , artifact .R2Key , artifact .PublicURL , artifact .SizeBytes , artifact .LineCount , artifact .ContentType , artifact . Module , artifact . Category )
14911501 if err != nil {
14921502 return fmt .Errorf ("failed to append scan artifact: %v" , err )
14931503 }
@@ -1499,7 +1509,7 @@ func (p *PostgresDB) ListScanArtifacts(scanID string) ([]*ScanArtifact, error) {
14991509 rows , err := p .pool .Query (p .ctx , `
15001510 SELECT id, scan_id, COALESCE(file_name, ''), COALESCE(local_path, ''), COALESCE(r2_key, ''),
15011511 COALESCE(public_url, ''), COALESCE(size_bytes, 0), COALESCE(line_count, 0),
1502- COALESCE(content_type, ''), created_at
1512+ COALESCE(content_type, ''), COALESCE(module, ''), COALESCE(category, ''), created_at
15031513 FROM scan_artifacts
15041514 WHERE scan_id = $1
15051515 ORDER BY created_at DESC, id DESC
@@ -1512,7 +1522,7 @@ func (p *PostgresDB) ListScanArtifacts(scanID string) ([]*ScanArtifact, error) {
15121522 var out []* ScanArtifact
15131523 for rows .Next () {
15141524 var a ScanArtifact
1515- if err := rows .Scan (& a .ID , & a .ScanID , & a .FileName , & a .LocalPath , & a .R2Key , & a .PublicURL , & a .SizeBytes , & a .LineCount , & a .ContentType , & a .CreatedAt ); err != nil {
1525+ if err := rows .Scan (& a .ID , & a .ScanID , & a .FileName , & a .LocalPath , & a .R2Key , & a .PublicURL , & a .SizeBytes , & a .LineCount , & a .ContentType , & a .Module , & a . Category , & a . CreatedAt ); err != nil {
15161526 return nil , fmt .Errorf ("failed to scan artifact row: %v" , err )
15171527 }
15181528 out = append (out , & a )
0 commit comments