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

Raw SQLs having whitespace(s) as prefix are going to write DB #19

Closed
shudipta opened this issue Dec 27, 2020 · 1 comment
Closed

Raw SQLs having whitespace(s) as prefix are going to write DB #19

shudipta opened this issue Dec 27, 2020 · 1 comment
Assignees

Comments

@shudipta
Copy link

shudipta commented Dec 27, 2020

GORM Playground Link

go-gorm/playground#239

Description

While using gorm.io/plugin/dbresolver to split read-write, raw query having whitespace(s) as prefix should go to read server.

import (
  "gorm.io/gorm"
  "gorm.io/plugin/dbresolver"
  "gorm.io/driver/mysql"
)

DB, err := gorm.Open(mysql.Open("db1_dsn"), &gorm.Config{})

DB.Use(dbresolver.Register(dbresolver.Config{
  // use `db1` as sources, `db2` as replicas for 
  Sources:  []gorm.Dialector{mysql.Open("db1_dsn")},
  Replicas: []gorm.Dialector{mysql.Open("db2_dsn")}
}, "users"))


// ...

readDB, err := gorm.Open(mysql.Open("db2_dsn"), &gorm.Config{})
if err != nil {
  // handle error
}

readDB.Create(&User{Name: "read"})

// this query should go to the read db with db2_dsn
DB.Raw(`
select name from users
where name = ?
`, "read").Row().Scan(&name)

This query should go to the read db with db2_dsn, but the current dbresolver sends it to the write db with db1_dsn.

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

No branches or pull requests

2 participants