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

chore: refactor crud repository methods #38144

Conversation

AnaghHegde
Copy link
Member

@AnaghHegde AnaghHegde commented Dec 13, 2024

Description

As part of transaction support in PG, we are moving from using the jpa methods for database operations. This PR is refactoring the code to use custom repository class for DatasourceStorageStructureRepository from the default CrudRepository.

Automation

/ok-to-test tags="@tag.Datasource"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/12310505479
Commit: f226809
Cypress dashboard.
Tags: @tag.Datasource
Spec:


Fri, 13 Dec 2024 07:34:38 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features

    • Introduced a method to retrieve DatasourceStorageStructure based on datasource and environment identifiers.
  • Bug Fixes

    • Simplified the structure of the DatasourceStorageStructureRepositoryCE interface by removing an outdated method.
  • Documentation

    • Added @Repository annotation to the DatasourceStorageStructureRepositoryCE interface to indicate its role as a Spring Data repository.

Copy link
Contributor

coderabbitai bot commented Dec 13, 2024

Walkthrough

The changes introduce a new method findByDatasourceIdAndEnvironmentId in both the CustomDatasourceStorageStructureRepositoryCE interface and its implementation CustomDatasourceStorageStructureRepositoryCEImpl. This method allows for the retrieval of a DatasourceStorageStructure based on specified datasourceId and environmentId. Additionally, the DatasourceStorageStructureRepositoryCE interface has been updated to include the @Repository annotation and has removed the same method, simplifying its structure while maintaining its role in data access.

Changes

File Change Summary
.../CustomDatasourceStorageStructureRepositoryCE.java Added method Mono<DatasourceStorageStructure> findByDatasourceIdAndEnvironmentId(String, String)
.../CustomDatasourceStorageStructureRepositoryCEImpl.java Added method public Mono<DatasourceStorageStructure> findByDatasourceIdAndEnvironmentId(String, String)
.../DatasourceStorageStructureRepositoryCE.java Removed method Mono<DatasourceStorageStructure> findByDatasourceIdAndEnvironmentId(String, String); added @Repository annotation

Possibly related PRs

  • chore: Add code split for adding package pull status indicator #37944: This PR modifies the ConsolidatedAPIServiceCEImpl class, which is part of the same repository structure as the CustomDatasourceStorageStructureRepositoryCE and its implementation. Although it does not directly relate to the methods added in the main PR, it indicates ongoing changes in the service layer that may interact with data retrieval methods like those introduced in the main PR.

Suggested labels

skip-changelog, ok-to-test, Bug

Suggested reviewers

  • abhvsn
  • sharat87
  • nidhi-nair

🎉 In the code's dance, a new method takes flight,
With datasourceId and env, it shines bright.
A repository's charm, now with Spring's embrace,
Fetching structures with grace, in the data space!
So here's to the changes, a cheer we declare,
In the land of Java, new features we share! 🎊


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. (Experiment)
  • @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.

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.

@AnaghHegde AnaghHegde added the ok-to-test Required label for CI label Dec 13, 2024
@github-actions github-actions bot added the skip-changelog Adding this label to a PR prevents it from being listed in the changelog label Dec 13, 2024
Copy link
Contributor

@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

🧹 Outside diff range and nitpick comments (1)
app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomDatasourceStorageStructureRepositoryCEImpl.java (1)

27-30: Consider adding error handling for non-unique results.

The .one() operator assumes unique results. Consider using .singleOrEmpty() or adding error handling for cases where multiple results might exist.

-        return queryBuilder().criteria(q).one();
+        return queryBuilder().criteria(q)
+                .singleOrEmpty()
+                .switchIfEmpty(Mono.error(new RuntimeException(
+                        "Multiple records found for datasourceId: " + datasourceId +
+                        " and environmentId: " + environmentId)));
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6eb44a1 and f226809.

📒 Files selected for processing (3)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomDatasourceStorageStructureRepositoryCE.java (1 hunks)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomDatasourceStorageStructureRepositoryCEImpl.java (2 hunks)
  • app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/DatasourceStorageStructureRepositoryCE.java (1 hunks)
🔇 Additional comments (2)
app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/DatasourceStorageStructureRepositoryCE.java (1)

6-10: LGTM! Clean repository interface structure.

The addition of @repository annotation and the simplified interface structure follows Spring best practices.

app/server/appsmith-server/src/main/java/com/appsmith/server/repositories/ce/CustomDatasourceStorageStructureRepositoryCE.java (1)

