Skip to content

Commit

Permalink
Add MSSQL support
Browse files Browse the repository at this point in the history
  • Loading branch information
justusdieckmann committed May 3, 2024
1 parent 783ec09 commit 1e2f404
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 3 deletions.
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 mssql', $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()];
}
}
43 changes: 43 additions & 0 deletions src/Installer/Database/MSSQLDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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";
}

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 (c) 2018 Blackboard Inc. (http://www.blackboard.com)
* 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()));
}
}

0 comments on commit 1e2f404

Please sign in to comment.