Skip to content

Commit

Permalink
Add SQLiteAdapter::isMemory helper function (#2336)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored Dec 14, 2024
1 parent fa2b585 commit 6f0ec12
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 13 additions & 2 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 '';
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 6f0ec12

Please sign in to comment.