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

Database Debug #1632

Open
ChrisSchinnerl opened this issue Oct 30, 2024 · 0 comments
Open

Database Debug #1632

ChrisSchinnerl opened this issue Oct 30, 2024 · 0 comments
Assignees
Milestone

Comments

@ChrisSchinnerl
Copy link
Member

The one thing that was nice about Gorm is that its library had a .Debug() somewhere, forget whether it was on the database or the transaction. When working with complex queries I regularly find myself extracting query and args and logging those separately. When I'm really deep in the weeds I use this little util that prints the actual query:

func DebugQuery(query string, args []any) string {
	for _, v := range args {
		index := strings.Index(query, "?")
		if index == -1 {
			continue
		}

		var value string
		if _, ok := v.(string); ok {
			value = fmt.Sprintf("%q", v)
		} else {
			value = fmt.Sprint(v)
		}

		query = string(query[:index]) + value + string(query[index+1:])
	}
	return query
}

It would be nice if we would have a Debug on our MainDatabase or MainDatabaseTx that calls this method and prints the query being executed.

@ChrisSchinnerl ChrisSchinnerl self-assigned this Oct 30, 2024
@ChrisSchinnerl ChrisSchinnerl added this to the v2.1.0 milestone Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant