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 support for MSSQL #302

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ Path to Moodle plugin

#### `--db-type`

Database type, mysqli, pgsql or mariadb
Database type, mysqli, pgsql, mariadb or sqrsrv

* Accept value: yes
* Is value required: yes
Expand Down
15 changes: 12 additions & 3 deletions gha.dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ jobs:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval 10s --health-timeout 5s --health-retries 3

mssql:
image: mcr.microsoft.com/mssql/server:2019-CU25-ubuntu-20.04
env:
ACCEPT_EULA: 'Y'
MSSQL_SA_PASSWORD: 'RequiredPassw0rd!'
ports:
- 1433:1433
options: --health-cmd="/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P RequiredPassw0rd! -Q \"SELECT 1\" -b -o /dev/null" --health-interval 10s --health-timeout 5s --health-retries 10

strategy:
fail-fast: false
matrix:
php: ['7.4', '8.0', '8.1']
moodle-branch: ['MOODLE_401_STABLE']
database: [pgsql, mariadb]
database: [pgsql, mariadb, sqlsrv]

steps:
- name: Check out repository code
Expand All @@ -44,7 +53,7 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ matrix.extensions }}
extensions: sqlsrv
ini-values: max_input_vars=5000
# If you are not using code coverage, keep "none". Otherwise, use "pcov" (Moodle 3.10 and up) or "xdebug".
# If you try to use code coverage with "none", it will fallback to phpdbg (which has known problems).
Expand All @@ -59,7 +68,7 @@ jobs:
echo "NVM_DIR=$HOME/.nvm" >> $GITHUB_ENV

- name: Install moodle-plugin-ci
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1
run: moodle-plugin-ci install --plugin ./plugin --db-host=127.0.0.1 ${{ matrix.database == 'sqlsrv' && '--db-pass=RequiredPassw0rd!' || '' }}
env:
DB: ${{ matrix.database }}
MOODLE_BRANCH: ${{ matrix.moodle-branch }}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected function configure(): void
->addOption('repo', null, InputOption::VALUE_REQUIRED, 'Moodle repository to clone', $repo)
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'Moodle git branch to clone, EG: MOODLE_29_STABLE', $branch)
->addOption('plugin', null, InputOption::VALUE_REQUIRED, 'Path to Moodle plugin', $plugin)
->addOption('db-type', null, InputOption::VALUE_REQUIRED, 'Database type, mysqli, pgsql or mariadb', $type)
->addOption('db-type', null, InputOption::VALUE_REQUIRED, 'Database type, mysqli, pgsql, mariadb or sqlsrv', $type)
->addOption('db-user', null, InputOption::VALUE_REQUIRED, 'Database user', $dbUser)
->addOption('db-pass', null, InputOption::VALUE_REQUIRED, 'Database pass', $dbPass)
->addOption('db-name', null, InputOption::VALUE_REQUIRED, 'Database name', $dbName)
Expand Down
4 changes: 2 additions & 2 deletions src/Installer/Database/DatabaseResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ private function resolveDatabaseType(string $type): AbstractDatabase
return $database;
}
}
throw new \DomainException(sprintf('Unknown database type (%s). Please use mysqli, pgsql or mariadb.', $type));
throw new \DomainException(sprintf('Unknown database type (%s). Please use mysqli, pgsql, mariadb or sqlsrv.', $type));
}

/**
* @return AbstractDatabase[]
*/
private function getDatabases(): array
{
return [new MySQLDatabase(), new PostgresDatabase(), new MariaDBDatabase()];
return [new MySQLDatabase(), new PostgresDatabase(), new MariaDBDatabase(), new MSSQLDatabase()];
}
}
42 changes: 42 additions & 0 deletions src/Installer/Database/MSSQLDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2024 Justus Dieckmann
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodlePluginCI\Installer\Database;

/**
* MSSQL Database.
*/
class MSSQLDatabase extends AbstractDatabase
{
public string $user = 'sa';
public string $type = 'sqlsrv';

public function getCreateDatabaseCommand(): array
{
$host = $this->host;
if (!empty($this->port)) {
$host .= ",$this->port";

Check warning on line 27 in src/Installer/Database/MSSQLDatabase.php

View check run for this annotation

Codecov / codecov/patch

src/Installer/Database/MSSQLDatabase.php#L27

Added line #L27 was not covered by tests
}

return array_filter([
'sqlcmd',
'-U',
$this->user,
!empty($this->pass) ? '-P' : '',
!empty($this->pass) ? $this->pass : '',
'-S',
$host,
'-Q',
sprintf('CREATE DATABASE %s COLLATE Latin1_General_CI_AI;', $this->name),
]);
}
}
5 changes: 5 additions & 0 deletions tests/Installer/Database/DatabaseResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public function testType()
'MoodlePluginCI\Installer\Database\MariaDBDatabase',
$resolver->resolveDatabase('mariadb')
);

$this->assertInstanceOf(
'MoodlePluginCI\Installer\Database\MSSQLDatabase',
$resolver->resolveDatabase('sqlsrv')
);
}

public function testTypeError()
Expand Down
30 changes: 30 additions & 0 deletions tests/Installer/Database/MSSQLDatabaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Moodle Plugin CI package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright 2024 Justus Dieckmann
* License http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace MoodlePluginCI\Tests\Installer\Database;

use MoodlePluginCI\Installer\Database\MSSQLDatabase;

class MSSQLDatabaseTest extends \PHPUnit\Framework\TestCase
{
public function testGetCreateDatabaseCommand()
{
$database = new MSSQLDatabase();
$database->name = 'TestName';
$database->user = 'TestUser';
$database->pass = 'TestPass';
$database->host = 'TestHost';

$expected = 'sqlcmd -U TestUser -P TestPass -S TestHost -Q CREATE DATABASE TestName COLLATE Latin1_General_CI_AI;';
$this->assertSame($expected, implode(' ', $database->getCreateDatabaseCommand()));
}
}
Loading