Laravel connection persistent SQLite in memory database configuration

295 Views Asked by At

I am setting up a Laravel solution where I want to use a SQLite database configured as in memory database. I have the following configuration for this:

.env:

DB_CONNECTION_DATA=sqlite
DB_DATABASE_DATA=:memory:

database.php:

...
'sqlite' => [
            'driver' => 'sqlite',
            'url' => env('DATABASE_URL'),
            'database' => env('DB_DATABASE_DATA'), 
            'prefix' => '',
            'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
        ],

Migration works fine with this configuration but seeding fails with error message:

SQLSTATE[HY000]: General error: 1 no such table: permissions (SQL: select * from "permissions")

I have googled around to find a solution for this and found this package: laravel-sqlite-named-memory-connection Here it is described that:

It connects to SQLite's in-memory database as well as :memory:. :memory: creates a new database for each connection, whereas :named-memory:<name> returns the same connection  if <name> is same.

So I assume that this is the explanation why seeding does not work because it is run in another connection than the migrations so the database does not exist for the seeding connection.

I could then use this package to solve this but the challenge is that it seem to be set up for Laravel 7, not 9 which I use and has not been updated for the last 3 years.

Anyone know if there are similar, more up to date, packages available or other ways to configure this?

PS: In googling I have found solutions in context of test use but I want to use this in production.

Tried:

php artisan db:seed PermissionsSeeder

Got error message:

SQLSTATE[HY000]: General error: 1 no such table: permissions (SQL: select * from "permissions")

Expected:

Successful seeding.

Also tried:

composer require crhg/laravel-sqlite-named-memory-connection

Got error message:

Info from https://repo.packagist.org: #StandWithUkraine
Using version ^1.1 for crhg/laravel-sqlite-named-memory-connection
./composer.json has been updated
Running composer update crhg/laravel-sqlite-named-memory-connection
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - crhg/laravel-sqlite-named-memory-connection[v1.1.0, ..., v1.1.2] require illuminate/support ^5.5 || ^6.0 -> found illuminate/support[v5.5.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev] but these were not loaded, likely because it conflicts with another require.
    - crhg/laravel-sqlite-named-memory-connection v1.1.3 requires illuminate/support ^5.5 || ^6.0 || ^7.0 -> found illuminate/support[v5.5.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires crhg/laravel-sqlite-named-memory-connection ^1.1 -> satisfiable by crhg/laravel-sqlite-named-memory-connection[v1.1.0, v1.1.1, v1.1.2, v1.1.3].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require crhg/laravel-sqlite-named-memory-connection:*" to figure out if any version is installable, or "composer require crhg/laravel-sqlite-named-memory-connection:^2.1" if you know which you need.

Expected:

Successful package installation.

0

There are 0 best solutions below