Skip to content

Commit

Permalink
Merge pull request #219 from kitloong/feature/mariadb
Browse files Browse the repository at this point in the history
Add MariaDB Driver
  • Loading branch information
kitloong committed Jun 1, 2024
2 parents 1742526 + f376da2 commit ca7d318
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/Database/Models/MySQL/MySQLColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class MySQLColumnType extends DatabaseColumnType
'varchar' => ColumnType::STRING,
'year' => ColumnType::YEAR,

// For MariaDB
'uuid' => ColumnType::UUID,

// Removed from Laravel v11
'geomcollection' => ColumnType::GEOMETRY_COLLECTION,
'linestring' => ColumnType::LINE_STRING,
Expand Down
9 changes: 5 additions & 4 deletions src/Enum/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
*/
enum Driver: string
{
case MYSQL = 'mysql';
case PGSQL = 'pgsql';
case SQLITE = 'sqlite';
case SQLSRV = 'sqlsrv';
case MARIADB = 'mariadb';
case MYSQL = 'mysql';
case PGSQL = 'pgsql';
case SQLITE = 'sqlite';
case SQLSRV = 'sqlsrv';
}
8 changes: 4 additions & 4 deletions src/MigrateGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,10 @@ protected function makeSchema(): Schema
}

return match ($driver) {
Driver::MYSQL->value => $this->schema = app(MySQLSchema::class),
Driver::PGSQL->value => $this->schema = app(PgSQLSchema::class),
Driver::SQLITE->value => $this->schema = app(SQLiteSchema::class),
Driver::SQLSRV->value => $this->schema = app(SQLSrvSchema::class),
Driver::MARIADB->value, Driver::MYSQL->value => $this->schema = app(MySQLSchema::class),
Driver::PGSQL->value => $this->schema = app(PgSQLSchema::class),
Driver::SQLITE->value => $this->schema = app(SQLiteSchema::class),
Driver::SQLSRV->value => $this->schema = app(SQLSrvSchema::class),
default => throw new Exception('The database driver in use is not supported.'),
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/TableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ private function makeMigrationPath(string $table): string
*/
private function shouldSetCharset(): bool
{
if (DB::getDriverName() !== Driver::MYSQL->value) {
if (!in_array(DB::getDriverName(), [Driver::MARIADB->value, Driver::MYSQL->value])) {
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/Support/AssetNameQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function quoteIdentifier(string $value): string
case Driver::SQLSRV->value:
return $value === '*' ? $value : '[' . str_replace(']', ']]', $value) . ']';

case Driver::MARIADB->value:
case Driver::MYSQL->value:
return $value === '*' ? $value : '`' . str_replace('`', '``', $value) . '`';

Expand Down
4 changes: 0 additions & 4 deletions src/Support/CheckLaravelVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ trait CheckLaravelVersion
{
public function atLeastLaravel11(): bool
{
if (App::version() === '11.x-dev') {
return true;
}

return $this->atLeastLaravelVersion('11.0');
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Feature/MariaDB/MariaDBTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use KitLoong\MigrationsGenerator\Support\CheckLaravelVersion;
use KitLoong\MigrationsGenerator\Tests\Feature\FeatureTestCase;
use PDO;

abstract class MariaDBTestCase extends FeatureTestCase
{
use CheckLaravelVersion;

/**
* @inheritDoc
*/
Expand All @@ -18,7 +21,7 @@ protected function getEnvironmentSetUp($app): void

$app['config']->set('database.default', 'mariadb');
$app['config']->set('database.connections.mariadb', [
'driver' => 'mysql',
'driver' => $this->atLeastLaravel11() ? 'mariadb' : 'mysql',
'url' => null,
'host' => env('MARIADB_HOST'),
'port' => env('MARIADB_PORT'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function up()
case Driver::SQLSRV->value:
$collation = 'Latin1_General_100_CI_AI_SC_UTF8';
break;
case Driver::MARIADB->value:
case Driver::MYSQL->value:
$collation = 'utf8_unicode_ci';
break;
Expand Down Expand Up @@ -61,7 +62,7 @@ public function up()
$table->text('text_charset')->charset('utf8');
$table->text('text_collation')->collation($collation);

if (DB::getDriverName() === Driver::MYSQL->value) {
if (in_array(DB::getDriverName(), [Driver::MARIADB->value, Driver::MYSQL->value])) {
$table->set('set', ['strawberry', 'vanilla']);
$table->set('set_default', ['strawberry', 'vanilla'])->default('strawberry');
$table->set('set_charset', ['strawberry', 'vanilla'])->charset('utf8');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function up()
// https://github.com/laravel/framework/pull/49634
if ($this->atLeastLaravel11()) {
if (
DB::getDriverName() !== Driver::MYSQL->value ||
!in_array(DB::getDriverName(), [Driver::MARIADB->value, Driver::MYSQL->value]) ||
version_compare(DB::getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION), '5.8', '>')
) {
$table->geography('geography');
Expand Down Expand Up @@ -189,13 +189,15 @@ public function up()
$table->ulid('ulid');

switch (DB::getDriverName()) {
case Driver::MARIADB->value:
case Driver::MYSQL->value:
$table->set('set', ['strawberry', 'vanilla']);
break;
default:
}

switch (DB::getDriverName()) {
case Driver::MARIADB->value:
case Driver::MYSQL->value:
$table->string('virtual')->nullable()->virtualAs('CONCAT(string, " ", string_255)');
$table->string('stored')->nullable()->storedAs("CONCAT(string_255, ' ', string)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function up()
}
}

if (in_array(DB::getDriverName(), [Driver::MYSQL->value, Driver::PGSQL->value])) {
if (in_array(DB::getDriverName(), [Driver::MARIADB->value, Driver::MYSQL->value, Driver::PGSQL->value])) {
$table->string('fulltext')->fulltext();
$table->string('fulltext_custom')->fulltext('fulltext_custom');
$table->fullText(['col_multi1', 'col_multi2']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public function up()
{
switch (DB::getDriverName()) {
case Driver::MARIADB->value:
case Driver::MYSQL->value:
DB::unprepared(
"CREATE PROCEDURE findNameWithHyphen()
Expand Down

0 comments on commit ca7d318

Please sign in to comment.