Skip to content

Commit 07755aa

Browse files
authored
Merge pull request #161 from geichelberger/add-db-port-option
Add --db-port option
2 parents 1de4152 + 96f6a90 commit 07755aa

File tree

12 files changed

+42
-9
lines changed

12 files changed

+42
-9
lines changed

docs/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
99
The format of this change log follows the advice given at [Keep a CHANGELOG](http://keepachangelog.com).
1010

1111
## [Unreleased]
12+
### Added
13+
- `moodle-plugin-ci install` now provides an option `--db-port` to define a custom database port.
1214

1315
## [3.3.0] - 2022-06-28
1416
### Added

docs/CLI.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ Install everything required for CI testing
813813

814814
### Usage
815815

816-
* `install [--moodle MOODLE] [--data DATA] [--repo REPO] [--branch BRANCH] [--plugin PLUGIN] [--db-type DB-TYPE] [--db-user DB-USER] [--db-pass DB-PASS] [--db-name DB-NAME] [--db-host DB-HOST] [--not-paths NOT-PATHS] [--not-names NOT-NAMES] [--extra-plugins EXTRA-PLUGINS] [--no-init] [--node-version NODE-VERSION]`
816+
* `install [--moodle MOODLE] [--data DATA] [--repo REPO] [--branch BRANCH] [--plugin PLUGIN] [--db-type DB-TYPE] [--db-user DB-USER] [--db-pass DB-PASS] [--db-name DB-NAME] [--db-host DB-HOST] [--db-port DB-PORT] [--not-paths NOT-PATHS] [--not-names NOT-NAMES] [--extra-plugins EXTRA-PLUGINS] [--no-init] [--node-version NODE-VERSION]`
817817

818818
Install everything required for CI testing
819819

@@ -909,6 +909,15 @@ Database host
909909
* Is multiple: no
910910
* Default: `'localhost'`
911911

912+
#### `--db-port`
913+
914+
Database port
915+
916+
* Accept value: yes
917+
* Is value required: yes
918+
* Is multiple: no
919+
* Default: `''`
920+
912921
#### `--not-paths`
913922

914923
CSV of file paths to exclude

psalm-baseline.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</PossiblyNullIterator>
4949
</file>
5050
<file src="src/Command/InstallCommand.php">
51-
<PossiblyInvalidArgument occurrences="12">
51+
<PossiblyInvalidArgument occurrences="13">
5252
<code>$input-&gt;getOption('plugin')</code>
5353
<code>$pluginsDir</code>
5454
<code>$input-&gt;getOption('moodle')</code>
@@ -59,6 +59,7 @@
5959
<code>$input-&gt;getOption('db-user')</code>
6060
<code>$input-&gt;getOption('db-pass')</code>
6161
<code>$input-&gt;getOption('db-host')</code>
62+
<code>$input-&gt;getOption('db-port')</code>
6263
<code>$input-&gt;getOption('not-paths')</code>
6364
<code>$input-&gt;getOption('not-names')</code>
6465
</PossiblyInvalidArgument>

res/template/config.php.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ $CFG->dbname = '{{DBNAME}}';
1111
$CFG->dbuser = '{{DBUSER}}';
1212
$CFG->dbpass = '{{DBPASS}}';
1313
$CFG->prefix = 'mdl_';
14-
$CFG->dboptions = [];
14+
$CFG->dboptions = [
15+
'dbport' => '{{DBPORT}}',
16+
];
1517

1618
$CFG->wwwroot = '{{WWWROOT}}';
1719
$CFG->dataroot = '{{DATAROOT}}';

src/Bridge/MoodleConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function createContents(AbstractDatabase $database, $dataDir)
3737
'{{DBTYPE}}' => $database->type,
3838
'{{DBLIBRARY}}' => $database->library,
3939
'{{DBHOST}}' => $database->host,
40+
'{{DBPORT}}' => $database->port,
4041
'{{DBNAME}}' => $database->name,
4142
'{{DBUSER}}' => $database->user,
4243
'{{DBPASS}}' => $database->pass,

src/Command/InstallCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ protected function configure()
9494
->addOption('db-pass', null, InputOption::VALUE_REQUIRED, 'Database pass', '')
9595
->addOption('db-name', null, InputOption::VALUE_REQUIRED, 'Database name', 'moodle')
9696
->addOption('db-host', null, InputOption::VALUE_REQUIRED, 'Database host', 'localhost')
97+
->addOption('db-port', null, InputOption::VALUE_REQUIRED, 'Database port', '')
9798
->addOption('not-paths', null, InputOption::VALUE_REQUIRED, 'CSV of file paths to exclude', $paths)
9899
->addOption('not-names', null, InputOption::VALUE_REQUIRED, 'CSV of file names to exclude', $names)
99100
->addOption('extra-plugins', null, InputOption::VALUE_REQUIRED, 'Directory of extra plugins to install', $extra)
@@ -174,7 +175,8 @@ public function initializeInstallerFactory(InputInterface $input)
174175
$input->getOption('db-name'),
175176
$input->getOption('db-user'),
176177
$input->getOption('db-pass'),
177-
$input->getOption('db-host')
178+
$input->getOption('db-host'),
179+
$input->getOption('db-port')
178180
);
179181

