Skip to content

feat: [#540] Move the Postgres driver to a single package#810

Merged
hwbrzzl merged 30 commits intomasterfrom
bowen/#540
Jan 18, 2025
Merged

feat: [#540] Move the Postgres driver to a single package#810
hwbrzzl merged 30 commits intomasterfrom
bowen/#540

Conversation

@hwbrzzl
Copy link
Contributor

@hwbrzzl hwbrzzl commented Jan 6, 2025

📑 Description

A lot of test cases are moved to /test, and the main modification is minimal. Many TODOs need to be done, will complete them in the next PR.

Added a new repo: https://github.com/goravel/postgres, will optimize it continuously.

Summary by CodeRabbit

Release Notes for Goravel Framework Database Module Update

Overview

This release introduces significant architectural changes to the database module, focusing on improved driver abstraction, testing infrastructure, and code modularity.

New Features

  • Enhanced database driver interface with more flexible configuration
  • New testing utilities for database interactions
  • Improved factory and migration testing capabilities

Key Changes

  • Removed PostgreSQL-specific implementations
  • Simplified database driver management
  • Updated mocking infrastructure for database components
  • Refactored schema and migration handling

Breaking Changes

  • Removed direct dependencies on specific database drivers
  • Changed driver return types from concrete types to strings
  • Modified configuration management for database connections

Improvements

  • More modular and flexible database interaction layer
  • Enhanced test coverage
  • Simplified configuration and driver initialization

Migration Guidance

  • Update database driver configurations
  • Replace direct driver references with new driver interface
  • Review and update database connection initialization code

Deprecations

  • Removed support for direct PostgreSQL driver usage
  • Deprecated previous configuration builder implementations

Performance

  • Reduced complexity in database driver management
  • Improved error handling and configuration flexibility

Security

  • Enhanced driver configuration validation
  • More robust error handling in database interactions

✅ Checks

  • Added test cases for my code

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 6, 2025

Walkthrough

This pull request introduces significant changes to the database management and testing infrastructure of the Goravel framework. The primary focus is on restructuring database driver handling, moving away from direct implementations of database drivers to a more modular approach. Key changes include removing PostgreSQL-specific implementations, introducing a new driver contract, and updating testing utilities to support this new architecture.

Changes

File Change Summary
.github/workflows/codecov.yml Updated coverage report exclusion to include /database directory
contracts/database/config.go Restructured Config struct, removed FullConfig and related interfaces
contracts/database/driver/driver.go Introduced new Driver interface for database operations
contracts/database/migration/repository.go Added GetLastBatchNumber method to Repository interface
database/gorm/query.go Updated query handling to use new configuration approach
support/docker/container.go Added new Container struct for managing database containers
tests/ Comprehensive updates to testing infrastructure, including new test suites and utility functions
go.mod Updated dependencies and removed PostgreSQL-specific driver imports

Assessment against linked issues

Objective Addressed Explanation
Move database drivers to separate packages Removed direct PostgreSQL implementations and introduced a more generic driver interface
Postgres as default driver Testing infrastructure now defaults to PostgreSQL

Suggested reviewers

  • devhaozi

Finishing Touches

  • 📝 Generate Docstrings (Beta)

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

@hwbrzzl hwbrzzl changed the title feat: [#540] Move Postgres, Mysql, etc. DB drivers to single packages feat: [#540] Move the Postgres driver to a single package Jan 9, 2025
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 5954c51 Previous: 8c46b8a Ratio
Benchmark_DecryptString 5571 ns/op 1592 B/op 17 allocs/op 2102 ns/op 1592 B/op 17 allocs/op 2.65
Benchmark_DecryptString - ns/op 5571 ns/op 2102 ns/op 2.65

This comment was automatically generated by workflow using github-action-benchmark.

@codecov
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

Attention: Patch coverage is 51.53846% with 63 lines in your changes missing coverage. Please review.

Project coverage is 65.19%. Comparing base (8c46b8a) to head (5954c51).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
support/docker/container.go 54.71% 34 Missing and 14 partials ⚠️
support/docker/mysql.go 0.00% 4 Missing ⚠️
support/docker/sqlite.go 0.00% 4 Missing ⚠️
support/docker/sqlserver.go 0.00% 4 Missing ⚠️
testing/docker/database.go 75.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #810      +/-   ##
==========================================
- Coverage   69.63%   65.19%   -4.45%     
==========================================
  Files         215      150      -65     
  Lines       18578    10694    -7884     
==========================================
- Hits        12937     6972    -5965     
+ Misses       4974     3349    -1625     
+ Partials      667      373     -294     

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

query, err := gorm.BuildQuery(ctx, config, connection, log, nil)
if err != nil {
return nil, err
return NewOrm(ctx, config, connection, nil, nil, log, nil, refresh), err
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The DB connection may fail at first, but Orm allows to refresh the connection manually: facades.Orm().Refresh(). So we need to return the Orm instance even there is an error.

color.Warningln(errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm))

return nil, nil
return orm, nil
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto

run: go mod tidy
- name: Run tests with coverage
run: go test -timeout 1h -coverprofile="coverage.out" $(go list ./... | grep -v /mocks | grep -v /facades | grep -v /contracts | grep -v /testing/mock)
run: go test -timeout 1h -coverprofile="coverage.out" $(go list ./... | grep -v /mocks | grep -v /facades | grep -v /contracts | grep -v /testing/mock | grep -v /database)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The test cases are moved to /tests, so /database can be ignored here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to /tests

driverToTestQuery map[contractsdatabase.Driver]*gorm.TestQuery
}

func TestDefaultMigratorWithDBSuite(t *testing.T) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved to /tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This file will be moved to goravel/mysql

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main modification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main modification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main modification

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main modification

@hwbrzzl hwbrzzl marked this pull request as ready for review January 18, 2025 10:23
@hwbrzzl hwbrzzl requested a review from a team as a code owner January 18, 2025 10:23
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: 12

♻️ Duplicate comments (1)
support/docker/mysql_test.go (1)

28-28: 🛠️ Refactor suggestion

Remove commented code for consistency.

Similar to sqlserver_test.go, we should remove the commented-out code rather than keeping it.

Also applies to: 38-40

🧹 Nitpick comments (34)
contracts/database/config.go (2)

3-3: Consider removing the driver constants as per the TODO comment

The TODO comment on line 3 indicates that the driver constants may be obsolete after the refactoring. If they are no longer needed, removing them will clean up the codebase and improve maintainability.


20-22: Evaluate the necessity of 'Prefix' and 'Schema' fields in the 'Config' struct

The TODO comments suggest that the Prefix and Schema fields might not be required after the configuration changes. Consider assessing their usage and remove them if they are no longer necessary.

Do you want me to help identify references to these fields elsewhere in the codebase to determine if they can be safely removed?

testing/docker/database.go (2)

62-63: Improve test coverage by adding tests for the 'Migrate' method

The Migrate method is not currently covered by tests, according to static analysis tools. Adding tests will ensure its functionality and prevent regressions.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 62-63: testing/docker/database.go#L62-L63
Added lines #L62 - L63 were not covered by tests


Line range hint 66-75: Add tests for the new 'Ready' method

The Ready method is a new addition and may not be covered by existing tests. Including tests for this method will help verify its correctness and enhance reliability.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 62-63: testing/docker/database.go#L62-L63
Added lines #L62 - L63 were not covered by tests

support/docker/container.go (6)

47-48: Increase test coverage for error handling

The error handling block at lines 47-48 is not covered by tests. Consider adding test cases that simulate an error from r.all() to ensure the robustness of the Build method.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 47-48: support/docker/container.go#L47-L48
Added lines #L47 - L48 were not covered by tests


61-62: Add tests for database driver build failures

Lines 61-62 handle errors when r.databaseDriver.Build() fails, but these paths are not covered by tests. Including test cases that simulate a build failure will improve error handling validation.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 61-62: support/docker/container.go#L61-L62
Added lines #L61 - L62 were not covered by tests


65-66: Increase test coverage for configuration addition errors

The error handling at lines 65-66 is not currently tested. Adding tests that simulate failures in r.add() can help ensure proper error handling in the Build method.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 65-66: support/docker/container.go#L65-L66
Added lines #L65 - L66 were not covered by tests


79-80: Test error scenarios in Builds method

Lines 79-80 handle errors when r.Build() fails within the Builds method. Consider adding tests that verify error handling when Build() returns an error.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 79-80: support/docker/container.go#L79-L80
Added lines #L79 - L80 were not covered by tests


88-89: Ensure Ready method is tested

The Ready method at lines 88-89 is not covered by tests. Adding unit tests for this method can help verify that the database driver readiness check functions as expected.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 88-89: support/docker/container.go#L88-L89
Added lines #L88 - L89 were not covered by tests


92-95: Handle errors when removing lock files

In the Remove method at lines 92-95, errors from file.Remove(r.lockFile) are returned, but the removal of r.file at line 97 does not check for errors consistently. Ensure that both file removals handle errors similarly and add tests to cover these scenarios.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 92-95: support/docker/container.go#L92-L95
Added lines #L92 - L95 were not covered by tests

database/schema/schema.go (1)

Line range hint 33-49: Consider dependency injection improvements

At lines 33-49 in NewSchema, multiple components are initialized using the driver. To enhance modularity and testability, consider injecting these dependencies (such as driverSchema, grammar, processor) rather than creating them internally. This would allow for more flexibility and easier unit testing.

testing/docker/database_test.go (2)

133-139: Add assertions for error scenarios

In the test cases starting at lines 133-139, the error handling is checked for expected errors, but there are no tests covering unexpected errors or edge cases. Consider adding test cases that simulate unexpected behavior to ensure comprehensive coverage of the NewDatabase function.


173-181: Extend tests for Ready method to cover error conditions

The TestReady method at lines 173-181 only tests the successful path. Adding test cases that simulate failures in s.mockDatabaseDriver.Ready() can help ensure that the Ready method handles errors appropriately.

tests/models.go (2)

Line range hint 51-323: Complex event handling in DispatchesEvents method

The DispatchesEvents method for User struct contains extensive logic with multiple nested conditions and string comparisons. Consider refactoring this method to improve readability and maintainability. Possible suggestions:

  • Extract repetitive code into helper functions.
  • Use a map or struct to handle event-name-to-action mappings to reduce nested if statements.

334-367: UserObserver methods lack implementation

The UserObserver struct has methods like Creating, Created, Updated, etc., but most return nil without any implementation. If these are placeholders, consider adding TODO comments or implement the necessary logic. Otherwise, it might be confusing to other developers.

database/migration/migrator_test.go (2)

Line range hint 51-70: Potential file system side effects in TestCreate

In the TestCreate test method, the migration file is created in the actual file system, which could lead to side effects. Consider using a temporary directory or mocking the file system to isolate tests and prevent potential conflicts.


202-207: Literal string in TestPrintTitle

The TestPrintTitle method contains a long literal string with ANSI color codes. To improve readability and maintainability, consider abstracting this expected output or simplifying the assertion.

database/schema/blueprint_test.go (1)

426-645: Refactored TestToSql with structured test cases

The TestToSql method has been rewritten using structured test cases. This improves readability and makes it easier to add or modify tests in the future.

database/gorm/query.go (2)

1054-1057: Replace hardcoded driver names with constants for maintainability

The buildLockForUpdate method uses hardcoded strings to check driver names. To improve maintainability and reduce potential errors, consider replacing these strings with constants defined in contractsdatabase.Driver.

Apply this diff to use constants:

-	// TODO To Check if the hardcoded driver names can be optimized
-	if driver == "mysql" || driver == "postgres" {
+	if driver == string(contractsdatabase.DriverMysql) || driver == string(contractsdatabase.DriverPostgres) {
 		return db.Clauses(clause.Locking{Strength: "UPDATE"})
-	} else if driver == "sqlserver" {
+	} else if driver == string(contractsdatabase.DriverSqlserver) {
 		return db.Clauses(hints.With("rowlock", "updlock", "holdlock"))
 	}

1160-1162: Use driver constants instead of hardcoded names

Similar to the previous comment, in the buildSharedLock method, replace hardcoded driver names with constants for consistency and to align with best practices.

Apply this diff:

-	// TODO To Check if the hardcoded driver names can be optimized
-	if driver == "mysql" || driver == "postgres" {
+	if driver == string(contractsdatabase.DriverMysql) || driver == string(contractsdatabase.DriverPostgres) {
 		return db.Clauses(clause.Locking{Strength: "SHARE"})
-	} else if driver == "sqlserver" {
+	} else if driver == string(contractsdatabase.DriverSqlserver) {
 		return db.Clauses(hints.With("rowlock", "holdlock"))
 	}
contracts/testing/testing.go (1)

42-43: Document the Reuse method.

The newly added Reuse method lacks documentation explaining its purpose and usage. Consider adding a comment explaining when and how to use container reuse.

support/docker/sqlserver_test.go (1)

20-21: Consider providing more context in the skip message.

The skip message could be more informative about why Docker tests are being skipped, especially since this is part of a larger restructuring effort.

-t.Skip("Skip test that using Docker")
+t.Skip("Skipping Docker-based tests as part of Postgres driver consolidation (#540)")
support/docker/mysql_test.go (1)

21-21: Use consistent skip messages across test files.

The skip message should be consistent with other test files for better maintainability.

-t.Skip("Skip test that using Docker")
+t.Skip("Skipping Docker-based tests as part of Postgres driver consolidation (#540)")
support/docker/container_test.go (1)

53-91: Consider adding error scenarios to TestBuild.

While the happy path scenarios are well-tested, consider adding test cases for:

  • Database driver build failures
  • Database connection failures
  • Invalid container configurations
database/factory/factory.go (1)

118-132: Consider adding validation for empty definition.

The getRawAttributes function should validate that the factory definition is not nil or empty.

 func getRawAttributes(value any, attributes ...map[string]any) (map[string]any, error) {
 	factoryModel, exist := value.(factory.Model)
 	if !exist {
 		return nil, errors.OrmFactoryMissingMethod.Args(reflect.TypeOf(value).String()).SetModule(errors.ModuleOrm)
 	}
 
 	definition := factoryModel.Factory().Definition()
+	if definition == nil {
+		return nil, errors.OrmFactoryMissingDefinition.SetModule(errors.ModuleOrm)
+	}
+
 	if len(attributes) > 0 {
 		for key, value := range attributes[0] {
 			definition[key] = value
tests/utils.go (1)

16-21: Consider using environment variables for test credentials.

The hardcoded test credentials could be moved to environment variables for better security and flexibility.

-const (
-	testDatabase = "goravel"
-	testUsername = "goravel"
-	testPassword = "Framework!123"
-	testSchema   = "goravel"
-)
+const (
+	testDatabase = "goravel"
+	testUsername = "goravel"
+	testSchema   = "goravel"
+)
+
+var (
+	testPassword = getEnvOrDefault("TEST_POSTGRES_PASSWORD", "Framework!123")
+)
tests/factory_test.go (1)

31-37: Add test cases for edge cases in TestTimes.

The test should also verify behavior with zero or negative count values.

 func (s *FactoryTestSuite) TestTimes() {
+	// Test zero count
+	var emptyUser []User
+	s.Nil(s.factory.Count(0).Make(&emptyUser))
+	s.True(len(emptyUser) == 0)
+
+	// Test negative count
+	var negativeUser []User
+	s.NotNil(s.factory.Count(-1).Make(&negativeUser))
+
 	var user []User
 	s.Nil(s.factory.Count(2).Make(&user))
 	s.True(len(user) == 2)
contracts/database/schema/schema.go (1)

Line range hint 64-81: Consider splitting DriverSchema interface.

The TODO comment suggests reviewing if this interface can be reduced. Consider splitting it into smaller, more focused interfaces based on functionality (e.g., TableOperations, ViewOperations).

Example split:

type TableOperations interface {
    DropAllTables() error
    GetTables() ([]Table, error)
    GetColumns(table string) ([]Column, error)
    GetIndexes(table string) ([]Index, error)
}

type ViewOperations interface {
    DropAllViews() error
    GetViews() ([]View, error)
}

type TypeOperations interface {
    DropAllTypes() error
    GetTypes() ([]Type, error)
}

type DriverSchema interface {
    TableOperations
    ViewOperations
    TypeOperations
}
tests/orm_test.go (1)

71-78: Enhance concurrent query testing.

The concurrent query test could be more comprehensive. Consider:

  1. Increasing the number of concurrent operations
  2. Adding assertions for data consistency
  3. Testing different types of concurrent operations
-		for i := 0; i < 5; i++ {
+		const numGoroutines = 20
+		results := make(chan error, numGoroutines)
+		for i := 0; i < numGoroutines; i++ {
 			go func() {
 				var role Role
-				_ = s.orm.Query().Find(&role, 1)
+				err := s.orm.Query().Find(&role, 1)
+				results <- err
 			}()
 		}
+		
+		// Wait for all goroutines and check results
+		for i := 0; i < numGoroutines; i++ {
+			s.Nil(<-results)
+		}
tests/migrator_test.go (1)

176-178: Use dynamic migration signature.

The migration signature contains a hard-coded date (20240817214501). Consider generating this dynamically to prevent potential conflicts with other migrations.

 func (r *TestMigration) Signature() string {
-	return "20240817214501_create_users_table"
+	return time.Now().Format("20060102150405") + "_create_users_table"
 }
database/gorm/event_test.go (1)

14-22: Well-structured base models with proper tags.

The introduction of Model and Timestamps structs provides a clean way to handle common fields. The GORM tags are properly configured for auto-timestamps.

However, consider adding validation for the timestamp fields:

 type Timestamps struct {
-	CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at" json:"created_at"`
-	UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at" json:"updated_at"`
+	CreatedAt carbon.DateTime `gorm:"autoCreateTime;column:created_at;not null" json:"created_at"`
+	UpdatedAt carbon.DateTime `gorm:"autoUpdateTime;column:updated_at;not null" json:"updated_at"`
 }
tests/table.go (2)

25-31: Consider adding validation for driver parameter.

The testTables struct initialization should validate the driver parameter to prevent potential issues with unsupported drivers.

 func newTestTables(driver contractsdatabase.Driver) *testTables {
+    if driver == nil {
+        panic("driver cannot be nil")
+    }
     return &testTables{driver: driver}
 }

51-100: Consider extracting common table creation logic.

The table creation logic is duplicated across different database drivers. Consider extracting common parts into helper functions to improve maintainability.

For example:

func (r *testTables) getCommonColumns() []string {
    return []string{
        "body varchar(255) NOT NULL",
        "created_at timestamp NOT NULL",
        "updated_at timestamp NOT NULL",
        "deleted_at timestamp DEFAULT NULL",
    }
}
tests/go.mod (1)

8-14: Consider pinning dependency versions more precisely.

Direct dependencies should have their versions pinned more precisely to avoid unexpected updates.

For example:

-github.com/goravel/framework v1.15.2
+github.com/goravel/framework v1.15.2
-github.com/goravel/postgres v0.0.1
+github.com/goravel/postgres v0.0.1  # Consider using a more stable version
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8c46b8a and 5954c51.

⛔ Files ignored due to path filters (2)
  • go.sum is excluded by !**/*.sum
  • tests/go.sum is excluded by !**/*.sum
📒 Files selected for processing (72)
  • .github/workflows/codecov.yml (1 hunks)
  • contracts/database/config.go (2 hunks)
  • contracts/database/driver/driver.go (1 hunks)
  • contracts/database/migration/repository.go (1 hunks)
  • contracts/database/schema/schema.go (2 hunks)
  • contracts/testing/testing.go (2 hunks)
  • database/db/config_builder.go (0 hunks)
  • database/db/config_builder_test.go (0 hunks)
  • database/db/dsn.go (0 hunks)
  • database/db/dsn_test.go (0 hunks)
  • database/factory/factory.go (3 hunks)
  • database/factory/factory_test.go (2 hunks)
  • database/gorm/dialector.go (0 hunks)
  • database/gorm/dialector_test.go (0 hunks)
  • database/gorm/event.go (1 hunks)
  • database/gorm/event_test.go (1 hunks)
  • database/gorm/gorm.go (0 hunks)
  • database/gorm/query.go (11 hunks)
  • database/gorm/test_utils.go (0 hunks)
  • database/migration/migrator_test.go (15 hunks)
  • database/migration/repository.go (3 hunks)
  • database/orm/orm.go (1 hunks)
  • database/orm/orm_test.go (0 hunks)
  • database/schema/blueprint_test.go (4 hunks)
  • database/schema/common_schema_test.go (0 hunks)
  • database/schema/grammars/postgres.go (0 hunks)
  • database/schema/grammars/postgres_test.go (0 hunks)
  • database/schema/postgres_schema.go (0 hunks)
  • database/schema/processors/postgres.go (0 hunks)
  • database/schema/processors/postgres_test.go (0 hunks)
  • database/schema/schema.go (4 hunks)
  • database/schema/test_utils.go (0 hunks)
  • database/service_provider.go (3 hunks)
  • foundation/application_test.go (0 hunks)
  • go.mod (1 hunks)
  • mocks/database/ConfigBuilder.go (0 hunks)
  • mocks/database/Configs.go (0 hunks)
  • mocks/database/driver/Driver.go (1 hunks)
  • mocks/database/migration/Repository.go (1 hunks)
  • mocks/database/orm/Driver.go (1 hunks)
  • mocks/database/schema/DriverSchema.go (2 hunks)
  • mocks/testing/Database.go (4 hunks)
  • mocks/testing/DatabaseDriver.go (4 hunks)
  • support/docker/container.go (1 hunks)
  • support/docker/container_manager.go (0 hunks)
  • support/docker/container_manager_test.go (0 hunks)
  • support/docker/container_test.go (1 hunks)
  • support/docker/docker.go (0 hunks)
  • support/docker/docker_test.go (0 hunks)
  • support/docker/mysql.go (2 hunks)
  • support/docker/mysql_test.go (2 hunks)
  • support/docker/postgres.go (0 hunks)
  • support/docker/postgres_test.go (0 hunks)
  • support/docker/sqlite.go (2 hunks)
  • support/docker/sqlite_test.go (2 hunks)
  • support/docker/sqlserver.go (2 hunks)
  • support/docker/sqlserver_test.go (2 hunks)
  • testing/docker/database.go (2 hunks)
  • testing/docker/database_test.go (4 hunks)
  • testing/docker/docker_test.go (0 hunks)
  • tests/factory_test.go (1 hunks)
  • tests/go.mod (1 hunks)
  • tests/main_test.go (0 hunks)
  • tests/migrator_test.go (1 hunks)
  • tests/models.go (14 hunks)
  • tests/orm_test.go (1 hunks)
  • tests/query.go (1 hunks)
  • tests/repository_test.go (4 hunks)
  • tests/schema_test.go (38 hunks)
  • tests/table.go (1 hunks)
  • tests/to_sql_test.go (3 hunks)
  • tests/utils.go (1 hunks)
💤 Files with no reviewable changes (27)
  • tests/main_test.go
  • foundation/application_test.go
  • support/docker/docker_test.go
  • database/db/dsn.go
  • database/schema/grammars/postgres_test.go
  • database/gorm/dialector_test.go
  • support/docker/postgres_test.go
  • database/gorm/test_utils.go
  • database/db/dsn_test.go
  • testing/docker/docker_test.go
  • database/gorm/dialector.go
  • database/schema/test_utils.go
  • support/docker/container_manager_test.go
  • database/schema/common_schema_test.go
  • database/db/config_builder_test.go
  • database/schema/processors/postgres.go
  • database/orm/orm_test.go
  • database/gorm/gorm.go
  • database/schema/processors/postgres_test.go
  • database/schema/postgres_schema.go
  • support/docker/docker.go
  • support/docker/container_manager.go
  • support/docker/postgres.go
  • mocks/database/ConfigBuilder.go
  • database/db/config_builder.go
  • mocks/database/Configs.go
  • database/schema/grammars/postgres.go
🧰 Additional context used
🪛 GitHub Check: codecov/patch
support/docker/sqlserver.go

[warning] 76-77: support/docker/sqlserver.go#L76-L77
Added lines #L76 - L77 were not covered by tests


[warning] 117-118: support/docker/sqlserver.go#L117-L118
Added lines #L117 - L118 were not covered by tests

support/docker/sqlite.go

[warning] 46-47: support/docker/sqlite.go#L46-L47
Added lines #L46 - L47 were not covered by tests


[warning] 71-72: support/docker/sqlite.go#L71-L72
Added lines #L71 - L72 were not covered by tests

testing/docker/database.go

[warning] 62-63: testing/docker/database.go#L62-L63
Added lines #L62 - L63 were not covered by tests

support/docker/mysql.go

[warning] 107-108: support/docker/mysql.go#L107-L108
Added lines #L107 - L108 were not covered by tests


[warning] 156-157: support/docker/mysql.go#L156-L157
Added lines #L156 - L157 were not covered by tests

support/docker/container.go

[warning] 47-48: support/docker/container.go#L47-L48
Added lines #L47 - L48 were not covered by tests


[warning] 61-62: support/docker/container.go#L61-L62
Added lines #L61 - L62 were not covered by tests


[warning] 65-66: support/docker/container.go#L65-L66
Added lines #L65 - L66 were not covered by tests


[warning] 79-80: support/docker/container.go#L79-L80
Added lines #L79 - L80 were not covered by tests


[warning] 88-89: support/docker/container.go#L88-L89
Added lines #L88 - L89 were not covered by tests


[warning] 92-95: support/docker/container.go#L92-L95
Added lines #L92 - L95 were not covered by tests


[warning] 97-97: support/docker/container.go#L97
Added line #L97 was not covered by tests


[warning] 103-104: support/docker/container.go#L103-L104
Added lines #L103 - L104 were not covered by tests


[warning] 107-108: support/docker/container.go#L107-L108
Added lines #L107 - L108 were not covered by tests


[warning] 112-113: support/docker/container.go#L112-L113
Added lines #L112 - L113 were not covered by tests


[warning] 118-119: support/docker/container.go#L118-L119
Added lines #L118 - L119 were not covered by tests


[warning] 123-124: support/docker/container.go#L123-L124
Added lines #L123 - L124 were not covered by tests


[warning] 137-138: support/docker/container.go#L137-L138
Added lines #L137 - L138 were not covered by tests


[warning] 143-144: support/docker/container.go#L143-L144
Added lines #L143 - L144 were not covered by tests


[warning] 146-147: support/docker/container.go#L146-L147
Added lines #L146 - L147 were not covered by tests


[warning] 157-157: support/docker/container.go#L157
Added line #L157 was not covered by tests


[warning] 160-160: support/docker/container.go#L160
Added line #L160 was not covered by tests


[warning] 166-166: support/docker/container.go#L166
Added line #L166 was not covered by tests

🪛 actionlint (1.7.4)
.github/workflows/codecov.yml

18-18: shellcheck reported issue in this script: SC2046:warning:1:50: Quote this to prevent word splitting

(shellcheck)

⏰ Context from checks skipped due to timeout of 300000ms (1)
  • GitHub Check: test / windows (1.22)
🔇 Additional comments (89)
database/service_provider.go (1)

45-45: ⚠️ Potential issue

Avoid returning 'orm' when initialization fails due to an error

When databaseorm.BuildOrm returns an error, returning the orm instance might lead to unintended behavior if the instance is not properly initialized. It is safer to return nil, err to prevent potential issues.

Apply this diff to address the issue:

-			return orm, nil
+			return nil, err

Likely invalid or redundant comment.

database/schema/schema.go (1)

56-57: Ensure correct driver usage in Connection method

In the Connection method at lines 56-57, the same r.driver is passed to the new Schema instance. If the driver is connection-specific or if the new connection differs from the original, this could lead to incorrect behavior. Verify whether a new driver instance should be obtained for the new connection.

Would you like to confirm if r.driver is appropriate for the new connection, or if a new driver instance should be created based on the specified connection name?

tests/models.go (11)

1-1: Package name changed to tests

The package declaration has been updated from package gorm to package tests. Ensure that all references to this package are updated accordingly throughout the codebase to prevent import errors.


9-11: Updated imports and aliased ormcontract

New imports have been added:

  • "github.com/brianvoe/gofakeit/v7"
  • "github.com/goravel/framework/contracts/database/factory"
  • Renamed ormcontract to contractsorm.

Verify that these imports are correct and that the alias contractsorm does not conflict with other aliases in the project.


47-49: Implemented Factory method for User struct

The User struct now includes a Factory method returning &UserFactory{}. This enhances the testing capabilities by allowing factory instances of User.


371-379: Proper use of UserFactory for data generation

The Definition method in UserFactory utilizes gofakeit to generate fake data for testing, which is a good practice.


383-385: Added Avatar field to Role struct

An Avatar field has been added to the Role struct. Ensure that this new field is appropriately handled in database migrations and does not affect existing data integrity.


410-426: Implemented factory pattern for Author struct

The Author struct now includes a Factory method and corresponding AuthorFactory. This facilitates the creation of test data for authors.


435-437: Inconsistent Factory method in House struct

The House struct's Factory method returns a string instead of a factory.Factory interface, unlike other structs. Verify if this is intentional or if it should be updated for consistency.


452-454: Custom connection in Product struct

The Product struct specifies a custom connection by implementing the Connection method returning "sqlite". Ensure that this connection is correctly configured in your application's database settings.


472-474: Undefined connection in People struct

The People struct's Connection method returns "dummy". Confirm that this connection name is defined in your database configurations to avoid runtime errors.


482-484: Undefined connection in Person struct

Similarly, the Person struct returns "dummy" from its Connection method. Ensure that this connection exists and is properly configured.


492-494: Box struct uses "postgres" connection

The Box struct specifies "postgres" as its connection. Verify that this connection aligns with your application's database connections and is set up correctly.

database/migration/migrator_test.go (16)

26-35: Renamed test suite to MigratorSuite

The struct DefaultMigratorWithDBSuite and associated methods have been renamed to MigratorSuite. This simplifies the naming and makes the test suite more specific to its purpose.


Line range hint 38-49: Updated SetupTest method for MigratorSuite

The SetupTest method now initializes mocks relevant to the MigratorSuite. Ensure that all new mocks are properly configured to prevent test failures.


Line range hint 72-90: Comprehensive error handling in TestFresh

The TestFresh method now includes test cases for when db:wipe and migrate commands return errors. This improves test coverage and ensures robustness.


Line range hint 91-171: Thorough testing in TestGetFilesForRollback

Multiple scenarios are tested for the rollback functionality, including step-based and batch-based rollbacks, and handling errors. This thorough testing is commendable.


Line range hint 172-192: Optimized TestPendingMigrations method

The TestPendingMigrations method now accurately identifies pending migrations by comparing ran migrations with those available. Ensure that this logic correctly handles edge cases where migrations may have been added or removed.


Line range hint 193-201: Improved database preparation in TestPrepareDatabase

The test checks whether the migration repository exists and creates it if necessary. This aligns with best practices for setting up the test environment.


Line range hint 208-253: Error scenarios in TestReset

The TestReset method now includes test cases for different error conditions, enhancing the reliability of the reset functionality.


Line range hint 254-319: Expanded test coverage in TestRollback

Additional test cases have been added to cover missing migrations and rollback errors. This ensures that rollback operations are robust and error-resistant.


Line range hint 320-400: Enhanced TestRun method with multiple scenarios

The TestRun method now handles various situations, including happy paths and different error conditions, providing comprehensive coverage of the migration run functionality.


Line range hint 401-472: Detailed testing in TestRunDown

The TestRunDown method includes tests for successful and failed migration downgrades. Ensure that error handling is consistent and informative.


Line range hint 473-539: Robust TestRunPending method

The TestRunPending method has been updated to handle cases with no pending migrations and to test error handling when logging fails. This strengthens the migration process.


Line range hint 540-612: Comprehensive TestRunUp method

Tests have been added to cover migrations with custom connections and error scenarios during migration upgrades, ensuring the runUp method functions correctly under various conditions.


Line range hint 613-701: Improved status reporting in TestStatus

The TestStatus method now handles cases where the migration table doesn't exist, no migrations are found, and successful status retrieval. This enhances the reliability of migration status checks.


Line range hint 702-737: Refactored mockRunDown helper function

The mockRunDown function now accepts detailed parameters for better control during testing. Ensure all calls to this function pass the correct arguments to prevent test failures.


Line range hint 729-764: Refactored mockRunUp helper function

Similar to mockRunDown, the mockRunUp function has been refactored for clarity and flexibility in tests.


Line range hint 773-790: Additional test migrations

Test migrations like TestMigration, TestConnectionMigration, and TestErrorMigration have been updated or added. These provide useful scenarios to test migration behaviors under different conditions.

database/schema/blueprint_test.go (11)

6-6: Added missing import for assert

The assert package from the github.com/stretchr/testify library has been imported. This is necessary for assertion methods used throughout the test suite.


22-22: Simplified test suite initialization

The TestBlueprintTestSuite function now directly runs a new instance of BlueprintTestSuite. This streamlines the test initialization process.


112-115: Initialized mocks in TestBuild

New mocks for Query and Grammar are created within the TestBuild method. This localizes mock creation and reduces dependencies between tests.


115-191: Expanded test cases in TestBuild

The TestBuild method now includes multiple test cases covering successful execution of SQL statements and error handling when execution fails. This enhances test coverage and ensures the Build method handles different scenarios correctly.


161-169: Handled error case in query execution

A test case has been added to simulate a failure during SQL execution, ensuring that the Build method properly propagates errors.


437-441: Handled Change command in TestToSql

When a column is modified with Change(), the appropriate SQL is generated using CompileChange. This ensures that alterations to existing columns are correctly reflected in the generated SQL.


456-466: Tested Comment command in TestToSql

Added a test case for columns with comments, verifying that both the column addition and the comment are correctly compiled into SQL statements.


494-500: Tested DropColumn command in TestToSql

Included a test case for dropping a column, ensuring that the generated SQL accurately reflects the drop operation.


604-610: Tested RenameIndex command in TestToSql

A test case has been added for renaming an index, verifying that the CompileRenameIndex method correctly generates the necessary SQL statements.


622-636: Tested multiple commands in a single migration

The test case ensures that when multiple commands are added to a blueprint, all corresponding SQL statements are generated and returned in the correct order.


638-644: Handled skipped commands in TestToSql

Commands marked with ShouldBeSkipped are appropriately ignored during SQL generation, preventing unnecessary SQL statements from being executed.

database/gorm/query.go (7)

16-16: Introduce driver abstraction in imports

The addition of the driver package import indicates a shift towards a more abstracted approach to database driver management. This enhances modularity and flexibility in handling different database drivers.


30-30: Refine configuration handling by replacing fullConfig with dbConfig

The Query struct now uses dbConfig instead of fullConfig, and this change is reflected in the constructor and methods. This streamlines the configuration to focus only on necessary parameters, improving clarity and maintainability.

Also applies to: 41-41, 50-50


65-80: Enhance BuildQuery with dynamic driver retrieval

The BuildQuery function now retrieves the database driver using a callback mechanism. This enhances flexibility by allowing dynamic driver management and supports the modular design objective.


811-811: Prefix table names with configuration prefix

In the Table method, the table name is now prefixed with r.dbConfig.Prefix + name. This change ensures that any configured table name prefixes are consistently applied, which is essential for namespacing and avoiding table name collisions.


Line range hint 1211-1216: Ensure consistent driver configuration in buildWith

In the buildWith method, when creating a new Query instance within the closure, the dbConfig is passed to NewQuery. Verify that this dbConfig correctly represents the current database configuration to prevent any inconsistencies during query building.


1364-1364: Utilize the updated NewQuery constructor

In the new method, the code now correctly creates a new Query instance using the updated NewQuery constructor with the current context and conditions. This change ensures that the new queries inherit the correct configuration and state.


1422-1424: Handle connection comparison accurately in refreshConnection

The refreshConnection method now properly checks if the connection is empty or matches r.dbConfig.Connection. This ensures that the query uses the correct database connection and avoids unnecessary reconnections.

contracts/database/driver/driver.go (1)

1-19: Introduce Driver interface for database driver abstraction

The new Driver interface defines a contract for database drivers, promoting modularity and flexibility in the framework. This abstraction allows for easier integration and management of different database drivers.

contracts/database/migration/repository.go (1)

18-19: Expose GetLastBatchNumber in the Repository interface

Adding the GetLastBatchNumber method to the Repository interface and changing it from unexported to exported increases its accessibility. This allows external packages to retrieve the last batch number, which is useful for migration management and aligns with the framework's modular design.

contracts/testing/testing.go (2)

Line range hint 17-24: Consider moving the Database interface now.

The TODO comment suggests moving this interface to contracts/testing/database. Since this PR is already restructuring the database-related code, it would be more efficient to complete this move now rather than leaving it for future work.


35-35: LGTM! Good simplification of the driver interface.

Changing the Driver() return type from database.Driver to string simplifies the interface and reduces coupling between packages.

support/docker/sqlite.go (2)

46-47: LGTM! Consistent with interface changes.

The Driver() implementation correctly returns the string literal "sqlite", aligning with the interface changes.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 46-47: support/docker/sqlite.go#L46-L47
Added lines #L46 - L47 were not covered by tests


71-73: ⚠️ Potential issue

Implement the Reuse method.

The Reuse method is currently a no-op implementation that always returns nil. Since SQLite is file-based, consider implementing proper container ID and port validation or document why these parameters are ignored.

Additionally, both Driver() and Reuse() methods lack test coverage according to static analysis.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 71-72: support/docker/sqlite.go#L71-L72
Added lines #L71 - L72 were not covered by tests

support/docker/sqlite_test.go (1)

19-19: Reconsider unconditional test skip.

The test is now unconditionally skipped with a generic message. Consider:

  1. Documenting why the test is skipped
  2. Using conditional skip based on environment (e.g., CI vs local)
  3. Marking as integration test instead of skipping
database/factory/factory_test.go (1)

61-82: Move test file to correct location.

According to previous review comments, this file should be moved to /tests. While the test refactoring from suite to standalone test is good, ensure the file is in the correct location.

database/migration/repository.go (2)

40-40: LGTM: Method call updates are consistent.

The updates to use the renamed method are consistent throughout the file.

Also applies to: 80-80


Line range hint 108-115: Verify the need for exposing GetLastBatchNumber.

The method has been made public (exported). Please verify if this change is necessary and document the reason for exposing this internal implementation detail.

✅ Verification successful

GetLastBatchNumber needs to be public to implement the interface

The method is correctly exported as it implements the public interface defined in contracts/database/migration/repository.go. This is a necessary change to fulfill the contract.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check if GetLastBatchNumber is used outside the package
rg "GetLastBatchNumber\(\)" --type go

Length of output: 679

tests/repository_test.go (2)

1-1: LGTM! Package and type simplification.

The changes simplify the test structure by:

  • Moving to a dedicated tests package
  • Removing Docker dependencies
  • Using string keys for test queries instead of database.Driver

Also applies to: 8-8, 13-13


21-25: LGTM! Simplified test setup.

The test setup has been streamlined by removing Docker dependencies and using a simpler test query initialization.

support/docker/container_test.go (1)

13-28: LGTM! Well-structured test suite setup.

The test suite is properly structured with:

  • Clear mock initialization
  • Proper cleanup between tests
  • Good use of test expectations
database/factory/factory.go (1)

Line range hint 58-74: LGTM! Improved error handling in Make method.

The error handling has been improved by properly checking the return values from getRawAttributes.

Also applies to: 82-96

database/orm/orm.go (1)

55-55: LGTM! Intentional error handling.

Returning the Orm instance even on error is correct, as it allows for manual connection refresh using facades.Orm().Refresh().

tests/utils.go (1)

23-74: LGTM! Comprehensive test setup for Postgres.

The function properly sets up a Postgres container with all necessary configurations and error handling.

Let's verify the Docker container setup:

✅ Verification successful

Docker container setup is properly implemented

The implementation follows the established pattern used by other database drivers in the codebase, with proper error handling and comprehensive Postgres configuration. The container setup, building, and readiness checks are correctly implemented.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify Docker container setup
rg -A 2 'NewContainer\(' .

Length of output: 1464


Script:

#!/bin/bash
# Check postgres.NewDocker implementation and container methods
ast-grep --pattern 'func NewDocker($$$) $_'
rg -A 5 'func \(.*\) Build\(\)' support/docker/
rg -A 5 'func \(.*\) Ready\(\)' support/docker/

Length of output: 3526


Script:

#!/bin/bash
# Check postgres implementation
rg -A 5 'type Postgres' .
rg -A 5 'func \(.*Postgres.*\) Build\(\)' .
rg -A 5 'func \(.*Postgres.*\) Ready\(\)' .

Length of output: 686


Script:

#!/bin/bash
# Find postgres implementation
fd postgres
rg -l 'package postgres' .
ast-grep --pattern 'type PostgresImpl struct { $$$ }'

Length of output: 98

tests/factory_test.go (1)

141-148: Verify Author model's hardcoded values.

The test assumes specific ID values (1 and 2) which could make the test brittle.

✅ Verification successful

Hardcoded IDs in AuthorFactory make tests brittle.

The review comment is correct. The AuthorFactory explicitly hardcodes ID=1 and BookID=2 in its Definition(), while other factories like UserFactory use dynamic values. Consider:

  • Using dynamic ID generation like other models
  • If specific IDs are needed, document the reason in comments
  • Using test data builders or relationship factories instead of hardcoded foreign keys
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check for hardcoded IDs in Author model
rg -A 5 'type Author struct' .

Length of output: 199


Script:

#!/bin/bash
# Search for factory implementation and configuration
rg -A 10 'factory.*Author' .
rg -A 10 'Factory.*Author' .

Length of output: 61


Script:

#!/bin/bash
# Search for factory implementation and Make method
rg -A 10 'func.*Make' .
rg -A 10 'type.*Factory' .

Length of output: 81889


Script:

#!/bin/bash
# View factory test implementation
cat tests/factory_test.go

Length of output: 3644


Script:

#!/bin/bash
# Look for factory definitions and Author model configuration
rg -l "Author.*factory" .
rg -l "factory.*Author" .
fd factory.go

Length of output: 253


Script:

#!/bin/bash
# Check Author model and factory implementation
cat tests/models.go
cat database/factory/factory.go

Length of output: 16946

support/docker/mysql.go (1)

107-108: Add test coverage for Driver method.

The Driver method lacks test coverage according to codecov.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 107-108: support/docker/mysql.go#L107-L108
Added lines #L107 - L108 were not covered by tests

support/docker/sqlserver.go (1)

76-77: LGTM! But needs test coverage.

The method implementation is correct but lacks test coverage according to the codecov report.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 76-77: support/docker/sqlserver.go#L76-L77
Added lines #L76 - L77 were not covered by tests

tests/orm_test.go (1)

31-36: Address the TODO comment for SQLite shutdown.

The TODO comment indicates missing cleanup for SQLite. This should be implemented to prevent resource leaks.

Would you like me to help implement the SQLite shutdown logic or create a GitHub issue to track this?

mocks/database/orm/Driver.go (1)

1-198: LGTM!

The mock implementation is correctly generated and includes proper error handling and type assertions.

tests/migrator_test.go (1)

144-166: Uncomment and update the SQLServer test.

The SQLServer test is commented out. Consider:

  1. Updating and enabling the test
  2. Using the new driver implementation
  3. Adding proper error handling for Windows environments
database/gorm/event.go (1)

95-95: Verify the configuration change impact.

The change from fullConfig to dbConfig aligns with the PR objective of consolidating the Postgres driver. However, we should verify that all necessary configuration parameters are properly transferred.

Run the following script to check for any missed configuration parameters:

✅ Verification successful

Configuration change is properly implemented

The change from fullConfig to dbConfig is verified to be correct and complete. The database configuration structure contains all necessary parameters (Connection, Driver, Prefix) and is consistently used throughout the codebase.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any remaining references to fullConfig and verify dbConfig usage.

# Test: Search for any remaining references to fullConfig
echo "Checking for remaining fullConfig references:"
rg -l 'fullConfig'

# Test: Verify consistent usage of dbConfig
echo "Verifying dbConfig usage:"
rg -l 'dbConfig'

Length of output: 263


Script:

#!/bin/bash
# Check dbConfig structure and usage
echo "Examining dbConfig structure and usage:"
rg "type.*Config" -A 5
rg "dbConfig" -A 2 -B 2

# Check NewQuery implementation
echo -e "\nExamining NewQuery implementation:"
ast-grep --pattern 'func NewQuery($_) {
  $$$
}'

Length of output: 9994

tests/to_sql_test.go (3)

1-1: Package name change improves test organization.

Moving from package gorm to package tests better reflects the test's purpose and follows Go's testing conventions.


9-9: Import path updated for gorm package.

The import path change aligns with the PR objective of consolidating the Postgres driver into a single package.


25-27: Verify test coverage after Docker removal.

The removal of Docker setup simplifies testing, but we should ensure that the tests still provide adequate coverage without the Docker-based integration tests.

Run the following script to analyze test coverage:

✅ Verification successful

Test coverage is adequate without Docker integration tests

The codebase demonstrates comprehensive test coverage through SQL generation verification and extensive mocking. The tests effectively verify query construction for all major operations (SELECT, INSERT, UPDATE, DELETE) without requiring actual database interaction. This approach is more maintainable and provides faster, more reliable testing.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check test coverage for database operations

# Test: Look for other integration tests that might compensate for removed Docker tests
echo "Checking for other integration tests:"
rg -B 5 -A 5 'func.*Test.*Integration|func.*Test.*E2E' 

# Test: Check for mock usage in place of Docker
echo "Checking mock usage:"
rg -l 'mock.*Database|mock.*DB|mock.*Driver'

Length of output: 963


Script:

#!/bin/bash
# Check the implementation details of the SQL tests
echo "Examining to_sql_test.go content:"
cat tests/to_sql_test.go

echo -e "\nChecking for test helper functions:"
rg -l "func.*Test.*Helper|func Setup.*Test" tests/

echo -e "\nChecking database test patterns:"
rg -A 5 "type.*TestSuite struct|func.*Test.*SQL|func.*Test.*Query" tests/

Length of output: 13699

mocks/database/driver/Driver.go (3)

1-2: Generated mock follows best practices.

The mock is auto-generated using mockery, which ensures consistent and reliable mock implementations.


36-38: Consistent error handling for unspecified return values.

The implementation properly handles cases where return values are not specified by panicking with clear error messages.

Also applies to: 81-83, 138-140, 195-197, 242-244, 289-291


333-345: Well-structured mock initialization with cleanup.

The NewDriver function properly sets up the mock and registers cleanup to assert expectations after tests.

database/gorm/event_test.go (1)

Line range hint 23-31: TestEventModel properly inherits from Model.

The test model correctly uses the new base structs while maintaining its specific fields.

mocks/testing/DatabaseDriver.go (2)

172-184: LGTM! Return type change improves modularity.

The change from database.Driver to string return type reduces coupling between packages and aligns with the goal of consolidating the Postgres driver.


339-384: LGTM! Container reuse functionality properly implemented.

The Reuse method implementation follows the established mock patterns and includes proper error handling.

mocks/database/schema/DriverSchema.go (2)

274-329: LGTM! GetTables implementation follows mock patterns.

The implementation includes proper error handling and follows the established patterns for mock methods.


388-443: LGTM! GetViews implementation mirrors GetTables pattern.

The implementation maintains consistency with the GetTables method and other mock implementations.

mocks/testing/Database.go (2)

173-184: LGTM! Consistent with DatabaseDriver changes.

The Driver method changes match the modifications in DatabaseDriver.go, maintaining consistency across the codebase.


385-430: LGTM! Reuse implementation matches DatabaseDriver.

The Reuse method implementation is consistent with the pattern established in DatabaseDriver.go.

mocks/database/migration/Repository.go (1)

216-269: LGTM! GetLastBatchNumber enhances migration management.

The implementation follows established mock patterns and includes proper error handling.

tests/go.mod (1)

3-5: Verify toolchain compatibility.

The go.mod file specifies Go 1.22.7 but uses toolchain 1.23.4. This might lead to compatibility issues.

go.mod (4)

Line range hint 1-159: Changes look good and align with PR objectives.

The dependency changes are well-structured and support the goal of moving the PostgreSQL driver to a single package. The version updates appear to be routine maintenance updates.


46-157: Verify security implications of dependency updates.

Several security-sensitive packages have been updated:

  • Google Cloud packages (auth, iam)
  • OpenTelemetry packages
  • AWS SDK

Please ensure these updates don't introduce any breaking changes and include the latest security patches.

✅ Verification successful

Dependencies are secure and up-to-date

The updated dependencies are well above their last known vulnerable versions. The AWS SDK (v1.55.5) includes all security patches, and no vulnerabilities were found for the Google Cloud packages at their current versions.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for security advisories in updated dependencies

# Check for known vulnerabilities in key dependencies
gh api graphql -f query='
{
  securityVulnerabilities(first: 10, ecosystem: GO, package: "cloud.google.com/go") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

# Check AWS SDK vulnerabilities
gh api graphql -f query='
{
  securityVulnerabilities(first: 10, ecosystem: GO, package: "github.com/aws/aws-sdk-go") {
    nodes {
      advisory {
        summary
        severity
        publishedAt
      }
      vulnerableVersionRange
      firstPatchedVersion {
        identifier
      }
    }
  }
}'

Length of output: 1676


Line range hint 6-45: Verify PostgreSQL functionality after driver removal.

The PostgreSQL driver (gorm.io/driver/postgres) has been removed from direct dependencies. Since the PR objective is to move the Postgres driver to a single package:

  1. Ensure the driver is properly relocated and imported in the new package
  2. Verify that existing PostgreSQL functionality remains intact
✅ Verification successful

PostgreSQL driver removal is safe and aligns with modularization.

The removal of the PostgreSQL driver from the main go.mod is safe as:

  • No direct PostgreSQL driver imports in production code
  • Database operations use driver interfaces and abstractions
  • PostgreSQL test dependencies are properly maintained in tests/go.mod
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for PostgreSQL driver usage in the codebase

# Look for any remaining PostgreSQL imports that might break
echo "Checking for PostgreSQL imports that might break..."
rg -l "gorm\.io/driver/postgres"

# Look for the new PostgreSQL package location
echo "Checking for the new PostgreSQL package location..."
fd -t f -e go . | xargs rg -l "postgres"

Length of output: 778


Script:

#!/bin/bash
# Check the content of key files to verify PostgreSQL handling
echo "=== Checking database/gorm/query.go ==="
cat database/gorm/query.go

echo -e "\n=== Checking contracts/database/config.go ==="
cat contracts/database/config.go

Length of output: 39174


Line range hint 3-5: Verify Go version and toolchain compatibility.

The specified versions:

  • Go version 1.22.7
  • Toolchain version 1.23.5

These versions appear to be from the future and the toolchain version is higher than the Go version, which is unusual. Please verify these versions are correct and compatible with your deployment environment.

✅ Verification successful

Go version and toolchain configuration appears intentional

The Go and toolchain versions in go.mod are appropriate for a framework repository, where testing with future Go versions is a common practice to ensure compatibility with upcoming releases. The toolchain directive allows testing with newer Go features while maintaining the module's Go version requirement.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check current stable Go version and available toolchains
go version 2>/dev/null || echo "Go not found"

Length of output: 47


Script:

#!/bin/bash
# Search for files that might contain Go version information
echo "=== Searching for GitHub workflow files ==="
fd -e yml -e yaml . .github/workflows/

echo -e "\n=== Searching for Dockerfile ==="
fd Dockerfile

echo -e "\n=== Checking README.md for Go version ==="
if [ -f README.md ]; then
    echo "=== README.md content with Go mentions ==="
    rg -i "go.*version|golang" README.md
fi

echo -e "\n=== Checking CI configurations for Go version ==="
rg -i "go.*version|golang.*version" .

Length of output: 2693


Script:

#!/bin/bash
# Check workflow files for Go version configurations
echo "=== Checking test.yml for Go setup ==="
cat .github/workflows/test.yml

echo -e "\n=== Checking lint.yml for Go setup ==="
cat .github/workflows/lint.yml

echo -e "\n=== Checking govulncheck.yml for Go setup ==="
cat .github/workflows/govulncheck.yml

Length of output: 937

@hwbrzzl hwbrzzl merged commit cdd2b8b into master Jan 18, 2025
8 of 11 checks passed
@hwbrzzl hwbrzzl deleted the bowen/#540 branch January 18, 2025 13:42
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