Skip to content

Commit

Permalink
Append sqlite suffix to name on info output (#2307)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored Sep 6, 2024
1 parent cbcd067 commit a093a96
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
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

0 comments on commit a093a96

Please sign in to comment.