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

Added Config for custom driver (specifically libSQL) #185

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ytsruh
Copy link

@ytsruh ytsruh commented Mar 11, 2024

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

What did this pull request do?

It adds a Config struct that can be used the same way the Postgres version can be. This allows a user to add their own driver instead of using the pre-defined one.

User Case Description

To use this with libSQL or Turso compatible SQLite

sqlite.go Outdated Show resolved Hide resolved
sqlite.go Outdated Show resolved Hide resolved
Copy link

@haaawk haaawk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@asilvadesigns
Copy link

👀

@xEricL
Copy link

xEricL commented Mar 29, 2024

Looking forward to this! 😃

@xEricL
Copy link

xEricL commented Mar 29, 2024

Just to confirm, I tested the changes in this PR and everything works flawlessly with Turso. Nice work @ytsruh!

@haaawk
Copy link

haaawk commented Apr 1, 2024

What is needed to merge this?
@jinzhu Could you have a look please?

@ncruces
Copy link
Contributor

ncruces commented Apr 10, 2024

I'll be blunt: this is a bad idea.

The API is fine, although I'm partial to just adding a func OpenConn(conn gorm.ConnPool) and be done with it.

The reason why this is a bad idea is the intended purpose of using alternative drivers. I cannot stress this enough: using multiple implementations of SQLite in the same process is a really, really bad idea. If you, by accident, open the same database with different drivers, the most likely result is database corruption 1.

As long as this package imports github.com/mattn/go-sqlite3 we shouldn't encourage using it with any other drivers.

You can easily make a copy of this, and publish your own Turso/libSQL Gorm driver. Same as was done by @glebarez for modernc.org/sqlite.

If we want to encourage alternative drivers, this package should stop importing github.com/mattn/go-sqlite3 and simply assume the driver is registered elsewhere.

Footnotes

  1. the POSIX locking mechanisms SQLite depends on require global variables and if you bundle multiple versions of the library, you'll have multiple copies of those global variables.

@ytsruh
Copy link
Author

ytsruh commented Apr 10, 2024

Thanks a lot, really valuable and I appreciate the feedback. This is my first time playing with SQLite since school so wasn't aware of this.

Given your suggestion would be a breaking change & the maintainers have requested no breaking changes - best course of action is that I split into a new package and publish myself, as you suggested.

If watchers could add a 'thumbs up' to this comment then I'll package this up (I'll replace github.com/mattn/go-sqlite3 package with the Turso!libsql one so it doesn't need to be imported) and publish as a separate Go package on pkg.go.dev

Thanks a lot

@haaawk
Copy link

haaawk commented Apr 10, 2024

I'll be blunt: this is a bad idea.

The API is fine, although I'm partial to just adding a func OpenConn(conn gorm.ConnPool) and be done with it.

The reason why this is a bad idea is the intended purpose of using alternative drivers. I cannot stress this enough: using multiple implementations of SQLite in the same process is a really, really bad idea. If you, by accident, open the same database with different drivers, the most likely result is database corruption 1.

As long as this package imports github.com/mattn/go-sqlite3 we shouldn't encourage using it with any other drivers.

You can easily make a copy of this, and publish your own Turso/libSQL Gorm driver. Same as was done by @glebarez for modernc.org/sqlite.

If we want to encourage alternative drivers, this package should stop importing github.com/mattn/go-sqlite3 and simply assume the driver is registered elsewhere.

Footnotes

  1. the POSIX locking mechanisms SQLite depends on require global variables and if you bundle multiple versions of the library, you'll have multiple copies of those global variables.

I'm not sure this is a problem at all. Neither github.com/tursodatabase/libsql-client-go nor github.com/tursodatabase/go-libsql imports
github.com/mattn/go-sqlite3 or modernc.org/sqlite. Turso uses http to access remote instance of sqlite.

@haaawk
Copy link

haaawk commented Apr 10, 2024

If you decide to do your own "clone" @ytsruh you can take inspiration in https://github.com/ncruces/go-sqlite3/blob/main/gormlite/sqlite.go

@ncruces
Copy link
Contributor

ncruces commented Apr 10, 2024

I'm not sure this is a problem at all. Neither github.com/tursodatabase/libsql-client-go nor github.com/tursodatabase/go-libsql imports github.com/mattn/go-sqlite3 or modernc.org/sqlite. Turso uses http to access remote instance of sqlite.

Then maybe it isn't a problem for Turso, because it only accesses remote databases through HTTP.

But this answer shows exactly why this is a problem: github.com/tursodatabase/go-libsql has a "libsql" driver, with CGO bindings to libSQL.

The fact that Turso doesn't import mattn or modernc is irrelevant. If your users import them (and GORM does), they'll have 3 drivers in their apps: "sqlite3" (mattn), "sqlite" (modernc) and "libsql".

And if they use those drivers to modify the same databases, they'll pretty quickly corrupt them.

@ncruces
Copy link
Contributor

ncruces commented Apr 10, 2024

If you decide to do your own "clone" @ytsruh you can take inspiration in https://github.com/ncruces/go-sqlite3/blob/main/gormlite/sqlite.go

It's very similar to this one, as is @glebarez. The only real difference is the error handling.

The situation with locking is very unfortunate, but people working on SQLite bindings and drivers need to put making it hard for users to corrupt data by accident at the very top.

@ytsruh
Copy link
Author

ytsruh commented Apr 14, 2024

Hmm I would rather get some feedback from the maintainer. The idea to use the config struct originally came from Jinzhu (in another thread). I would rather it be part of the original package but I dont want any breaking changes.

I'll give it another week and see if any feedback comes. If not, I'll clone my own and create some docs.

@ytsruh
Copy link
Author

ytsruh commented Apr 21, 2024

Hi all, I've decided to go solo/indie on this one. Package should now be available at https://github.com/ytsruh/gorm-libsql

go get -u github.com/ytsruh/gorm-libsql

I'd still prefer this to be part of the gorm library so if it is merged in then I willa archive my package and point any users back to Gorm. Until that happens, its useable (with docs in the readme).

Thanks everybody for your support/feedback on this

@haaawk
Copy link

haaawk commented Apr 21, 2024

Thanks for doing this @ytsruh !!!

@xEricL
Copy link

xEricL commented Apr 21, 2024

Hi all, I've decided to go solo/indie on this one. Package should now be available at https://github.com/ytsruh/gorm-libsql

go get -u github.com/ytsruh/gorm-libsql

I'd still prefer this to be part of the gorm library so if it is merged in then I willa archive my package and point any users back to Gorm. Until that happens, its useable (with docs in the readme).

Thanks everybody for your support/feedback on this

@ytsruh When I run go get -u github.com/ytsruh/gorm-libsql I get the error

go: downloading github.com/ytsruh/gorm-libsql v0.1.1
go: github.com/ytsruh/gorm-libsql@upgrade (v0.1.1) requires github.com/ytsruh/gorm-libsql@v0.1.1: parsing go.mod:
        module declares its path as: gorm.io/driver/sqlite
                but was required as: github.com/ytsruh/gorm-libsql

Also, in your README.md, your import uses "https://":

sqlite "https://github.com/ytsruh/gorm-libsql"

@ytsruh
Copy link
Author

ytsruh commented Apr 22, 2024

Sorry about that! (Both of them) Both fixed now...

... https://pkg.go.dev/github.com/ytsruh/gorm-libsql

JuanJoCasamitjana added a commit to JuanJoCasamitjana/sqlite that referenced this pull request Apr 23, 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.

None yet

7 participants