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

feat(indexer): postgres schema creation + CI config #20701

Merged
merged 100 commits into from
Jul 18, 2024

Conversation

aaronc
Copy link
Member

@aaronc aaronc commented Jun 17, 2024

Description

This PR adds:

  • a new go module cosmossdk.io/indexer/postgres for the postgres indexer
  • a new go module cosmossdk.io/indexer/postgres/tests for postgres indexer integration tests (to keep the dependencies on pgx and embedded postgres out of indexer/postgres)
  • postgres schema creation for cosmossdk.io/schema module schemas with tests
  • CI config for the above

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced automated weekly updates for dependencies in the PostgreSQL indexer directories.
    • Added a new job for running PostgreSQL indexer tests, including SonarCloud integration.
  • Bug Fixes

    • Updated error message formatting in various validation functions for better compatibility and clarity.
  • Chores

    • Added new entry for PostgreSQL indexer in the PR labeler configuration.

Copy link

sonarqubecloud bot commented Jul 8, 2024

Quality Gate Passed Quality Gate passed for 'Cosmos SDK - Postgres Indexer'

Issues
2 New issues
0 Accepted issues

Measures
0 Security Hotspots
81.2% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

sonarqubecloud bot commented Jul 8, 2024

Quality Gate Failed Quality Gate failed for 'Cosmos SDK - Schema'

Failed conditions
B Maintainability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

@github-actions github-actions bot removed the C:schema label Jul 9, 2024

// CreateTableSql generates a CREATE TABLE statement for the object type.
func (tm *TableManager) CreateTableSql(writer io.Writer) error {
_, err := fmt.Fprintf(writer, "CREATE TABLE IF NOT EXISTS %q (\n\t", tm.TableName())
Copy link
Member

Choose a reason for hiding this comment

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

Shall we use ? for avoiding requiring to sanitize table name and module names?

Copy link
Member Author

Choose a reason for hiding this comment

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

Can placeholders bind identifiers? I thought it was only parameters. The downside would be that it's hard to inspect the actual SQL in tests and logs

Copy link
Member

Choose a reason for hiding this comment

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

I see, everything goes to a writer, cannot be a prepared statement indeed. We should probably sanitize the inputs then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Schema validation enforces use of the NameFormat regex first, then second %q quotes and escapes these

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

lgtm! sql generator seems to output valid sql.
I find only the module manager to have a confusing name given what is a module manager in the SDK, especially because it manages only one module.

@aaronc
Copy link
Member Author

aaronc commented Jul 14, 2024

lgtm! sql generator seems to output valid sql. I find only the module manager to have a confusing name given what is a module manager in the SDK, especially because it manages only one module.

You're right that manager is non intuitive because they don't actually manage modules. How about calling these "module indexer" and "object indexer" instead of manager? Applied this renaming in the latest commit.

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: 0

Outside diff range, codebase verification and nitpick comments (4)
indexer/postgres/go.mod (1)

1-5: Update to Go module declaration and notes.

The module declaration and the note about staying on an earlier version of Go are clear and well-documented. However, consider updating to a more recent version of Go if possible to benefit from the latest features and security updates.

indexer/postgres/tests/testdata/init_schema_no_retain_delete.txt (1)

7-35: Table creation for test_all_kinds.

The SQL statements for creating the table test_all_kinds are correct. However, consider the following improvements:

  1. The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
  2. Add a comma for clarity.
- "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos")) STORED,
+ "time" TIMESTAMPTZ ALWAYS GENERATED AS (nanos_to_timestamptz("time_nanos")) STORED,

- "float64" DOUBLE PRECISION NOT NULL
+ "float64" DOUBLE PRECISION NOT NULL,
Tools
LanguageTool

[style] ~25-~25: The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
Context: ..." BOOLEAN NOT NULL, "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos"))...

(ADVERB_WORD_ORDER)


[typographical] ~29-~29: Consider adding a comma here.
Context: ...oat32" REAL NOT NULL, "float64" DOUBLE PRECISION NOT NULL, "bech32address" TEXT NOT NUL...

(NN_NOT_NN_COMMA)

indexer/postgres/tests/testdata/init_schema.txt (1)

8-35: Improve table creation for test_all_kinds.

The SQL statement for creating the table test_all_kinds is mostly correct. However, consider the following improvements:

  1. The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
  2. Add a comma for clarity.
- "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos")) STORED,
+ "time" TIMESTAMPTZ ALWAYS GENERATED AS (nanos_to_timestamptz("time_nanos")) STORED,

- "float64" DOUBLE PRECISION NOT NULL
+ "float64" DOUBLE PRECISION NOT NULL,
Tools
LanguageTool

[style] ~25-~25: The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
Context: ..." BOOLEAN NOT NULL, "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos"))...

(ADVERB_WORD_ORDER)


[typographical] ~29-~29: Consider adding a comma here.
Context: ...oat32" REAL NOT NULL, "float64" DOUBLE PRECISION NOT NULL, "bech32address" TEXT NOT NUL...

(NN_NOT_NN_COMMA)

indexer/postgres/module.go (1)

31-55: Improve error messages in InitializeSchema.

The InitializeSchema method is mostly correct. However, consider adding more context to error messages for better debugging.

- return err
+ return fmt.Errorf("failed to create enum types for fields in module %s: %w", m.moduleName, err)
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b48d768 and 16cf734.

Files ignored due to path filters (2)
  • indexer/postgres/go.sum is excluded by !**/*.sum
  • indexer/postgres/tests/go.sum is excluded by !**/*.sum
Files selected for processing (15)
  • indexer/postgres/README.md (1 hunks)
  • indexer/postgres/column.go (1 hunks)
  • indexer/postgres/create_table.go (1 hunks)
  • indexer/postgres/create_table_test.go (1 hunks)
  • indexer/postgres/enum.go (1 hunks)
  • indexer/postgres/go.mod (1 hunks)
  • indexer/postgres/indexer.go (1 hunks)
  • indexer/postgres/internal/testdata/example_schema.go (1 hunks)
  • indexer/postgres/module.go (1 hunks)
  • indexer/postgres/object.go (1 hunks)
  • indexer/postgres/options.go (1 hunks)
  • indexer/postgres/tests/go.mod (1 hunks)
  • indexer/postgres/tests/init_schema_test.go (1 hunks)
  • indexer/postgres/tests/testdata/init_schema.txt (1 hunks)
  • indexer/postgres/tests/testdata/init_schema_no_retain_delete.txt (1 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/postgres/create_table.go
Files skipped from review as they are similar to previous changes (8)
  • indexer/postgres/README.md
  • indexer/postgres/column.go
  • indexer/postgres/create_table_test.go
  • indexer/postgres/enum.go
  • indexer/postgres/indexer.go
  • indexer/postgres/internal/testdata/example_schema.go
  • indexer/postgres/options.go
  • indexer/postgres/tests/init_schema_test.go
Additional context used
Path-based instructions (2)
indexer/postgres/object.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

indexer/postgres/module.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

LanguageTool
indexer/postgres/tests/testdata/init_schema_no_retain_delete.txt

[style] ~25-~25: The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
Context: ..." BOOLEAN NOT NULL, "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos"))...

(ADVERB_WORD_ORDER)


[typographical] ~29-~29: Consider adding a comma here.
Context: ...oat32" REAL NOT NULL, "float64" DOUBLE PRECISION NOT NULL, "bech32address" TEXT NOT NUL...

(NN_NOT_NN_COMMA)

indexer/postgres/tests/testdata/init_schema.txt

[style] ~25-~25: The adverb ‘ALWAYS’ is usually put before the verb ‘GENERATED’.
Context: ..." BOOLEAN NOT NULL, "time" TIMESTAMPTZ GENERATED ALWAYS AS (nanos_to_timestamptz("time_nanos"))...

(ADVERB_WORD_ORDER)


[typographical] ~29-~29: Consider adding a comma here.
Context: ...oat32" REAL NOT NULL, "float64" DOUBLE PRECISION NOT NULL, "bech32address" TEXT NOT NUL...

(NN_NOT_NN_COMMA)

Additional comments not posted (18)
indexer/postgres/go.mod (1)

7-11: Dependency management.

The dependency on cosmossdk.io/schema is correctly specified. Ensure that this dependency is compatible with the rest of your codebase.

indexer/postgres/object.go (4)

1-7: Package and imports.

The package declaration and imports are appropriate. Ensure that all imported packages are necessary and used within the code.


9-16: ObjectIndexer struct definition.

The ObjectIndexer struct is well-defined with clear field names. Ensure that the Options type is correctly defined and used.


18-39: NewObjectIndexer function.

The NewObjectIndexer function correctly initializes the ObjectIndexer struct. The use of maps for allFields and valueFields is efficient. Ensure that the Options parameter is correctly utilized within the struct.


41-44: TableName method.

The TableName method correctly formats the table name using the module name and object type name. The use of fmt.Sprintf is appropriate.

indexer/postgres/tests/go.mod (3)

1-11: Module declaration and dependencies.

The module declaration and dependencies are appropriate for testing. Ensure that all dependencies are necessary and compatible with the rest of your codebase.


13-29: Indirect dependencies.

The indirect dependencies are correctly specified. Ensure that these dependencies do not introduce any conflicts or issues in your codebase.


31-33: Replace directive and Go version.

The replace directive correctly points to the local module. The Go version is appropriately set to 1.22.

indexer/postgres/tests/testdata/init_schema_no_retain_delete.txt (4)

1-2: Enum type creation.

The SQL statement for creating the test_my_enum enum type is correct.


4-5: Enum type creation.

The SQL statement for creating the test_vote_type enum type is correct.


37-45: Table creation for test_singleton.

The SQL statements for creating the table test_singleton are correct.


47-54: Table creation for test_vote.

The SQL statements for creating the table test_vote are correct.

indexer/postgres/tests/testdata/init_schema.txt (4)

2-2: Enum creation for test_my_enum looks good.

The SQL statement for creating the enum type test_my_enum is correct.


5-5: Enum creation for test_vote_type looks good.

The SQL statement for creating the enum type test_vote_type is correct.


38-45: Table creation for test_singleton looks good.

The SQL statement for creating the table test_singleton is correct.


48-55: Table creation for test_vote looks good.

The SQL statement for creating the table test_vote is correct.

indexer/postgres/module.go (2)

20-28: Constructor for ModuleIndexer looks good.

The NewModuleIndexer method correctly initializes a new ModuleIndexer with the provided parameters.


59-61: Getter for ObjectIndexers looks good.

The ObjectIndexers method correctly returns the object indexers for the module.

@testinginprod testinginprod self-requested a review July 15, 2024 08:22
Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

lgtm

@tac0turtle tac0turtle enabled auto-merge July 15, 2024 08:25
@tac0turtle tac0turtle added this pull request to the merge queue Jul 18, 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: 0

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 16cf734 and 8eb2f88.

Files selected for processing (7)
  • .github/dependabot.yml (1 hunks)
  • .github/pr_labeler.yml (1 hunks)
  • .github/workflows/test.yml (1 hunks)
  • indexer/postgres/create_table_test.go (1 hunks)
  • indexer/postgres/tests/init_schema_test.go (1 hunks)
  • schema/field.go (3 hunks)
  • schema/object_type.go (3 hunks)
Files skipped from review as they are similar to previous changes (4)
  • .github/dependabot.yml
  • .github/pr_labeler.yml
  • .github/workflows/test.yml
  • indexer/postgres/tests/init_schema_test.go
Additional context used
Path-based instructions (3)
schema/field.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

indexer/postgres/create_table_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

schema/object_type.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (11)
schema/field.go (3)

36-36: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.


49-49: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.


70-70: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.

indexer/postgres/create_table_test.go (5)

10-41: Approved: Test function for creating SQL table with all kinds of fields.

The function is well-structured and includes expected output comments for validation.


43-54: Approved: Test function for creating SQL table for a singleton object.

The function is well-structured and includes expected output comments for validation.


56-67: Approved: Test function for creating SQL table for a vote object.

The function is well-structured and includes expected output comments for validation.


69-79: Approved: Test function for creating SQL table for a vote object without retaining deletions.

The function is well-structured and includes expected output comments for validation.


81-94: Approved: Helper functions for creating SQL tables.

The functions are well-structured and handle error checking appropriately.

schema/object_type.go (3)

46-46: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.


65-65: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.


92-92: Approved: Change in error message formatting.

The change from %w to %v in the error message formatting is appropriate to address the false positive linting issue.

Merged via the queue into main with commit 9376db5 Jul 18, 2024
75 checks passed
@tac0turtle tac0turtle deleted the aaronc/indexer-postgres1 branch July 18, 2024 09:38
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.

5 participants