| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostops" |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | // withAutoAppSQL is used by the explicit generated adapters on existing Store |
| 12 | // mutation methods. Direct db writes remain refused. A future Store method |
| 13 | // receives no adapter until its scope and route fixture are reviewed. |
| 14 | func (s *Store) withAutoAppSQL(ctx context.Context, accountID int64, app, action string, call func(*Store) error) error { |
| 15 | if app == "" || s.gate == nil || s.bound != nil { |
| 16 | return call(s) |
| 17 | } |
| 18 | if s.outerInsideView() { |
| 19 | return errOuterStoreInView |
| 20 | } |
| 21 | if s.claimSQLAdmit == nil || s.claimBucket == nil { |
| 22 | return hostops.ErrContract |
| 23 | } |
| 24 | acct, err := s.AccountByID(accountID) |
| 25 | if err != nil { |
| 26 | return err |
| 27 | } |
| 28 | if acct == nil { |
| 29 | return hostops.ErrIdentity |
| 30 | } |
| 31 | var sqlErr error |
| 32 | err = s.claimSQLAdmit(ctx, acct.Sqid, app, action, func(pctx context.Context) error { |
| 33 | a, err := hostops.ReadCanonicalAdmission(pctx, s.claimBucket, acct.Sqid, app) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | scope := claimSQLScope{AccountID: accountID, App: app, AppID: a.Value.AppID, Mode: claimModeEffect} |
| 38 | sqlErr = s.WithAppMutation(pctx, []sqlGrant{permitGrant(pctx, s.claimBucket, acct.Sqid, scope)}, call) |
| 39 | // The bound view has released its connection before this returns. |
| 40 | // Any unproven failure remains uncertain until local SQL recovery fences it. |
| 41 | return sqlErr |
| 42 | }) |
| 43 | // The admission door normally returns the callback's error itself. |
| 44 | if err != nil && errors.Is(err, sqlErr) { |
| 45 | return err |
| 46 | } |
| 47 | return errors.Join(sqlErr, err) |
| 48 | } |
| 49 | func (s *Store) bindClaimSQLMutations(bucket release.Store) error { |
| 50 | if s.gate == nil { |
| 51 | return hostops.ErrContract |
| 52 | } |
| 53 | runner, ok := bucket.(hostops.SQLIngestRunner) |
| 54 | if !ok { |
| 55 | return hostops.ErrContract |
| 56 | } |
| 57 | s.claimBucket = bucket |
| 58 | database, err := s.sqlDatabaseID() |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | s.claimSQLAdmit = func(ctx context.Context, account, app, action string, fn func(context.Context) error) error { |
| 63 | return runner.RunSQLIngest(ctx, account, app, action, database, fn) |
| 64 | } |
| 65 | return nil |
| 66 | } |
| 67 | |