1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 14:14:04 +00:00

SQLite improvements

- (BC BREAK) Remove $WAL static property. We instead just let
  Laravel use its journal_mode config now
- Remove journal, wal, and shm files when deleting tenant DB
- Check that the system is 64-bit when using NoAttach (we don't
  build 32 bit extensions)
- Use local static instead of a class static property for caching
  loadExtensionSupported
This commit is contained in:
Samuel Štancl 2025-09-01 02:07:36 +02:00
parent 4e22c4dd6e
commit 13a2209f11
3 changed files with 38 additions and 63 deletions

View file

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Stancl\Tenancy\Database\TenantDatabaseManagers;
use AssertionError;
use Closure;
use Illuminate\Database\Eloquent\Model;
use PDO;
@ -19,13 +18,6 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
*/
public static string|null $path = null;
/**
* Should the WAL journal mode be used for newly created databases.
*
* @see https://www.sqlite.org/pragma.html#pragma_journal_mode
*/
public static bool $WAL = true;
/*
* If this isn't null, a connection to the tenant DB will be created
* and passed to the provided closure, for the purpose of keeping the
@ -89,26 +81,7 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
return true;
}
try {
if (file_put_contents($path = $this->getPath($name), '') === false) {
return false;
}
// todo@sqlite we can just respect Laravel config for WAL now
if (static::$WAL) {
$pdo = new PDO('sqlite:' . $path);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// @phpstan-ignore-next-line method.nonObject
assert($pdo->query('pragma journal_mode = wal')->fetch(PDO::FETCH_ASSOC)['journal_mode'] === 'wal', 'Unable to set journal mode to wal.');
}
return true;
} catch (AssertionError $e) {
throw $e;
} catch (Throwable) {
return false;
}
return file_put_contents($this->getPath($name), '') !== false;
}
public function deleteDatabase(TenantWithDatabase $tenant): bool
@ -123,9 +96,16 @@ class SQLiteDatabaseManager implements TenantDatabaseManager
return true;
}
$path = $this->getPath($name);
try {
// todo@sqlite we should also remove any other files for the DB e.g. WAL
return unlink($this->getPath($name));
unlink($path.'-journal');
unlink($path.'-wal');
unlink($path.'-shm');
} catch (Throwable) {}
try {
return unlink($path);
} catch (Throwable) {
return false;
}