Skip to content
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

Add SQLiteAdapter::isMemory helper function #2336

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading