Skip to content

feat: [#712,#713] facades.Orm().Query() and facades.DB() Support Updating JSON Columns#1092

Merged
almas-x merged 2 commits intomasterfrom
almas/json-columns-update
Jun 25, 2025
Merged

feat: [#712,#713] facades.Orm().Query() and facades.DB() Support Updating JSON Columns#1092
almas-x merged 2 commits intomasterfrom
almas/json-columns-update

Conversation

@almas-x
Copy link
Contributor

@almas-x almas-x commented Jun 25, 2025

📑 Description

Resolve goravel/goravel#712
Resolve goravel/goravel#713

When updating a JSON column, support use -> syntax to update the appropriate key in the JSON object.

facades.Orm().Query().Model(&models.User{}).Where("id", 1).Update("options->enabled", true)
facades.Orm().Query().Model(&models.User{}).Where("id", 1).Update("options->languages[0]", "en")
facades.Orm().Query().Model(&models.User{}).Where("id", 1).Update("options->languages", []string{"en", "de"})

facades.Orm().Query().Model(&models.User{}).Where("id", 1).Update(map[string]any{
    "preferences->dining->meal": "salad",
    "options->languages[0]":     "en",
    "options->enabled":          true,
})
facades.DB().Table("users").Where("id", 1).Update("options->enabled", true)
facades.DB().Table("users").Where("id", 1).Update("options->languages[0]", "en")
facades.DB().Table("users").Where("id", 1).Update("options->languages", []string{"en", "de"})

facades.DB().Table("users").Where("id", 1).Update(map[string]any{
    "preferences->dining->meal": "salad",
    "options->languages[0]":     "en",
    "options->enabled":          true,
})

✅ Checks

  • Added test cases for my code

Copilot AI review requested due to automatic review settings June 25, 2025 09:06
@almas-x almas-x requested a review from a team as a code owner June 25, 2025 09:06
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for updating JSON columns by allowing the use of the "->" syntax to target specific keys within the JSON object. Key changes include:

  • Implementing the CompileJsonColumnsUpdate method in both grammar and its mocks.
  • Adding an error constant for JSON column update failures.
  • Updating the query and test files to incorporate the new JSON column update logic.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
mocks/database/driver/JsonGrammar.go Added mock function and helper methods for CompileJsonColumnsUpdate.
mocks/database/driver/Grammar.go Added mock function and helper methods for CompileJsonColumnsUpdate.
errors/list.go Added a new error constant for JSON column update invalid values.
database/gorm/query.go Updated query implementation to compile JSON column updates when values are maps.
database/db/query_test.go Updated tests to set expectations for JSON column update behavior.
database/db/query.go Updated update builder to run JSON column update compilation.
database/db/expression.go Minor update wrapping gorm.Expr in a new Expr type.
contracts/database/driver/grammar.go Added a new interface method for JSON column update in the driver grammar.
Comments suppressed due to low confidence (1)

contracts/database/driver/grammar.go:167

  • Consider expanding the comment to clarify that keys may use the '->' syntax for nested JSON updates. This will help maintainers quickly understand the expected input format.
	// CompileJsonColumnsUpdate Compile the JSON  columns for an update statement.

@almas-x
Copy link
Contributor Author

almas-x commented Jun 25, 2025

I will submit a separate PR to fix and add the external tests after the database driver repository PR is merged.

@codecov
Copy link

codecov bot commented Jun 25, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.09%. Comparing base (b7a40b5) to head (1171a37).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1092   +/-   ##
=======================================
  Coverage   71.09%   71.09%           
=======================================
  Files         183      183           
  Lines       12854    12854           
=======================================
  Hits         9138     9138           
  Misses       3345     3345           
  Partials      371      371           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

hwbrzzl
hwbrzzl previously approved these changes Jun 25, 2025
Copy link
Contributor

@hwbrzzl hwbrzzl left a comment

Choose a reason for hiding this comment

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

Nice 👍

@almas-x almas-x merged commit a2cf499 into master Jun 25, 2025
12 of 14 checks passed
@almas-x almas-x deleted the almas/json-columns-update branch June 25, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

facades.DB() Support Updating JSON Columns facades.Orm().Query() Support Updating JSON Columns

3 participants