-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(database/gdb): Resolved the schema error in the database output log when using the database sharding feature #4319
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
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.
Pull Request Overview
This PR fixes a bug in database sharding where the schema name was incorrectly logged in database operations when using the sharding feature. The issue was that while the SQL execution used the correct sharded schema, the logging output still showed the original schema name instead of the dynamically determined one.
- Temporarily updates the Core schema to match the sharded schema during database operations
- Adds defer statements to restore the original schema after operations complete
- Ensures log output correctly reflects the actual database schema being used
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
| h.Model.db.GetCore().schema = h.Schema | ||
| defer func() { | ||
| h.Model.db.GetCore().schema = h.originalSchemaName.String() | ||
| }() |
Copilot
AI
Sep 28, 2025
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.
Direct assignment to the Core schema without synchronization could cause race conditions if multiple goroutines access the same database instance simultaneously. Consider using atomic operations or mutex protection for the schema field access.
| } | ||
| h.Model.db.GetCore().schema = h.Schema | ||
| defer func() { | ||
| h.Model.db.GetCore().schema = h.originalSchemaName.String() |
Copilot
AI
Sep 28, 2025
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.
The schema restoration logic is duplicated across all four hook methods. Consider extracting this into a helper method to reduce code duplication and improve maintainability.
| } | ||
| h.Model.db.GetCore().schema = h.Schema | ||
| defer func() { | ||
| h.Model.db.GetCore().schema = h.originalSchemaName.String() |
Copilot
AI
Sep 28, 2025
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.
The schema restoration logic is duplicated across all four hook methods. Consider extracting this into a helper method to reduce code duplication and improve maintainability.
| } | ||
| h.Model.db.GetCore().schema = h.Schema | ||
| defer func() { | ||
| h.Model.db.GetCore().schema = h.originalSchemaName.String() |
Copilot
AI
Sep 28, 2025
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.
The schema restoration logic is duplicated across all four hook methods. Consider extracting this into a helper method to reduce code duplication and improve maintainability.
| } | ||
| h.Model.db.GetCore().schema = h.Schema | ||
| defer func() { | ||
| h.Model.db.GetCore().schema = h.originalSchemaName.String() |
Copilot
AI
Sep 28, 2025
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.
The schema restoration logic is duplicated across all four hook methods. Consider extracting this into a helper method to reduce code duplication and improve maintainability.
error log
right log
我有两个完全一样的数据库db_0和db_1,两个custom表里都只有一条数据,id是1和2,执行上面两条查询得的结果是正确的,




输出日志中应该分别是
[default] [db_0]和[default] [db_1],但是实际输出的都是[default] [db_0],查看具体代码实现,该日志由Core实例中获取group和schema构成,而实际执行sql时如果使用了分库特性那么执行sql的link会使用当前面schema重新生成,但是没有重新赋给Core实例,所以输出日志的时候还是错的,只需要在生成link后生成日志前把schema赋给Core就行,方法执行完成后defer将schema重置回去,防止有人重复使用同一个gdb对象造成困扰