Compatibility with `github.com/mattn/go-sqlite3` to use with entgo.io
I might have a wrong expectation but I thought that I could replace github.com/mattn/go-sqlite3 by "modernc.org/sqlite" and get https://entgo.io/ working.
Unfortunately it is not exactly that simple but it is close once you understand the issue. The problem is described here better than I could do it.
The issue can be mitigated with the registration of a custom driver named sqlite3:
type sqliteDriver struct {
*sqlite.Driver
}
func (d sqliteDriver) Open(name string) (driver.Conn, error) {
conn, err := d.Driver.Open(name)
if err != nil {
return conn, err
}
c := conn.(interface{Exec(stmt string, args []driver.Value) (driver.Result, error)})
if _, err := c.Exec("PRAGMA foreign_keys = on;", nil); err != nil {
conn.Close()
return nil, errors.Wrap(err, "failed to enable enable foreign keys")
}
return conn, nil
}
func init() {
sql.Register("sqlite3", sqliteDriver{Driver: &sqlite.Driver{}})
}
func main() {
client, err := ent.Open(dialect.SQLite, "file:ent?mode=memory&cache=shared")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// ...
}
Please do not hesitate to let me know if you need more information ?