-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Support Mariadb Builder
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
Check failure on line 1 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php
|
||
|
||
namespace Envor\SchemaMacros\MariaDb; | ||
|
||
use Stringable; | ||
|
||
/** | ||
* Create the database if it does not exist | ||
* | ||
* @param string|Stringable $database | ||
* | ||
* @mixin \Illuminate\Database\Schema\MariaDbBuilder | ||
* | ||
* @return bool | ||
*/ | ||
class MariaDbCreateDatabaseIfNotExists | ||
{ | ||
public function __invoke(): callable | ||
{ | ||
return function (string|Stringable $database): bool { | ||
$database = (string) $database; | ||
|
||
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */ | ||
if ($this->databaseExists($database)) { | ||
Check failure on line 24 in src/MariaDb/MariaDbCreateDatabaseIfNotExists.php
|
||
return false; | ||
} | ||
|
||
return $this->createDatabase($database); | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
Check failure on line 1 in src/MariaDb/MariaDbDatabaseExists.php
|
||
|
||
namespace Envor\SchemaMacros\MariaDb; | ||
|
||
use Stringable; | ||
|
||
/** | ||
* determine if the database exists | ||
* | ||
* @param string|Stringable $database | ||
* | ||
* @mixin \Illuminate\Database\Schema\MariaDbBuilder | ||
* | ||
* @return bool | ||
*/ | ||
class MariaDbDatabaseExists | ||
{ | ||
public function __invoke(): callable | ||
{ | ||
return function (string|Stringable $database): bool { | ||
$database = (string) $database; | ||
|
||
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */ | ||
return (bool) $this->getConnection()->select("SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{$database}'"); | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
Check failure on line 1 in src/MariaDb/MariaDbEmptyTrash.php
|
||
|
||
namespace Envor\SchemaMacros\MariaDb; | ||
|
||
use Envor\SchemaMacros\SchemaMacros; | ||
use Illuminate\Support\Carbon; | ||
|
||
/** | ||
* Erase trashed databases | ||
* | ||
* @param int $daysOld | ||
* | ||
* @mixin \Illuminate\Database\Schema\MariaDbBuilder | ||
* | ||
* @return int | ||
*/ | ||
class MariaDbEmptyTrash | ||
{ | ||
public function __invoke(): callable | ||
{ | ||
return function (int $daysOld = 0): int { | ||
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */ | ||
$trashDatabases = collect($this->getConnection()->select("SHOW DATABASES LIKE 'trashed_%'")) | ||
->map(fn ($database) => array_values(get_object_vars($database))[0]); | ||
|
||
$deleted = 0; | ||
|
||
foreach ($trashDatabases as $trashDatabase) { | ||
$trashDatabase = (string) $trashDatabase; | ||
|
||
if ($daysOld > 0) { | ||
$dateSlice = array_slice(explode('_', $trashDatabase), 1, 6); | ||
$dateString = implode('_', $dateSlice); | ||
$date = Carbon::createFromFormat(SchemaMacros::TRASH_DATE_FORMAT, $dateString)->getTimestamp(); | ||
|
||
if ($date > now()->subDays($daysOld)->getTimestamp()) { | ||
continue; | ||
} | ||
} | ||
|
||
if ($this->dropDatabaseIfExists($trashDatabase)) { | ||
$deleted++; | ||
} | ||
} | ||
|
||
return $deleted; | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace Envor\SchemaMacros\MariaDb; | ||
|
||
use Envor\SchemaMacros\SchemaMacros; | ||
use Illuminate\Support\Facades\DB; | ||
use Stringable; | ||
|
||
/** | ||
* Move the database to the trash | ||
* | ||
* @param string|Stringable $database | ||
* | ||
* @mixin \Illuminate\Database\Schema\MariaDbBuilder | ||
* | ||
* @return bool|string | ||
*/ | ||
class MariaDbTrashDatabase | ||
{ | ||
public function __invoke(): callable | ||
{ | ||
return function (string|Stringable $database) { | ||
$database = (string) $database; | ||
|
||
/** @var \Illuminate\Database\Schema\MariaDbBuilder $this */ | ||
if (! $this->mariadbDatabaseExists($database)) { | ||
return false; | ||
} | ||
|
||
try { | ||
|
||
$trashedAt = now()->format(SchemaMacros::TRASH_DATE_FORMAT); | ||
$trashedDatabase = "trashed_{$trashedAt}_{$database}"; | ||
$this->dropDatabaseIfExists($trashedDatabase); | ||
$this->createDatabase($trashedDatabase); | ||
|
||
$currentConnection = $this->getConnection(); | ||
$currentConnectionName = $currentConnection->getName(); | ||
|
||
config(['database.connections.new_connection_for_database' => array_merge(config("database.connections.{$currentConnectionName}"), [ | ||
'database' => $database, | ||
])]); | ||
|
||
$db = DB::connection('new_connection_for_database'); | ||
|
||
$db->statement("SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';"); | ||
|
||
$tables = $this->getTableListing(); | ||
|
||
foreach ($tables as $table) { | ||
$db->statement("create table if not exists `{$trashedDatabase}`.`{$table}` like `{$database}`.`{$table}`;"); | ||
$db->statement("insert into `{$trashedDatabase}`.`{$table}` select * from `{$database}`.`{$table}`;"); | ||
} | ||
|
||
$this->dropDatabaseIfExists($database); | ||
$this->setConnection($currentConnection); | ||
|
||
return $trashedDatabase; | ||
} catch (\Exception $e) { | ||
return false; | ||
} | ||
}; | ||
} | ||
} |