Skip to content

Commit

Permalink
common: Support MySQL through socket
Browse files Browse the repository at this point in the history
Unlike PostgreSQL, MySQL’s DSN needs a separate option for UNIX sockets.
It also cannot handle ports with socket.

This will be useful for running tests with internal MySQL instance.
  • Loading branch information
jtojnar committed Nov 4, 2021
1 parent 0c31244 commit b570f49
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Data directory can be configured ([#1043](https://github.com/fossar/selfoss/pull/1043))
- New spout for searching Twitter (e.g. following hashtags) was added. ([#1213](https://github.com/fossar/selfoss/pull/1213))
- Added option `reading_speed_wpm` for showing estimated reading time. ([#1232](https://github.com/fossar/selfoss/pull/1232))
- Added option `db_socket` for connecting to MySQL database through UNIX domain. ([#1284](https://github.com/fossar/selfoss/pull/1284))
- Search query is now part of URL. ([#1216](https://github.com/fossar/selfoss/pull/1216))
- Search will be carried out using regular expressions when the search query is wrapped in forward slashes, e.g. `/regex/`. The expression syntax is database specific. ([#1205](https://github.com/fossar/selfoss/pull/1205))
- YouTube spout now supports following playlists. ([#1260](https://github.com/fossar/selfoss/pull/1260))
Expand Down
6 changes: 6 additions & 0 deletions docs/content/docs/administration/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ Table prefix for MySQL/SQLite databases. This is useful to avoid conflicts when
Port for database connections. By default `3306` will be used for MySQL and `5432` for PostgreSQL.
</div>

### `db_socket`
<div class="config-option">

A UNIX domain socket used for connecting to the MySQL database server. Usually, you want to use `db_host=localhost`, which should use the default socket path (typically `/run/mysqld/mysqld.sock` for MySQL or `/run/postgresql` for PostgreSQL) but if you need to specify a different location, you can. This is orthogonal to `db_host` option.
</div>

### `logger_destination`
<div class="config-option">

Expand Down
17 changes: 15 additions & 2 deletions src/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
$dice->addRule(daos\SourcesInterface::class, $shared);
$dice->addRule(daos\TagsInterface::class, $shared);

if ($configuration->isChanged('dbSocket') && $configuration->isChanged('dbHost')) {
echo 'You cannot set both `db_socket` and `db_host` options.' . PHP_EOL;
exit;
}

// Database connection
if ($configuration->dbType === 'sqlite') {
$db_file = $configuration->dbFile;
Expand All @@ -93,16 +98,21 @@
touch($db_file);
}

// https://www.php.net/manual/en/ref.pdo-sqlite.connection.php
$dsn = 'sqlite:' . $db_file;
$dbParams = [
$dsn,
];
} elseif ($configuration->dbType === 'mysql') {
$socket = $configuration->dbSocket;
$host = $configuration->dbHost;
$port = $configuration->dbPort;
$database = $configuration->dbDatabase;

if ($port !== null) {
// https://www.php.net/manual/en/ref.pdo-mysql.connection.php
if ($socket !== null) {
$dsn = "mysql:unix_socket=$socket; dbname=$database";
} elseif ($port !== null) {
$dsn = "mysql:host=$host; port=$port; dbname=$database";
} else {
$dsn = "mysql:host=$host; dbname=$database";
Expand All @@ -115,10 +125,13 @@
[PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4;'],
];
} elseif ($configuration->dbType === 'pgsql') {
$host = $configuration->dbHost;
$socket = $configuration->dbSocket;
// PostgreSQL uses host key for socket.
$host = $configuration->dbSocket !== null ? $configuration->dbSocket : $configuration->dbHost;
$port = $configuration->dbPort;
$database = $configuration->dbDatabase;

// https://www.php.net/manual/en/ref.pdo-pgsql.connection.php
if ($port !== null) {
$dsn = "pgsql:host=$host; port=$port; dbname=$database";
} else {
Expand Down
18 changes: 18 additions & 0 deletions src/helpers/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Configuration {
'ftrssCustomDataDir',
];

/** @var array<string, bool> Keeps track of options that have been changed. */
private $modifiedOptions = [];

// Internal but overridable values.

/** @var int debugging level @internal */
Expand Down Expand Up @@ -56,6 +59,9 @@ class Configuration {
/** @var ?int */
public $dbPort = null;

/** @var ?string */
public $dbSocket = null;

/** @var string */
public $dbPrefix = '';

Expand Down Expand Up @@ -217,6 +223,7 @@ public function __construct($configPath = null, $environment = []) {
}

$this->{$propertyName} = $value;
$this->modifiedOptions[$propertyName] = true;
}

// Interpolate variables in the config values.
Expand All @@ -226,4 +233,15 @@ public function __construct($configPath = null, $environment = []) {
$this->{$property} = str_replace('%datadir%', $datadir, $value);
}
}

/**
* Checks whether given configuration option has been changed.
*
* @param string $key
*
* @return bool
*/
public function isChanged($key) {
return isset($this->modifiedOptions[$key]);
}
}

0 comments on commit b570f49

Please # to comment.