-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
[11.x] Support named in-memory SQLite connections #53635
Conversation
If you want I can also add a couple of tests — might prevent some future regressions since Looks like these are the only relevant tests of the connector itself: framework/tests/Database/DatabaseConnectorTest.php Lines 260 to 284 in abc1faa
So I could just add a similar test for named databases as there is for in memory databases. There's probably no need to test that "named connections work" since that's PDO's responsibility. Regarding hardcoded
So let me know if I should add any specific tests. I'll probably just push changes to the two snippets above and add the connector mock test for named in-memory DBs. |
I've added the test now and confirmed that if I revert my patch to
Now I could go over a few spots in the tests and make sure all tests that test the behavior I've extended here are also tested with named in-memory databases but I don't think that should be necessary. If you think those changes should be made I don't have a problem with making them though. |
Thanks! |
PR: laravel/framework#53635 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
* [9.x] Add support for named in-memory SQLite connections PR: laravel/framework#53635 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
* [9.x] Add support for named in-memory SQLite connections PR: laravel/framework#53635 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
SQLite supports named in-memory databases. The benefit of named connections is that you can essentially "reconnect" to the same memory region even without passing a PDO instance around. Only the DSN needs to be the same.
Here's an example:
It shows how you can connect to the same database, without passing the PDO instance directly, as long as the DSN string is identical.
foo()
will logfoo
whilebar()
will logfoo
andbar
. The only condition here is that the connection is kept alive somehow.If you swap the
$dsn
definitions, bothfoo()
andbar()
will create their own database when the PDO connection is created (and free it once$conn
goes out of scope). Additionally, if you remove the$_ =
, the memory of the database will be freed as there's no longer any open connection, so it'll behave identically to:memory:
.Practical use case
This lets me support
:memory:
in my tenancy package for testing tenant databases, providing a nice speedup. During the tenant creation process there's a lot of logic that essentially keeps connecting to and disconnecting from the tenant DB. Users may also switch to the central context usingtenancy()->end()
ortenancy()->central(closure)
which closes the tenant connection.With named databases, I only need to keep a connection to the tenant DB alive somewhere (literally just
register_shutdown_function(fn () => $conn)
works) and then the package can seamlessly connect to and disconnect from tenant databases that live entirely in process memory and don't need any special cleanup.Code style
I could just check for
mode=memory
but that could in theory be part of a real filename. Don't think it'd make anything vulnerable if therealpath()
check got skipped, but I went with?mode
||&mode
just in case since it's a little more restrictive.The DSN syntax for named in-memory databases is just
mode=memory
in URI format typically paired withcache=shared
. The order being arbitrary.I also tried to respect the 3 char rule in the comment but couldn't think of anything that'd fit here better, so the last line is an extra character shorter.