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

Merged
merged 2 commits into from
Jun 12, 2024
Merged

Added Config for custom driver (specifically libSQL) #185

merged 2 commits into from
Jun 12, 2024

Conversation

ytsruh
Copy link
Contributor

@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
Contributor 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
Contributor 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
Contributor 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/[email protected]: 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
Contributor 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
@jinzhu
Copy link
Member

jinzhu commented Jun 12, 2024

Sorry for the delay. This PR looks good, so I'll merge it first. If you want your new driver to be added to the GORM documentation, you can submit a PR to gorm.io.

@jinzhu jinzhu merged commit bbca3b3 into go-gorm:master Jun 12, 2024
3 checks passed
@ncruces
Copy link
Contributor

ncruces commented Jun 21, 2024

This PR is just depressing.

Where does libsql-client-go even return an error that's compatible with error_translator.go?

@glebarez has its own error translation, gormlite does the same.

This just does... nothing?

It's like #161: it just ignores the issues, and acts like they don't exist.

Do the extensive Gorm unit tests even pass? No one knows, no one cares, apparently.

@avinassh
Copy link

TIL about GORM tests. May be they should be part of this repo's CI? In any case, those can be worked and fixed.

@ncruces
Copy link
Contributor

ncruces commented Jun 21, 2024

They are part of GORM's CI.

This GORM driver is tested with the mattn SQLite driver on GORM's CI.

If you want to use another SQLite driver with this GORM driver, you should run tests, like:

@xEricL
Copy link

xEricL commented Jun 22, 2024

@jinzhu what are your thoughts on @ncruces's concerns?

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.

8 participants