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

Append sqlite suffix to name on info output #2307

Merged
merged 3 commits into from
Sep 6, 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
7 changes: 6 additions & 1 deletion src/Phinx/Console/Command/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Phinx\Config\Config;
use Phinx\Config\ConfigInterface;
use Phinx\Db\Adapter\AdapterInterface;
use Phinx\Db\Adapter\SQLiteAdapter;
use Phinx\Migration\Manager;
use Phinx\Util\Util;
use RuntimeException;
Expand Down Expand Up @@ -465,7 +466,11 @@ protected function writeInformationOutput(?string &$environment, OutputInterface
}

if (isset($envOptions['name'])) {
$output->writeln('<info>using database</info> ' . $envOptions['name'], $this->verbosityLevel);
$name = $envOptions['name'];
if ($envOptions['adapter'] === 'sqlite') {
$name .= SQLiteAdapter::getSuffix($envOptions);
}
$output->writeln('<info>using database</info> ' . $name, $this->verbosityLevel);
} else {
$output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');

Expand Down
33 changes: 26 additions & 7 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class SQLiteAdapter extends PdoAdapter
{
public const MEMORY = ':memory:';

public const DEFAULT_SUFFIX = '.sqlite3';

/**
* List of supported Phinx column types with their SQL equivalents
* some types have an affinity appended to ensure they do not receive NUMERIC affinity
Expand Down Expand Up @@ -121,7 +123,7 @@ class SQLiteAdapter extends PdoAdapter
/**
* @var string
*/
protected string $suffix = '.sqlite3';
protected string $suffix = self::DEFAULT_SUFFIX;

/**
* Indicates whether the database library version is at least the specified version
Expand Down Expand Up @@ -195,21 +197,38 @@ public function connect(): void
}

/**
* @inheritDoc
* Get the suffix to use for the SQLite database file.
*
* @param array $options Environment options
* @return string
*/
public function setOptions(array $options): AdapterInterface
public static function getSuffix(array $options): string
{
parent::setOptions($options);
if ($options['name'] === self::MEMORY) {
return '';
}

$suffix = self::DEFAULT_SUFFIX;
if (isset($options['suffix'])) {
$this->suffix = $options['suffix'];
$suffix = $options['suffix'];
}
//don't "fix" the file extension if it is blank, some people
//might want a SQLITE db file with absolutely no extension.
if ($this->suffix !== '' && strpos($this->suffix, '.') !== 0) {
$this->suffix = '.' . $this->suffix;
if ($suffix !== '' && strpos($suffix, '.') !== 0) {
$suffix = '.' . $suffix;
}

return $suffix;
}

/**
* @inheritDoc
*/
public function setOptions(array $options): AdapterInterface
{
parent::setOptions($options);
$this->suffix = self::getSuffix($options);

return $this;
}

Expand Down
Loading