- 迁移 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 通过
153 lines
3.5 KiB
Go
153 lines
3.5 KiB
Go
package creator
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
)
|
|
|
|
// Creator migrations use their own history table. Phase-a and hub retain a
|
|
// shared schema_migration table for their schemas, but their numeric versions
|
|
// overlap with the creator migration files and must not suppress one another.
|
|
type SecretReference struct {
|
|
ID string
|
|
Provider string
|
|
}
|
|
|
|
type SecretBridge interface {
|
|
Store(context.Context, SecretReference, string, string) error
|
|
Delete(context.Context, SecretReference, string) error
|
|
}
|
|
|
|
type Store struct {
|
|
db *sql.DB
|
|
secrets SecretBridge
|
|
// Automatic writes for one execution account stay serialized while event
|
|
// ingestion and unrelated accounts remain concurrent.
|
|
automaticLocks sync.Map // map[string]*sync.Mutex
|
|
}
|
|
|
|
func Open(ctx context.Context, databaseURL string) (*Store, error) {
|
|
db, err := sql.Open("pgx", databaseURL)
|
|
if err != nil {
|
|
return nil, errors.New("open creator database")
|
|
}
|
|
db.SetMaxOpenConns(10)
|
|
db.SetMaxIdleConns(2)
|
|
db.SetConnMaxIdleTime(5 * time.Minute)
|
|
if err := db.PingContext(ctx); err != nil {
|
|
_ = db.Close()
|
|
return nil, errors.New("connect to creator database")
|
|
}
|
|
store := &Store{db: db}
|
|
return store, nil
|
|
}
|
|
|
|
func (s *Store) Close() error { return s.db.Close() }
|
|
|
|
func (s *Store) Ping(ctx context.Context) error { return s.db.PingContext(ctx) }
|
|
|
|
func (s *Store) SetSecretBridge(bridge SecretBridge) { s.secrets = bridge }
|
|
|
|
func (s *Store) automaticExecutionLock(accountID string) *sync.Mutex {
|
|
lock, _ := s.automaticLocks.LoadOrStore(accountID, &sync.Mutex{})
|
|
return lock.(*sync.Mutex)
|
|
}
|
|
|
|
func (s *Store) acquireAutomaticExecutionLock(ctx context.Context, accountID string) (func(), error) {
|
|
conn, err := s.db.Conn(ctx)
|
|
if err != nil {
|
|
return nil, databaseError(err)
|
|
}
|
|
if _, err := conn.ExecContext(ctx, `SELECT pg_advisory_lock(hashtextextended($1, 0))`, accountID); err != nil {
|
|
_ = conn.Close()
|
|
return nil, databaseError(err)
|
|
}
|
|
return func() { _ = conn.Close() }, nil
|
|
}
|
|
|
|
func newID(prefix string) string {
|
|
var bytes [12]byte
|
|
if _, err := rand.Read(bytes[:]); err != nil {
|
|
panic(fmt.Sprintf("generate creator id: %v", err))
|
|
}
|
|
return prefix + "-" + hex.EncodeToString(bytes[:])
|
|
}
|
|
|
|
func databaseError(err error) error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
var pgErr *pgconn.PgError
|
|
if errors.As(err, &pgErr) {
|
|
switch pgErr.Code {
|
|
case "23505":
|
|
return ErrConflict
|
|
case "23503", "23514", "22P02":
|
|
return ErrInvalid
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func rowError(err error) error {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return databaseError(err)
|
|
}
|
|
|
|
func jsonText(value any) (string, error) {
|
|
encoded, err := json.Marshal(value)
|
|
if err != nil {
|
|
return "", fmt.Errorf("encode creator json: %w", err)
|
|
}
|
|
return string(encoded), nil
|
|
}
|
|
|
|
func decodeStringList(encoded []byte) ([]string, error) {
|
|
if len(encoded) == 0 {
|
|
return []string{}, nil
|
|
}
|
|
var result []string
|
|
if err := json.Unmarshal(encoded, &result); err != nil {
|
|
return nil, fmt.Errorf("decode creator string list: %w", err)
|
|
}
|
|
if result == nil {
|
|
result = []string{}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func nullableString(value string) any {
|
|
if value == "" {
|
|
return nil
|
|
}
|
|
return value
|
|
}
|
|
|
|
func nullableTime(value sql.NullTime) *time.Time {
|
|
if !value.Valid {
|
|
return nil
|
|
}
|
|
result := value.Time.UTC()
|
|
return &result
|
|
}
|
|
|
|
func nullableInt64(value sql.NullInt64) *int64 {
|
|
if !value.Valid {
|
|
return nil
|
|
}
|
|
result := value.Int64
|
|
return &result
|
|
}
|