Skip to content

Commit

Permalink
feat: separate mariadb driver
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Nov 29, 2024
1 parent d90573f commit 5d6f465
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/REUSABLE_backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
driver: mysql
- service: mariadb
db: MariaDB
driver: mysql
driver: mariadb
- service: 'mysql:8.1.0'
db: MySQL 8.1
driver: mysql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function getTimedCounts(Builder $query, string $column, ?DateTime $start
$dbFormattedDatetime = match ($query->getConnection()->getDriverName()) {
'sqlite' => "strftime($format, $column)",
'pgsql' => "TO_CHAR($column, $format)",
'mysql' => "DATE_FORMAT($column, $format)",
'mysql', 'mariadb' => "DATE_FORMAT($column, $format)",
default => throw new Exception('Unsupported database driver'),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
* LICENSE file that was distributed with this source code.
*/

use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\Builder;

return [
'up' => function (Builder $schema) {
$connection = $schema->getConnection();

if ($connection->getDriverName() === 'mysql') {
if ($connection->getDriverName() instanceof MySqlConnection) {
$prefix = $connection->getTablePrefix();
$connection->statement('ALTER TABLE '.$prefix.'posts ENGINE = InnoDB');
}
Expand All @@ -22,7 +23,7 @@
'down' => function (Builder $schema) {
$connection = $schema->getConnection();

if ($connection->getDriverName() === 'mysql') {
if ($connection->getDriverName() instanceof MySqlConnection) {
$prefix = $connection->getTablePrefix();
$connection->statement('ALTER TABLE '.$prefix.'posts ENGINE = MyISAM');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

use Illuminate\Database\MariaDbConnection;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

Expand All @@ -26,14 +27,16 @@
$table->json('preferences_json')->nullable();
});

if ($connection instanceof MariaDbConnection) {
$connection->table('users')->update([
'preferences_json' => $connection->raw("IF(JSON_VALID(CONVERT($preferences USING utf8mb4)), CONVERT($preferences USING utf8mb4), NULL)"),
]);
} elseif ($driver === 'mysql') {
$connection->table('users')->update([
'preferences_json' => $connection->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"),
]);
if ($connection instanceof MySqlConnection) {
if ($connection->isMaria()) {
$connection->table('users')->update([
'preferences_json' => $connection->raw("IF(JSON_VALID(CONVERT($preferences USING utf8mb4)), CONVERT($preferences USING utf8mb4), NULL)"),
]);
} else {
$connection->table('users')->update([
'preferences_json' => $connection->raw("CAST(CONVERT($preferences USING utf8mb4) AS JSON)"),
]);
}
}

$schema->table('users', function (Blueprint $table) {
Expand All @@ -60,7 +63,7 @@
$table->binary('preferences_binary')->nullable();
});

if ($driver === 'mysql') {
if ($connection instanceof MySqlConnection) {
$connection->table('users')->update([
'preferences_binary' => $connection->raw($preferences),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

use Illuminate\Database\MariaDbConnection;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;

Expand All @@ -25,14 +26,16 @@
$table->json('data_json')->nullable();
});

if ($connection instanceof MariaDbConnection) {
$connection->table('notifications')->update([
'data_json' => $connection->raw('IF(JSON_VALID(CONVERT(data USING utf8mb4)), CONVERT(data USING utf8mb4), NULL)'),
]);
} elseif ($driver === 'mysql') {
$connection->table('notifications')->update([
'data_json' => $connection->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'),
]);
if ($connection instanceof MySqlConnection) {
if ($connection->isMaria()) {
$connection->table('notifications')->update([
'data_json' => $connection->raw('IF(JSON_VALID(CONVERT(data USING utf8mb4)), CONVERT(data USING utf8mb4), NULL)'),
]);
} else {
$connection->table('notifications')->update([
'data_json' => $connection->raw('CAST(CONVERT(data USING utf8mb4) AS JSON)'),
]);
}
}

$schema->table('notifications', function (Blueprint $table) {
Expand All @@ -58,7 +61,7 @@
$table->binary('data_binary')->nullable();
});

if ($driver === 'mysql') {
if ($connection instanceof MySqlConnection) {
$connection->table('notifications')->update([
'data_binary' => $connection->raw('data'),
]);
Expand Down
3 changes: 2 additions & 1 deletion framework/core/src/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function register(): void

$config = $container['flarum']->config('database');

if ($config['driver'] === 'mysql') {
if (in_array($config['driver'], ['mysql', 'mariadb'])) {
$config['engine'] = 'InnoDB';
} elseif ($config['driver'] === 'sqlite' && ! file_exists($config['database'])) {
$config['database'] = $container->make(Paths::class)->base.'/'.$config['database'];
Expand Down Expand Up @@ -85,6 +85,7 @@ protected function registerBuilderMacros(): void
{
$drivers = [
'mysql' => 'MySql',
'mariadb' => 'MariaDb',
'pgsql' => 'PgSql',
'sqlite' => 'Sqlite',
];
Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Discussion/Search/FulltextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(
public function search(SearchState $state, string $value): void
{
match ($state->getQuery()->getConnection()->getDriverName()) {
'mysql' => $this->mysql($state, $value),
'mysql', 'mariadb' => $this->mysql($state, $value),
'pgsql' => $this->pgsql($state, $value),
'sqlite' => $this->sqlite($state, $value),
default => throw new RuntimeException('Unsupported database driver: '.$state->getQuery()->getConnection()->getDriverName()),
Expand Down
3 changes: 2 additions & 1 deletion framework/core/src/Foundation/ApplicationInfoProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function identifyQueueDriver(): string
public function identifyDatabaseVersion(): string
{
return match ($this->config['database.driver']) {
'mysql', 'pgsql' => $this->db->selectOne('select version() as version')->version,
'mysql', 'mariadb', 'pgsql' => $this->db->selectOne('select version() as version')->version,
'sqlite' => $this->db->selectOne('select sqlite_version() as version')->version,
default => 'Unknown',
};
Expand All @@ -81,6 +81,7 @@ public function identifyDatabaseDriver(): string
{
return match ($this->config['database.driver']) {
'mysql' => 'MySQL',
'mariadb' => 'MariaDB',
'pgsql' => 'PostgreSQL',
'sqlite' => 'SQLite',
default => $this->config['database.driver'],
Expand Down
6 changes: 3 additions & 3 deletions framework/core/src/Install/Console/UserDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function configure(Installation $installation): Installation

private function getDatabaseConfiguration(): DatabaseConfig
{
$driver = $this->ask('Database driver (mysql, sqlite, pgsql) (Default: mysql):', 'mysql');
$driver = $this->ask('Database driver (mysql, mariadb, sqlite, pgsql) (Default: mysql):', 'mysql');
$port = match ($driver) {
'mysql' => 3306,
'mysql', 'mariadb' => 3306,
'pgsql' => 5432,
default => 0,
};

if (in_array($driver, ['mysql', 'pgsql'])) {
if (in_array($driver, ['mysql', 'mariadb', 'pgsql'])) {
$host = $this->ask('Database host (required):', required: true);

if (Str::contains($host, ':')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private function makeDatabaseConfig(array $input): DatabaseConfig
$driver = Arr::get($input, 'dbDriver');
$host = Arr::get($input, 'dbHost');
$port = match ($driver) {
'mysql' => 3306,
'mysql', 'mariadb' => 3306,
'pgsql' => 5432,
default => 0,
};
Expand Down
12 changes: 6 additions & 6 deletions framework/core/src/Install/DatabaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ private function validate(): void
throw new ValidationFailed('Please specify a database driver.');
}

if (! in_array($this->driver, ['mysql', 'sqlite', 'pgsql'])) {
throw new ValidationFailed('Currently, only MySQL/MariaDB and SQLite are supported.');
if (! in_array($this->driver, ['mysql', 'mariadb', 'sqlite', 'pgsql'])) {
throw new ValidationFailed('Currently, only MySQL, MariaDB, SQLite and PostgreSQL are supported.');
}

if (in_array($this->driver, ['mysql', 'pgsql']) && empty($this->host)) {
if (in_array($this->driver, ['mysql', 'mariadb', 'pgsql']) && empty($this->host)) {
throw new ValidationFailed('Please specify the hostname of your database server.');
}

if (in_array($this->driver, ['mysql', 'pgsql']) && ($this->port < 1 || $this->port > 65535)) {
if (in_array($this->driver, ['mysql', 'mariadb', 'pgsql']) && ($this->port < 1 || $this->port > 65535)) {
throw new ValidationFailed('Please provide a valid port number between 1 and 65535.');
}

if (empty($this->database)) {
throw new ValidationFailed('Please specify the database name.');
}

if (in_array($this->driver, ['mysql', 'pgsql']) && empty($this->username)) {
if (in_array($this->driver, ['mysql', 'mariadb', 'pgsql']) && empty($this->username)) {
throw new ValidationFailed('Please specify the username for accessing the database.');
}

Expand All @@ -84,7 +84,7 @@ public function prepare(Paths $paths): void
private function driverOptions(): array
{
return match ($this->driver) {
'mysql' => [
'mysql', 'mariadb' => [
'host' => $this->host,
'port' => $this->port,
'username' => $this->username,
Expand Down
23 changes: 23 additions & 0 deletions framework/core/src/Install/Steps/ConnectToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
use Closure;
use Flarum\Install\DatabaseConfig;
use Flarum\Install\Step;
use Illuminate\Database\Connectors\MariaDbConnector;
use Illuminate\Database\Connectors\MySqlConnector;
use Illuminate\Database\Connectors\PostgresConnector;
use Illuminate\Database\Connectors\SQLiteConnector;
use Illuminate\Database\MariaDbConnection;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SQLiteConnection;
Expand Down Expand Up @@ -42,6 +44,7 @@ public function run(): void

match ($config['driver']) {
'mysql' => $this->mysql($config),
'mariadb' => $this->mariadb($config),
'pgsql' => $this->pgsql($config),
'sqlite' => $this->sqlite($config),
default => throw new InvalidArgumentException('Unsupported database driver: '.$config['driver']),
Expand Down Expand Up @@ -74,6 +77,26 @@ private function mysql(array $config): void
);
}

private function mariadb(array $config): void
{
$pdo = (new MariaDbConnector())->connect($config);

$version = $pdo->query('SELECT VERSION()')->fetchColumn();

if (version_compare($version, '10.3.0', '<')) {
throw new RangeException("MariaDB version ($version) too low. You need at least MariaDB 10.3");
}

($this->store)(
new MariaDbConnection(
$pdo,
$config['database'],
$config['prefix'],
$config
)
);
}

private function pgsql(array $config): void
{
$pdo = (new PostgresConnector)->connect($config);
Expand Down
2 changes: 1 addition & 1 deletion framework/core/src/Post/Filter/FulltextFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
public function search(SearchState $state, string $value): void
{
match ($state->getQuery()->getConnection()->getDriverName()) {
'mysql' => $this->mysql($state, $value),
'mysql', 'mariadb' => $this->mysql($state, $value),
'pgsql' => $this->pgsql($state, $value),
'sqlite' => $this->sqlite($state, $value),
default => throw new RuntimeException('Unsupported database driver: '.$state->getQuery()->getConnection()->getDriverName()),
Expand Down
3 changes: 2 additions & 1 deletion framework/core/views/install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<label>Database Driver</label>
<select class="FormControl" name="dbDriver">
<option value="mysql">MySQL</option>
<option value="mariadb">MariaDB</option>
<option value="pgsql">PostgreSQL</option>
<option value="sqlite">SQLite</option>
</select>
Expand All @@ -35,7 +36,7 @@
<input class="FormControl" name="dbName" value="flarum">
</div>
<div data-group="mysql,pgsql">
<div data-group="mysql,mariadb,pgsql">
<div class="FormField">
<label>Host</label>
<input class="FormControl" name="dbHost" value="localhost">
Expand Down
2 changes: 1 addition & 1 deletion php-packages/testing/src/integration/Setup/SetupScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct()
$this->driver = getenv('DB_DRIVER') ?: 'mysql';
$this->host = getenv('DB_HOST') ?: 'localhost';
$this->port = intval(getenv('DB_PORT') ?: match ($this->driver) {
'mysql' => 3306,
'mysql', 'mariadb' => 3306,
'pgsql' => 5432,
default => 0,
});
Expand Down

0 comments on commit 5d6f465

Please sign in to comment.