180182
return $factory;

src/Installer/Database/AbstractDatabase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ abstract class AbstractDatabase
4545
*/
4646
public $host = 'localhost';
4747

48+
/**
49+
* Database port.
50+
*
51+
* @var string
52+
*/
53+
public $port = '';
54+
4855
/**
4956
* Moodle database type.
5057
*

src/Installer/Database/DatabaseResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class DatabaseResolver
2323
* @param string|null $user
2424
* @param string|null $pass
2525
* @param string|null $host
26+
* @param string|null $port
2627
*
2728
* @return AbstractDatabase
2829
*/
29-
public function resolveDatabase($type, $name = null, $user = null, $pass = null, $host = null)
30+
public function resolveDatabase($type, $name = null, $user = null, $pass = null, $host = null, $port = null)
3031
{
3132
$database = $this->resolveDatabaseType($type);
3233

@@ -42,6 +43,9 @@ public function resolveDatabase($type, $name = null, $user = null, $pass = null,
4243
if ($host !== null) {
4344
$database->host = $host;
4445
}
46+
if ($port !== null) {
47+
$database->port = $port;
48+
}
4549

4650
return $database;
4751
}

src/Installer/Database/MySQLDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public function getCreateDatabaseCommand()
2424
$passOpt = !empty($this->pass) ? ' --password='.escapeshellarg($this->pass) : '';
2525
$user = escapeshellarg($this->user);
2626
$host = escapeshellarg($this->host);
27+
$port = !empty($this->port) ? ' --port='.escapeshellarg($this->port) : '';
2728
$createDB = escapeshellarg(sprintf('CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', $this->name));
2829

29-
return sprintf('mysql -u %s%s -h %s -e %s', $user, $passOpt, $host, $createDB);
30+
return sprintf('mysql -u %s%s -h %s%s -e %s', $user, $passOpt, $host, $port, $createDB);
3031
}
3132
}

src/Installer/Database/PostgresDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public function getCreateDatabaseCommand()
2525
$pass = !empty($this->pass) ? 'env PGPASSWORD='.escapeshellarg($this->pass).' ' : '';
2626
$user = escapeshellarg($this->user);
2727
$host = escapeshellarg($this->host);
28+
$port = !empty($this->port) ? ' --port '.escapeshellarg($this->port) : '';
2829
$createDB = escapeshellarg(sprintf('CREATE DATABASE "%s";', $this->name));
2930

30-
return sprintf('%spsql -c %s -U %s -h %s', $pass, $createDB, $user, $host);
31+
return sprintf('%spsql -c %s -U %s -h %s%s', $pass, $createDB, $user, $host, $port);
3132
}
3233
}

0 commit comments

Comments
 (0)