Files
creator-hub/internal/environment/legacy_browser_version_test.go
T
rogee 0879d9fd25 refactor(schema): 迁移 043 schema 收敛——42→19 张表,统一迁移 runner
- 迁移 043 一次到位:runtime_instance/environment_binding 并入 browser_env(账号即环境)、
  credential_reference 并入 social_account、work_source/metric_plan 并入 creator_work、
  sync 租约并入 creator_collection_checkpoint(kind='sync')、audit_event 瘦列、
  creator_settings 去转写列、TRUNCATE 账号域(竞品采集数据无损保留)
- 统一迁移 runner:environment.Open 串起 001 账号基座 + hub 链 + 038 creator 版本回填
  (v+1000) + creator 1017-1042 + 043;account/creator Open 不再迁移
- 三域 store SQL 全量适配:CreateBoundEnv 就绪门禁、SetRuntimeCleanupPending fence
  语义(未知代 MissingRuntimeID 允许登记并释放活跃实例)、metric_plan 列并入 work、
  checkpoint sync 租约、deletion 链路重写
- api 适配:envView 去 runtime_instance_id、audit filter 去任务列、browser_env 单表查询
- 测试:迁移 043 形态探针、迁移不可变映射新路径、legacy 迁移重放子测试重写、
  fence/出口生命周期/单来源 upsert 语义覆盖;CREATORHUB_POSTGRES_TEST_URL 全绿,
  environment 70%/creator 67.5% 覆盖率,tsc 通过
2026-09-28 19:48:55 +08:00

78 lines
3.0 KiB
Go

package environment
import (
"context"
"regexp"
"strings"
"unicode/utf8"
)
// Legacy database tests still exercise migrations created before gateway-owned
// browser selection. These helpers are test-only; production has no such model
// or management methods.
type BrowserVersion struct {
Version string
BrowserPath string
Note string
Enabled bool
}
var legacyBrowserVersionPattern = regexp.MustCompile(`^[0-9][A-Za-z0-9.+~-]{0,63}$`)
func (s *Store) CreateBrowserVersion(ctx context.Context, version BrowserVersion) error {
version.Version, version.BrowserPath, version.Note = strings.TrimSpace(version.Version), strings.TrimSpace(version.BrowserPath), strings.TrimSpace(version.Note)
if !legacyBrowserVersionPattern.MatchString(version.Version) || !strings.HasPrefix(version.BrowserPath, "/") || utf8.RuneCountInString(version.Note) > 200 {
return ErrInvalid
}
_, err := s.db.ExecContext(ctx, `INSERT INTO browser_version (version, browser_path, note, enabled) VALUES ($1, $2, $3, $4)`, version.Version, version.BrowserPath, version.Note, version.Enabled)
return publicDatabaseError(err)
}
func (s *Store) UpdateBrowserVersion(ctx context.Context, version BrowserVersion) error {
version.BrowserPath, version.Note = strings.TrimSpace(version.BrowserPath), strings.TrimSpace(version.Note)
if !legacyBrowserVersionPattern.MatchString(version.Version) || !strings.HasPrefix(version.BrowserPath, "/") || utf8.RuneCountInString(version.Note) > 200 {
return ErrInvalid
}
var updated string
err := s.db.QueryRowContext(ctx, `UPDATE browser_version SET browser_path = $2, note = $3, enabled = $4, updated_at = now() WHERE version = $1 RETURNING version`, version.Version, version.BrowserPath, version.Note, version.Enabled).Scan(&updated)
return rowError(err)
}
func (s *Store) BrowserPath(ctx context.Context, version string) (string, error) {
if !legacyBrowserVersionPattern.MatchString(version) {
return "", ErrInvalid
}
var path string
if err := s.db.QueryRowContext(ctx, `SELECT browser_path FROM browser_version WHERE version = $1 AND enabled`, version).Scan(&path); err != nil {
return "", rowError(err)
}
return path, nil
}
func (s *Store) DeleteBrowserVersion(ctx context.Context, version string) error {
if !legacyBrowserVersionPattern.MatchString(version) {
return ErrInvalid
}
var deleted string
if err := s.db.QueryRowContext(ctx, `DELETE FROM browser_version WHERE version = $1 RETURNING version`, version).Scan(&deleted); err != nil {
return rowError(err)
}
return nil
}
func (s *Store) UpgradeEnv(ctx context.Context, alias, version string) error {
if !legacyBrowserVersionPattern.MatchString(version) {
return ErrInvalid
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer tx.Rollback()
var updated string
if err := tx.QueryRowContext(ctx, `UPDATE browser_env SET browser_version = $2, version = version + 1 WHERE alias = $1 RETURNING alias`, alias, version).Scan(&updated); err != nil {
return rowError(err)
}
return commitHub(tx)
}