Skip to content

Conversation

@Boy132
Copy link
Member

@Boy132 Boy132 commented Sep 29, 2025

Closes #1754

@Boy132 Boy132 self-assigned this Sep 29, 2025
@coderabbitai
Copy link

coderabbitai bot commented Sep 29, 2025

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

Adds Server::ensureVariablesExist() to seed missing ServerVariable records from egg variables. The Startup and EditServer Filament pages now invoke this method (via a relationship callback or repeater config) before returning the viewable, ordered server-variables query.

Changes

Cohort / File(s) Summary of Changes
Server model: seeding helper
app/Models/Server.php
Added public function ensureVariablesExist(): void which iterates egg variables and creates missing ServerVariable records using firstOrCreate, initializing variable_value from the variable's default_value when creating.
Startup page: relationship callback that seeds
app/Filament/Server/Pages/Startup.php
Relationship callback for serverVariables now has signature (Builder $query, Server $server) and calls $server->ensureVariablesExist() before applying the user_viewable filter and ordering by variable.sort.
EditServer page: repeater uses seeding helper
app/Filament/Admin/Resources/Servers/Pages/EditServer.php
Removed explicit per-variable upsert loop; repeater configuration now invokes $server->ensureVariablesExist() to ensure required ServerVariable records exist, preserving ordering via orderByPowerJoins('variable.sort').

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User
  participant UI as Startup Page (Filament)
  participant S as Server
  participant M as Server::ensureVariablesExist()
  participant SV as ServerVariable Model
  participant DB as Database

  User->>UI: Open Startup page
  UI->>S: get current Server
  UI->>M: call ensureVariablesExist()
  M->>SV: for each EggVariable -> firstOrCreate({server_id, variable_id}, {...})
  SV->>DB: INSERT/SELECT (upsert)
  DB-->>SV: persisted records
  UI->>SV: query user_viewable ServerVariables ordered by variable.sort
  SV->>DB: SELECT with joins/filters
  DB-->>UI: Result set
  UI-->>User: Render repeater with variables
Loading
sequenceDiagram
  autonumber
  actor Admin
  participant UI as EditServer Page (Filament)
  participant S as Server
  participant M as Server::ensureVariablesExist()
  participant SV as ServerVariable Model
  participant DB as Database

  Admin->>UI: Open/Edit server variables form
  UI->>S: call ensureVariablesExist()
  M->>SV: create missing ServerVariable records via firstOrCreate
  SV->>DB: INSERT/SELECT (upsert)
  DB-->>SV: persisted records
  UI->>SV: query variables ordered by variable.sort
  DB-->>UI: Result set
  UI-->>Admin: Render form with seeded variables
Loading

Possibly related PRs

Pre-merge checks

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Title Check ✅ Passed The title clearly describes the main change by indicating that default variable values will now be set and missing variables will be created when viewing a server, which aligns directly with the core functionality introduced in the changeset. It succinctly captures both the propagation of default values and the creation of variables on view without extraneous details.
Linked Issues Check ✅ Passed The pull request implements a new ensureServerVariablesExist() method and integrates it into both the Startup page and the Admin resource to seed default EggVariable values into existing servers, thereby satisfying the core requirement from issue [#1754].
Out of Scope Changes Check ✅ Passed All modifications exclusively focus on creating or upserting server variable records and setting default values in line with the objectives of issue [#1754], and there are no unrelated or extraneous changes present in the diff.
Description Check ✅ Passed The description “Closes #1754” directly references the linked issue that this pull request addresses, confirming its relevance to the changeset.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c35977 and f7e9f20.

📒 Files selected for processing (3)
  • app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1 hunks)
  • app/Filament/Server/Pages/Startup.php (1 hunks)
  • app/Models/Server.php (1 hunks)

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (3)
app/Filament/Server/Pages/Startup.php (2)

105-113: Guard against null defaults to keep variable_value stringy.

If both server_value and default_value are null, you’ll persist null. Given ServerVariable::$validationRules['variable_value' => ['string']], prefer a safe empty string.

Apply this diff:

- 'variable_value' => $variable->server_value ?? $variable->default_value,
+ 'variable_value' => $variable->server_value ?? ($variable->default_value ?? ''),

105-113: Avoid seeding inside a relationship callback (side‑effects on read + N+1).

This runs a select/insert per variable on every render. Prefer:

  • Seed when an EggVariable is created/attached (model observer/job), or
  • A dedicated service method called on state changes, or
  • A bulk upsert to minimize queries.

Example extraction (conceptual):

- foreach ($server->variables as $variable) {
-     ServerVariable::firstOrCreate([...]);
- }
+ app(\App\Services\Servers\ServerVariableSeeder::class)->seedMissing($server);

And in the service, compute the missing (server_id, variable_id) pairs and perform a single upsert or insert ignore rather than per‑row firstOrCreate.

app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1)

640-649: Deduplicate seeding logic shared with Startup page.

The foreach+firstOrCreate block now exists in two places (Startup and EditServer). Extract to a shared service/observer to avoid drift and reduce query count with a bulk strategy.

I can draft a small ServerVariableSeeder with a bulk upsert if you’d like.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ec5fd32 and a8345f9.

📒 Files selected for processing (2)
  • app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1 hunks)
  • app/Filament/Server/Pages/Startup.php (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1)
app/Models/ServerVariable.php (2)
  • ServerVariable (21-65)
  • variable (61-64)
app/Filament/Server/Pages/Startup.php (2)
app/Models/Server.php (2)
  • Server (132-504)
  • variables (310-321)
app/Models/ServerVariable.php (2)
  • variable (61-64)
  • ServerVariable (21-65)
🔇 Additional comments (2)
app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1)

641-646: LGTM: direct firstOrCreate + prefer variable default.

  • Static ServerVariable::firstOrCreate is fine vs ::query()->firstOrCreate.
  • Using $variable->server_value ?? $variable->default_value aligns with the PR objective.

If default_value can be null in your schema, consider coalescing to empty string to keep variable_value non‑null:

- 'variable_value' => $variable->server_value ?? $variable->default_value,
+ 'variable_value' => $variable->server_value ?? ($variable->default_value ?? ''),
app/Filament/Server/Pages/Startup.php (1)

105-116: Ensure relationship callback signature matches Filament version
Your Startup.php uses function (Builder $query, Server $server), whereas other relationship callbacks (including EditServer.php) use a single‐parameter closure and call $this->getRecord() inside. Confirm that your installed Filament version supports passing the record as the second argument; if not, revert to a one‐parameter closure and retrieve the record with $this->getRecord() to avoid runtime errors.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1)

633-642: Avoid writes inside relationship callbacks; seed earlier to reduce surprises and queries.

Calling $server->ensureServerVariablesExist() during the relationship query executes DB writes while building the form. Prefer seeding earlier (e.g., in mount(), beforeFill(), or at the start of form()), so rendering is read-only and runs fewer queries per request. Keep ordering with orderByPowerJoins('variable.sort') unchanged.

If you keep it here, at minimum wrap seeding to do nothing when no rows are missing to avoid repeated firstOrCreate selects.

Would you like a small patch moving the seeding to mount() and keeping the same behavior?

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a8345f9 and 3c35977.

📒 Files selected for processing (3)
  • app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1 hunks)
  • app/Filament/Server/Pages/Startup.php (1 hunks)
  • app/Models/Server.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • app/Filament/Server/Pages/Startup.php
🧰 Additional context used
🧬 Code graph analysis (2)
app/Models/Server.php (1)
app/Models/ServerVariable.php (2)
  • variable (61-64)
  • ServerVariable (21-65)
app/Filament/Admin/Resources/Servers/Pages/EditServer.php (1)
app/Models/Server.php (1)
  • ensureServerVariablesExist (328-338)

@Boy132 Boy132 merged commit 2fc30e1 into main Sep 29, 2025
25 checks passed
@Boy132 Boy132 deleted the boy132/fix-startup-default branch September 29, 2025 13:14
@github-actions github-actions bot locked and limited conversation to collaborators Sep 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Creating a new EggVariable does not propagate its default value to servers as expected.

3 participants