From 6b75084ea2742dcdc47782bd28096356cbfcb5b7 Mon Sep 17 00:00:00 2001 From: Justus Dieckmann Date: Fri, 3 May 2024 17:52:24 +0200 Subject: [PATCH 1/2] Add MSSQL support --- docs/CLI.md | 2 +- src/Command/InstallCommand.php | 2 +- src/Installer/Database/DatabaseResolver.php | 4 +- src/Installer/Database/MSSQLDatabase.php | 42 +++++++++++++++++++ .../Database/DatabaseResolverTest.php | 5 +++ .../Installer/Database/MSSQLDatabaseTest.php | 30 +++++++++++++ 6 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 src/Installer/Database/MSSQLDatabase.php create mode 100644 tests/Installer/Database/MSSQLDatabaseTest.php diff --git a/docs/CLI.md b/docs/CLI.md index d11fcd7d..6ef4b780 100644 --- a/docs/CLI.md +++ b/docs/CLI.md @@ -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 diff --git a/src/Command/InstallCommand.php b/src/Command/InstallCommand.php index 827bf0ed..44659d63 100644 --- a/src/Command/InstallCommand.php +++ b/src/Command/InstallCommand.php @@ -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) diff --git a/src/Installer/Database/DatabaseResolver.php b/src/Installer/Database/DatabaseResolver.php index 84452740..50aa2599 100644 --- a/src/Installer/Database/DatabaseResolver.php +++ b/src/Installer/Database/DatabaseResolver.php @@ -65,7 +65,7 @@ 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)); } /** @@ -73,6 +73,6 @@ private function resolveDatabaseType(string $type): AbstractDatabase */ private function getDatabases(): array { - return [new MySQLDatabase(), new PostgresDatabase(), new MariaDBDatabase()]; + return [new MySQLDatabase(), new PostgresDatabase(), new MariaDBDatabase(), new MSSQLDatabase()]; } } diff --git a/src/Installer/Database/MSSQLDatabase.php b/src/Installer/Database/MSSQLDatabase.php new file mode 100644 index 00000000..75e81374 --- /dev/null +++ b/src/Installer/Database/MSSQLDatabase.php @@ -0,0 +1,42 @@ +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), + ]); + } +} diff --git a/tests/Installer/Database/DatabaseResolverTest.php b/tests/Installer/Database/DatabaseResolverTest.php index 496dc143..05ab32de 100644 --- a/tests/Installer/Database/DatabaseResolverTest.php +++ b/tests/Installer/Database/DatabaseResolverTest.php @@ -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() diff --git a/tests/Installer/Database/MSSQLDatabaseTest.php b/tests/Installer/Database/MSSQLDatabaseTest.php new file mode 100644 index 00000000..3c596316 --- /dev/null +++ b/tests/Installer/Database/MSSQLDatabaseTest.php @@ -0,0 +1,30 @@ +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())); + } +} From 0ddd3c9d4df4871a4554b46335c3514e83cdb7e4 Mon Sep 17 00:00:00 2001 From: Justus Dieckmann Date: Fri, 3 May 2024 20:52:11 +0200 Subject: [PATCH 2/2] Update gha.dist.yml to include MSSQL --- gha.dist.yml | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/gha.dist.yml b/gha.dist.yml index 3cdfd061..7c87bc4f 100644 --- a/gha.dist.yml +++ b/gha.dist.yml @@ -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 @@ -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). @@ -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 }}