11-11: LGTM! Verify method usage patterns.

The new method signature follows reactive programming patterns correctly.

Let's verify the usage patterns of this method:

✅ Verification successful

Method usage pattern is consistent and well-integrated

The method is properly used across the codebase:

  • Called by DatasourceStructureServiceCEImpl for structure retrieval
  • Follows same pattern as DatasourceStorageRepositoryCE's identical method
  • Correctly implemented in CustomDatasourceStorageStructureRepositoryCEImpl
  • Used consistently in DatasourceStorageServiceCEImpl with proper environment ID handling
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all calls to findByDatasourceIdAndEnvironmentId
rg -t java "findByDatasourceIdAndEnvironmentId" --no-heading

Length of output: 1827

Comment on lines +24 to +31
@Override
public Mono<DatasourceStorageStructure> findByDatasourceIdAndEnvironmentId(
String datasourceId, String environmentId) {
final BridgeQuery<DatasourceStorageStructure> q = Bridge.<DatasourceStorageStructure>equal(
DatasourceStorageStructure.Fields.datasourceId, datasourceId)
.equal(DatasourceStorageStructure.Fields.environmentId, environmentId);
return queryBuilder().criteria(q).one();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Add compound index for datasourceId and environmentId

The DatasourceStorageStructure entity lacks a unique compound index for datasourceId and environmentId fields, which could lead to duplicate entries. Add the following annotation to ensure data integrity:

@CompoundIndex(def = "{'datasourceId': 1, 'environmentId': 1}", unique = true)
  • Location: app/server/external/src/main/java/com/appsmith/external/models/DatasourceStorageStructure.java
🔗 Analysis chain

LGTM! Verify unique constraint enforcement.

The implementation correctly uses the Bridge pattern for query construction.

Let's verify the unique constraint enforcement:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for unique index or constraint definitions
rg -t java "datasourceId.*environmentId.*unique" --no-heading

Length of output: 63


Script:

#!/bin/bash
# Search for unique index or constraint definitions in MongoDB documents
ast-grep --pattern 'class DatasourceStorageStructure {
  $$$
  @Index($$$)
  $$$
}'

# Search for unique index definitions in repository configuration
rg -t java "@Index.*datasourceId.*environmentId" -A 2 -B 2

# Search for compound index definitions
rg -t java "CompoundIndex.*datasourceId.*environmentId" -A 2 -B 2

Length of output: 214


Script:

#!/bin/bash
# Find and examine the DatasourceStorageStructure entity class
fd -e java "DatasourceStorageStructure.java" --exec cat {}

# Look for MongoDB index configurations in repository configuration files
fd -e java "MongoConfig" --exec cat {}

Length of output: 14924

@AnaghHegde AnaghHegde merged commit 5235304 into release Dec 13, 2024
49 checks passed
@AnaghHegde AnaghHegde deleted the chore/refactor-datasource-storage-structure-crud-repository branch December 13, 2024 09:10
github-actions bot pushed a commit to Zeral-Zhang/appsmith that referenced this pull request Feb 7, 2025
## Description
As part of transaction support in PG, we are moving from using the jpa
methods for database operations. This PR is refactoring the code to use
custom repository class for DatasourceStorageStructureRepository from
the default CrudRepository.

## Automation

/ok-to-test tags="@tag.Datasource"

### 🔍 Cypress test results
<!-- This is an auto-generated comment: Cypress test results  -->
> [!TIP]
> 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
> Workflow run:
<https://github.com/appsmithorg/appsmith/actions/runs/12310505479>
> Commit: f226809
> <a
href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=12310505479&attempt=2"
target="_blank">Cypress dashboard</a>.
> Tags: `@tag.Datasource`
> Spec:
> <hr>Fri, 13 Dec 2024 07:34:38 UTC
<!-- end of auto-generated comment: Cypress test results  -->


## Communication
Should the DevRel and Marketing teams inform users about this change?
- [ ] Yes
- [ ] No


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Introduced a method to retrieve `DatasourceStorageStructure` based on
datasource and environment identifiers.
  
- **Bug Fixes**
- Simplified the structure of the
`DatasourceStorageStructureRepositoryCE` interface by removing an
outdated method.

- **Documentation**
- Added `@Repository` annotation to the
`DatasourceStorageStructureRepositoryCE` interface to indicate its role
as a Spring Data repository.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ok-to-test Required label for CI skip-changelog Adding this label to a PR prevents it from being listed in the changelog
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants