-
Notifications
You must be signed in to change notification settings - Fork 85
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: custom database connection for model #254
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good first step, great work, Bro!
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #254 +/- ##
==========================================
+ Coverage 65.46% 65.66% +0.19%
==========================================
Files 137 137
Lines 8475 8702 +227
==========================================
+ Hits 5548 5714 +166
- Misses 2539 2583 +44
- Partials 388 405 +17
☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work, you did it, Bro.
Hello @hwbrzzl, ...
// Model with custom connection
type Product struct {
orm.Model
orm.SoftDeletes
Name string
Price float64
}
func (p *Product) Connection() string {
return "postgresql"
}
...
func (s *QueryTestSuite) TestFirst() {
for _, query := range s.queries {
tests := []struct {
name string
setup func()
}{
{
name: "success",
setup: func() {
// NOTE: this will throw error when create
product := Product{Name: "create_product", Price: 200}
s.Nil(query.Create(&product))
s.True(product.ID > 0)
var product1 Product
s.Nil(query.Where("name", "create_product").First(&product1))
s.True(product1.ID > 0)
},
},
}
for _, test := range tests {
s.Run(test.name, func() {
test.setup()
})
}
}
} But this is giving me below error:
|
@kkumar-gcc I think you don't need to add a new test case for
About the error, it's caused by the default query is
|
Hello @hwbrzzl, I've encountered an issue while attempting to execute a separate test case for the custom connection. Strangely, I'm receiving a MySQL error, even though the connection has been properly switched to PostgreSQL. This error seems unexpected given that the connection has been intentionally refreshed to use PostgreSQL. 023/08/20 16:09:07 /home/krish/goravel/framework/database/gorm/query.go:88 Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"goravel_product" ("created_at","updated_at","deleted_at","name","price") VALUES' at line 1
[1.115ms] [rows:0] INSERT INTO "goravel_product" ("created_at","updated_at","deleted_at","name","price") VALUES ('2023-08-20 10:39:07.516','2023-08-20 10:39:07.516',NULL,'create_product',200) RETURNING "id"
query_test.go:393:
Error Trace: /home/krish/goravel/framework/database/gorm/query_test.go:393
Error: Expected nil, but got: &mysql.MySQLError{Number:0x428, SQLState:[5]uint8{0x34, 0x32, 0x30, 0x30, 0x30}, Message:"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\"goravel_product\" (\"created_at\",\"updated_at\",\"deleted_at\",\"name\",\"price\") VALUES' at line 1"}
Test: TestCustomConnection
{{0 {2023-08-20 16:09:07 2023-08-20 16:09:07}} {{0001-01-01 00:00:00 +0000 UTC false}} create_product 200}
query_test.go:395:
Error Trace: /home/krish/goravel/framework/database/gorm/query_test.go:395
Error: Should be true
Test: TestCustomConnection |
I tested it locally, but haven't found the best way to implement this, I think maybe we need to choose another way: https://gorm.io/docs/dbresolver.html, it's supported by the Gorm official. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Closes: #182
📑 Description
This PR introduces a new
Model
method to the ORM facade, allowing users to specify custom database connections for specific models.Example Usage:
✅ Checks