Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated models.init pattern for initializing models #22358

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

daniellockyer
Copy link
Member

refs https://ghost.slack.com/archives/C02G9E68C/p1740997729615089 ref https://linear.app/ghost/issue/ENG-2071/remove-modelsinit-pattern

  • models.init will dynamically require all the files in the folder and re-export them
  • this is useful in that you can add a model, but the downside is that it breaks all editor autocomplete because we don't know the exports until runtime
  • more generally, this pattern is super annoying and we always have to remember to do models.init in unit tests
  • to fix that, we can deprecate the use of this (there are some other places outside of this codebase we need to remove it from too) and explicit export all the files
  • this means you have to add your new model to this file, but that's better than not having any types available

refs https://ghost.slack.com/archives/C02G9E68C/p1740997729615089
ref https://linear.app/ghost/issue/ENG-2071/remove-modelsinit-pattern

- `models.init` will dynamically require all the files in the folder and
  re-export them
- this is useful in that you can add a model, but the downside is that
  it breaks all editor autocomplete because we don't know the exports
  until runtime
- more generally, this pattern is super annoying and we always have to
  remember to do `models.init` in unit tests
- to fix that, we can deprecate the use of this (there are some other
  places outside of this codebase we need to remove it from too) and
  explicit export all the files
- this means you have to add your new model to this file, but that's
  better than not having any types available
Copy link
Contributor

coderabbitai bot commented Mar 4, 2025

Walkthrough

The changes remove synchronous calls to initialize models from various parts of the application and its test suites. In the core boot, CLI, REPL, migration hooks, update-check routines, and the model index file, the explicit call to models.init() has been eliminated. Similarly, many test files across unit, integration, and regression suites no longer invoke model initialization via lifecycle hooks. In some cases, the code now directly imports specific model files instead of relying on a centralized models object. Overall, the modifications focus on deferring or removing synchronous model setup, streamlining both the production and test environments without altering public interfaces.


📜 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 49b6a64 and 63f3aac.

📒 Files selected for processing (67)
  • ghost/core/core/boot.js (0 hunks)
  • ghost/core/core/cli/generate-data.js (0 hunks)
  • ghost/core/core/cli/repl.js (0 hunks)
  • ghost/core/core/server/data/migrations/hooks/init/before.js (0 hunks)
  • ghost/core/core/server/models/base/listeners.js (4 hunks)
  • ghost/core/core/server/models/index.js (1 hunks)
  • ghost/core/core/server/models/user.js (1 hunks)
  • ghost/core/core/server/services/update-check/run-update-check.js (0 hunks)
  • ghost/core/core/server/web/api/testmode/jobs/graceful-job.js (0 hunks)
  • ghost/core/test/integration/url_service.test.js (0 hunks)
  • ghost/core/test/regression/api/admin/schedules.test.js (0 hunks)
  • ghost/core/test/regression/models/model_settings.test.js (0 hunks)
  • ghost/core/test/unit/api/cache-invalidation.test.js (0 hunks)
  • ghost/core/test/unit/api/canary/session.test.js (0 hunks)
  • ghost/core/test/unit/api/canary/utils/validators/input/pages.test.js (0 hunks)
  • ghost/core/test/unit/api/canary/utils/validators/input/posts.test.js (0 hunks)
  • ghost/core/test/unit/frontend/helpers/authors.test.js (0 hunks)
  • ghost/core/test/unit/frontend/helpers/get.test.js (0 hunks)
  • ghost/core/test/unit/frontend/helpers/ghost_head.test.js (1 hunks)
  • ghost/core/test/unit/frontend/helpers/recommendations.test.js (0 hunks)
  • ghost/core/test/unit/frontend/helpers/tags.test.js (0 hunks)
  • ghost/core/test/unit/frontend/meta/keywords.test.js (0 hunks)
  • ghost/core/test/unit/server/adapters/scheduling/post-scheduling/post-scheduler.test.js (0 hunks)
  • ghost/core/test/unit/server/data/db/backup.test.js (0 hunks)
  • ghost/core/test/unit/server/data/exporter/index.test.js (0 hunks)
  • ghost/core/test/unit/server/data/schema/fixtures/fixture-manager.test.js (0 hunks)
  • ghost/core/test/unit/server/data/schema/validator.test.js (0 hunks)
  • ghost/core/test/unit/server/models/api-key.test.js (0 hunks)
  • ghost/core/test/unit/server/models/base/crud.test.js (0 hunks)
  • ghost/core/test/unit/server/models/base/index.test.js (0 hunks)
  • ghost/core/test/unit/server/models/base/relations.test.js (0 hunks)
  • ghost/core/test/unit/server/models/comment.test.js (0 hunks)
  • ghost/core/test/unit/server/models/custom-theme-setting.test.js (0 hunks)
  • ghost/core/test/unit/server/models/integration.test.js (0 hunks)
  • ghost/core/test/unit/server/models/invite.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member-click-event.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member-created-event.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member-feedback.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member-paid-subscription-event.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member-subscribe-event.test.js (0 hunks)
  • ghost/core/test/unit/server/models/member.test.js (0 hunks)
  • ghost/core/test/unit/server/models/milestone.test.js (0 hunks)
  • ghost/core/test/unit/server/models/newsletter.test.js (0 hunks)
  • ghost/core/test/unit/server/models/permission.test.js (0 hunks)
  • ghost/core/test/unit/server/models/post.test.js (0 hunks)
  • ghost/core/test/unit/server/models/session.test.js (0 hunks)
  • ghost/core/test/unit/server/models/settings.test.js (0 hunks)
  • ghost/core/test/unit/server/models/single-use-token.test.js (0 hunks)
  • ghost/core/test/unit/server/models/stripe-customer-subscription.test.js (0 hunks)
  • ghost/core/test/unit/server/models/subscription-created-event.test.js (0 hunks)
  • ghost/core/test/unit/server/models/tag.test.js (0 hunks)
  • ghost/core/test/unit/server/models/user.test.js (0 hunks)
  • ghost/core/test/unit/server/services/auth/api-key/admin.test.js (0 hunks)
  • ghost/core/test/unit/server/services/auth/api-key/content.test.js (0 hunks)
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js (0 hunks)
  • ghost/core/test/unit/server/services/auth/session/store.test.js (0 hunks)
  • ghost/core/test/unit/server/services/frontend-data-service/frontend-data-service.test.js (0 hunks)
  • ghost/core/test/unit/server/services/frontend-data-service/index.test.js (0 hunks)
  • ghost/core/test/unit/server/services/members/middleware.test.js (1 hunks)
  • ghost/core/test/unit/server/services/newsletters/index.test.js (0 hunks)
  • ghost/core/test/unit/server/services/newsletters/service.test.js (0 hunks)
  • ghost/core/test/unit/server/services/permissions/can-this.test.js (0 hunks)
  • ghost/core/test/unit/server/services/permissions/index.test.js (0 hunks)
  • ghost/core/test/unit/server/services/permissions/providers.test.js (0 hunks)
  • ghost/core/test/unit/server/services/staff/index.test.js (0 hunks)
  • ghost/core/test/unit/server/services/webhooks/serialize.test.js (0 hunks)
  • ghost/core/test/utils/index.js (0 hunks)
