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

[#6824] Handle context in Scan() without breaking external library compatibility #7178

Conversation

zeze1004
Copy link

@zeze1004 zeze1004 commented Sep 1, 2024

issue: #6824

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

I initially thought that adding context as an argument to the Scan() method would solve the issue, but it causes compile errors because external libraries do not expect Scan to accept context.

I’m wondering if it’s the right approach to modify Scan to directly accept context, or if there’s a better way to handle this issue without changing the method signature.

package sqlite
...
type Migrator struct {
	migrator.Migrator
}

func (m *Migrator) RunWithoutForeignKey(fc func() error) error {
	var enabled int
	m.DB.Raw("PRAGMA foreign_keys").Scan(&enabled)   // Scan() error ⭐ ⭐ ⭐  
	if enabled == 1 {
		m.DB.Exec("PRAGMA foreign_keys = OFF")
		defer m.DB.Exec("PRAGMA foreign_keys = ON")
	}

	return fc()
}

User Case Description

I’m a bit concerned about how I’m approaching this. Specifically, I’m wondering if adding context.Context internally in the Scan method is the right move. Since external libraries like sqlite might be using this method, I’m not sure if this could cause issues.

Questions:

Is it okay to add context.Context handling like this, or is there a better way I should consider?
Could this change cause compatibility issues with libraries that don’t expect the context.Context?
Should I be thinking about this problem differently, especially regarding method signatures?

I’d really appreciate any guidance or feedback on this. Thanks!

@zeze1004 zeze1004 changed the title Handle context in Scan() without breaking external library compatibility [#6824] Handle context in Scan() without breaking external library compatibility Sep 1, 2024
@jinzhu
Copy link
Member

jinzhu commented Sep 14, 2024

Just use it like this? https://gorm.io/docs/context.html#Single-Session-Mode

@jinzhu jinzhu closed this Sep 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants