CreateWorkspace mints a workspace against the pool. Callers already inside a transaction want createWorkspaceQ instead. With WithPlanLimit() the mint runs in its own transaction and is refused with a *PlanLimitError when the owner is already at their workspaces limit, counted under the owner lock (
(input models.WorkspaceCreate, opts ...MintOption)
| 98 | // the import asking for the owner lock at its end. With one order, the create |
| 99 | // waits on the slug holding nothing, and the import commits. |
| 100 | func (s *Store) CreateWorkspace(input models.WorkspaceCreate, opts ...MintOption) (*models.Workspace, error) { |
| 101 | mo := resolveMintOptions(opts) |
| 102 | if !mo.planLimit { |
| 103 | return s.createWorkspaceQ(s.db, input, mo.preserveSlug) |
| 104 | } |
| 105 | tx, err := s.db.Begin() |
| 106 | if err != nil { |
| 107 | return nil, fmt.Errorf("create workspace: begin: %w", err) |
| 108 | } |
| 109 | defer tx.Rollback() |
| 110 | ws, err := s.createWorkspaceQ(tx, input, mo.preserveSlug) |
| 111 | if err != nil { |
| 112 | return nil, err |
| 113 | } |
| 114 | if err := s.enforceUserLimitTx(tx, input.OwnerID, "workspaces", 1); err != nil { |
| 115 | return nil, err |
| 116 | } |
| 117 | if err := tx.Commit(); err != nil { |
| 118 | return nil, fmt.Errorf("create workspace: commit: %w", err) |
| 119 | } |
| 120 | return ws, nil |
| 121 | } |
| 122 | |
| 123 | // workspaceSlugProbedHook, when set by a test, runs between the slug probe and |
| 124 | // the INSERT: the gap a concurrent create of the same name lands in (BUG-3307). |