💤 Files with no reviewable changes (62)
  • ghost/core/test/unit/server/models/single-use-token.test.js
  • ghost/core/core/cli/repl.js
  • ghost/core/test/unit/server/models/custom-theme-setting.test.js
  • ghost/core/core/server/data/migrations/hooks/init/before.js
  • ghost/core/core/cli/generate-data.js
  • ghost/core/test/unit/server/models/stripe-customer-subscription.test.js
  • ghost/core/test/regression/api/admin/schedules.test.js
  • ghost/core/test/unit/server/models/comment.test.js
  • ghost/core/core/server/web/api/testmode/jobs/graceful-job.js
  • ghost/core/test/unit/server/models/member-paid-subscription-event.test.js
  • ghost/core/test/unit/server/models/subscription-created-event.test.js
  • ghost/core/test/unit/server/models/newsletter.test.js
  • ghost/core/test/unit/server/models/session.test.js
  • ghost/core/test/unit/server/services/webhooks/serialize.test.js
  • ghost/core/core/boot.js
  • ghost/core/test/unit/server/models/member-created-event.test.js
  • ghost/core/test/regression/models/model_settings.test.js
  • ghost/core/test/unit/server/models/member-feedback.test.js
  • ghost/core/test/unit/server/models/tag.test.js
  • ghost/core/test/unit/api/canary/session.test.js
  • ghost/core/test/unit/server/models/base/index.test.js
  • ghost/core/test/unit/server/data/schema/fixtures/fixture-manager.test.js
  • ghost/core/test/integration/url_service.test.js
  • ghost/core/test/unit/frontend/meta/keywords.test.js
  • ghost/core/test/unit/server/adapters/scheduling/post-scheduling/post-scheduler.test.js
  • ghost/core/test/unit/frontend/helpers/recommendations.test.js
  • ghost/core/test/unit/server/services/permissions/index.test.js
  • ghost/core/test/unit/server/services/auth/api-key/content.test.js
  • ghost/core/test/unit/server/services/permissions/can-this.test.js
  • ghost/core/test/unit/server/data/schema/validator.test.js
  • ghost/core/test/unit/server/models/api-key.test.js
  • ghost/core/test/unit/server/services/frontend-data-service/index.test.js
  • ghost/core/test/utils/index.js
  • ghost/core/test/unit/server/services/newsletters/index.test.js
  • ghost/core/test/unit/api/canary/utils/validators/input/pages.test.js
  • ghost/core/test/unit/server/models/permission.test.js
  • ghost/core/test/unit/server/data/db/backup.test.js
  • ghost/core/test/unit/server/services/auth/api-key/admin.test.js
  • ghost/core/test/unit/server/models/invite.test.js
  • ghost/core/test/unit/server/models/member.test.js
  • ghost/core/test/unit/api/cache-invalidation.test.js
  • ghost/core/core/server/services/update-check/run-update-check.js
  • ghost/core/test/unit/server/models/settings.test.js
  • ghost/core/test/unit/server/models/base/relations.test.js
  • ghost/core/test/unit/server/models/post.test.js
  • ghost/core/test/unit/server/services/frontend-data-service/frontend-data-service.test.js
  • ghost/core/test/unit/server/services/staff/index.test.js
  • ghost/core/test/unit/frontend/helpers/authors.test.js
  • ghost/core/test/unit/server/models/user.test.js
  • ghost/core/test/unit/server/data/exporter/index.test.js
  • ghost/core/test/unit/frontend/helpers/get.test.js
  • ghost/core/test/unit/server/services/auth/session/middleware.test.js
  • ghost/core/test/unit/server/models/integration.test.js
  • ghost/core/test/unit/server/models/base/crud.test.js
  • ghost/core/test/unit/server/models/milestone.test.js
  • ghost/core/test/unit/server/models/member-click-event.test.js
  • ghost/core/test/unit/api/canary/utils/validators/input/posts.test.js
  • ghost/core/test/unit/server/services/auth/session/store.test.js
  • ghost/core/test/unit/frontend/helpers/tags.test.js
  • ghost/core/test/unit/server/models/member-subscribe-event.test.js
  • ghost/core/test/unit/server/services/permissions/providers.test.js
  • ghost/core/test/unit/server/services/newsletters/service.test.js
✅ Files skipped from review due to trivial changes (2)
  • ghost/core/test/unit/server/services/members/middleware.test.js
  • ghost/core/core/server/models/user.js
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: Browser tests
  • GitHub Check: Ghost-CLI tests
  • GitHub Check: Database tests (Node 20.11.1, sqlite3)
  • GitHub Check: Unit tests (Node 22.13.1)
  • GitHub Check: Unit tests (Node 20.11.1)
  • GitHub Check: Regression tests (Node 20.11.1, sqlite3)
  • GitHub Check: Unit tests (Node 18.12.1)
  • GitHub Check: Database tests (Node 22.13.1, mysql8)
  • GitHub Check: Regression tests (Node 20.11.1, mysql8)
  • GitHub Check: Database tests (Node 20.11.1, mysql8)
  • GitHub Check: Database tests (Node 18.12.1, mysql8)
  • GitHub Check: Lint
🔇 Additional comments (8)
ghost/core/test/unit/frontend/helpers/ghost_head.test.js (1)

1546-1546: No functional changes introduced.
This appears to be a minor formatting or bracket change, which doesn't affect the test logic.

ghost/core/core/server/models/index.js (1)

9-77: Static exports improve clarity and tooling support.
Replacing the dynamic require approach with explicit exports is a strong step toward better editor autocomplete and maintainability. Keep in mind that new models must now be manually added to this list.

ghost/core/core/server/models/base/listeners.js (6)

7-9: Good improvement in dependency management.

The direct import of specific models instead of using a global models object improves code clarity and enables better IDE support for autocomplete and type checking.


47-47: Direct model method usage looks good.

Replacing models.Post.findAll with PostModel.findAll is a straightforward substitution that should work identically while providing better IDE support.


73-73: Direct model edit method usage looks good.

The change from models.Post.edit to PostModel.edit follows the same pattern of direct model usage, which is consistent with the PR's objectives.


117-117: Direct Settings model usage looks good.

Using SettingsModel.edit instead of models.Settings.edit is consistent with the pattern applied throughout this file and aligns with the PR objectives.


42-42:

✅ Verification successful

Verify transaction method availability in PostModel.

The code now uses PostModel.transaction instead of what was likely models.Base.transaction. Make sure PostModel properly inherits or implements this transaction functionality to ensure consistent behavior.


🏁 Script executed:

#!/bin/bash
# Check if PostModel properly inherits or implements the transaction method
# Look for the transaction method definition in the Post model or its parent classes

# First, check the Post model implementation
if [ -f "ghost/core/core/server/models/post.js" ]; then
  echo "Examining Post model implementation..."
  grep -n "transaction" ghost/core/core/server/models/post.js
fi

# Then check the Base model that Post likely extends
if [ -f "ghost/core/core/server/models/base/index.js" ]; then
  echo "Examining Base model implementation..."
  grep -n "transaction" ghost/core/core/server/models/base/index.js
fi

# Check if there's a model utility file that might contain the transaction method
if [ -d "ghost/core/core/server/models" ]; then
  echo "Searching for transaction method in model utilities..."
  find ghost/core/core/server/models -type f -name "*.js" | xargs grep -l "transaction" | xargs grep -n "transaction" || echo "No results found"
fi

Length of output: 3922


Transaction Method Inheritance Verified

After checking the code, I confirmed that the Base model (in ghost/core/core/server/models/base/index.js at lines 75–76) implements the transaction method using ghostBookshelf.transaction(fn). The Post model (in ghost/core/core/server/models/post.js) relies on this implementation—consistent with how transaction functionality is used throughout the repository (see multiple references across models). Therefore, invoking PostModel.transaction(...) in ghost/core/core/server/models/base/listeners.js (line 42) is correct and consistent with our design.


7-117:

❓ Verification inconclusive

Ensure consistency with other model imports across the codebase.

The changes consistently replace the global models object with direct imports for specific models. To maintain this consistency throughout the codebase, verify that:

  1. All other files follow the same pattern
  2. The naming convention is consistent (ModelName: ModelNameModel)
  3. The model imports use the correct relative paths

This will ensure a smooth transition away from the deprecated models.init pattern.


🏁 Script executed:

#!/bin/bash
# Check the consistency of model import changes across the codebase

# 1. Find files that still use the old models.init pattern
echo "Checking for remaining uses of models.init:"
grep -r "models\.init" --include="*.js" ghost/core/

# 2. Check for consistency in model import naming conventions
echo -e "\nChecking model import naming conventions:"
grep -r "const {.*: .*Model}" --include="*.js" ghost/core/ | head -n 20

# 3. Check for direct model usage
echo -e "\nChecking direct model usage instead of models.X:"
grep -r "models\.[A-Z]" --include="*.js" ghost/core/ | head -n 20

Length of output: 3691


Direct Model Import Consistency & Legacy Usage Verification

  • The file ghost/core/core/server/models/base/listeners.js correctly uses direct imports (e.g. {Post: PostModel} and {Settings: SettingsModel}) with the expected naming convention and relative paths.
  • However, our repository scan shows that legacy patterns still persist in several places. Notably:
    • models.init() is used in files like:
      • ghost/core/core/server/services/xmlrpc.js
      • ghost/core/core/server/services/slack.js
      • ghost/core/core/server/data/migrations/hooks/migrate/before.js
      • a deprecation warning in ghost/core/core/server/models/index.js
    • Additionally, some test files continue to reference model usage via the global models object (e.g., in ghost/core/test/utils/stripe-mocker.js and other test files).
  • Action Items:
    • Update production files still using models.init() or direct models.X calls to leverage direct imports, ensuring they follow the {ModelName: ModelNameModel} convention with correct relative paths.
    • Verify whether the legacy usages in test files should be updated for consistency or if they can remain temporarily during the transition.
✨ Finishing Touches
  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant