From 6f0ec12268568d993e81410c4d5fdff2ce510dd8 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sat, 14 Dec 2024 12:15:08 -0700 Subject: [PATCH] Add SQLiteAdapter::isMemory helper function (#2336) --- src/Phinx/Config/Config.php | 2 +- src/Phinx/Db/Adapter/SQLiteAdapter.php | 15 +++++++++++++-- tests/Phinx/Db/Adapter/SQLiteAdapterTest.php | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/Phinx/Config/Config.php b/src/Phinx/Config/Config.php index 85186ff9a..4b38d31d4 100644 --- a/src/Phinx/Config/Config.php +++ b/src/Phinx/Config/Config.php @@ -179,7 +179,7 @@ public function getEnvironment(string $name): ?array if ( isset($environments[$name]['adapter']) && $environments[$name]['adapter'] === 'sqlite' - && !empty($environments[$name]['memory']) + && SQLiteAdapter::isMemory($environments[$name]) ) { $environments[$name]['name'] = SQLiteAdapter::MEMORY; } diff --git a/src/Phinx/Db/Adapter/SQLiteAdapter.php b/src/Phinx/Db/Adapter/SQLiteAdapter.php index eca3b7603..e25b7ab03 100644 --- a/src/Phinx/Db/Adapter/SQLiteAdapter.php +++ b/src/Phinx/Db/Adapter/SQLiteAdapter.php @@ -138,6 +138,17 @@ public function databaseVersionAtLeast(string $ver): bool return version_compare($actual, $ver, '>='); } + /** + * Check if the given options represent a memory database + * + * @param array $options Options to check + * @return bool + */ + public static function isMemory(array $options): bool + { + return !empty($options['memory']) || ($options['name'] ?? '') === static::MEMORY; + } + /** * {@inheritDoc} * @@ -171,7 +182,7 @@ public function connect(): void $dsn = 'sqlite:file:' . ($options['name'] ?? '') . '?' . implode('&', $params); } else { // use a memory database if the option was specified - if (!empty($options['memory']) || $options['name'] === static::MEMORY) { + if (SQLiteAdapter::isMemory($options)) { $dsn = 'sqlite:' . static::MEMORY; } else { $dsn = 'sqlite:' . $options['name'] . $this->suffix; @@ -204,7 +215,7 @@ public function connect(): void */ public static function getSuffix(array $options): string { - if (($options['name'] ?? '') === self::MEMORY) { + if (SQLiteAdapter::isMemory($options)) { return ''; } diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index 2b87f7558..f44906e29 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -3481,4 +3481,23 @@ public function testPdoNotPersistentConnection() $adapter = new SQLiteAdapter(SQLITE_DB_CONFIG); $this->assertFalse($adapter->getConnection()->getAttribute(PDO::ATTR_PERSISTENT)); } + + public function isMemoryProvider(): array + { + return [ + [['name' => ':memory:'], true], + [['memory' => true], true], + [['name' => 'foo', 'memory' => true], true], + [['name' => 'bar'], false], + [['memory' => false], false], + ]; + } + + /** + * @dataProvider isMemoryProvider + */ + public function testIsMemory(array $config, bool $expected): void + { + $this->assertSame($expected, SQLiteAdapter::isMemory($config)); + } }