Skip to content

Commit

Permalink
refactor: use strategy design pattern instead of all database methods…
Browse files Browse the repository at this point in the history
… in one file
  • Loading branch information
WatheqAlshowaiter committed Aug 26, 2024
1 parent 851b167 commit 29f5e3a
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 74 deletions.
104 changes: 30 additions & 74 deletions src/BackupTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Console\Output\ConsoleOutput;
use WatheqAlshowaiter\BackupTables\BackupTablesStrategy\SqliteBackupStrategy;
use WatheqAlshowaiter\BackupTables\BackupTablesStrategy\MysqlBackupStrategy;
use WatheqAlshowaiter\BackupTables\BackupTablesStrategy\MariaDbBackupStrategy;
use WatheqAlshowaiter\BackupTables\BackupTablesStrategy\SqlServerBackupStrategy;
use WatheqAlshowaiter\BackupTables\BackupTablesStrategy\PostgresBackupStrategy;

class BackupTablesService
{
Expand Down Expand Up @@ -44,6 +49,9 @@ public function generateBackup($tablesToBackup, string $dataTimeText = 'Y_m_d_H_
return false;
}

/**
* @throws Exception
*/
protected function processBackup(array $tablesToBackup = [], $dateTimeFormat = 'Y_m_d_H_i_s'): array
{
$currentDateTime = now()->format($dateTimeFormat);
Expand All @@ -67,27 +75,12 @@ protected function processBackup(array $tablesToBackup = [], $dateTimeFormat = '

$databaseDriver = DB::connection()->getDriverName();

$backupStrategy = $this->getBackupStrategy($databaseDriver);

Schema::disableForeignKeyConstraints();

switch ($databaseDriver) {
case 'sqlite':
$this->response[] = $this->backupTablesForSqlite($newTableName, $table);
break;
case 'mysql':
$this->response[] = $this->backupTablesForForMysql($newTableName, $table);
break;
case 'mariadb':
$this->response[] = $this->backupTablesForForMariaDb($newTableName, $table);
break;
case 'pgsql':
$this->response[] = $this->backupTablesForForPostgres($newTableName, $table);
break;
case 'sqlsrv':
$this->response[] = $this->backupTablesForForSqlServer($newTableName, $table);
break;
default:
throw new Exception('NOT SUPPORTED DATABASE DRIVER');
}
$this->response[] = $backupStrategy->backup($newTableName, $table);

Schema::enableForeignKeyConstraints();
}

Expand All @@ -96,59 +89,6 @@ protected function processBackup(array $tablesToBackup = [], $dateTimeFormat = '
];
}

protected function backupTablesForSqlite($newTableName, $table): array
{
DB::statement(/**@lang SQLite */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
DB::statement(/**@lang SQLite */ "INSERT INTO $newTableName SELECT * FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

protected function backupTablesForForMysql($newTableName, $table): array
{

if ($this->getMysqlVersion() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT) {
DB::statement(/**@lang PostgreSQL */ "CREATE TABLE $newTableName AS SELECT * FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

// for MySQL 5.7

DB::statement("Create TABLE IF Not exists $newTableName like $table");

$columns = collect(DB::select("SHOW COLUMNS FROM $table"))
->reject(function ($column) {
return str_contains($column->Extra, 'VIRTUAL GENERATED') || str_contains($column->Extra, 'STORED GENERATED');
})->pluck('Field')
->implode(', ');

DB::statement(/**@lang MySQL */ "INSERT INTO $newTableName ($columns) SELECT $columns FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

protected function backupTablesForForMariaDb($newTableName, $table): array
{
DB::statement(/**@lang MariaDB*/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

protected function backupTablesForForPostgres($newTableName, $table): array
{
DB::statement(/**@lang PostgreSQL*/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

protected function backupTablesForForSqlServer($newTableName, $table): array
{
DB::statement(/**@lang TSQL*/ "SELECT * INTO $newTableName FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}

public function convertModelToTableName($table): string
{
$modelParent = "Illuminate\Database\Eloquent\Model";
Expand Down Expand Up @@ -187,8 +127,24 @@ protected function buildBackupFilename(string $table, string $currentDateTime)
return str_replace(['-', ':'], '_', $newTableName);
}

protected function getMysqlVersion(): float
/**
* @throws Exception
*/
protected function getBackupStrategy(string $databaseDriver)
{
return (float) DB::select('select version()')[0]->{'version()'};
switch ($databaseDriver) {
case 'sqlite':
return new SqliteBackupStrategy();
case 'mysql':
return new MysqlBackupStrategy();
case 'mariadb':
return new MariaDbBackupStrategy();
case 'pgsql':
return new PostgresBackupStrategy();
case 'sqlsrv':
return new SqlServerBackupStrategy();
default:
throw new Exception('NOT SUPPORTED DATABASE DRIVER');
}
}
}
8 changes: 8 additions & 0 deletions src/BackupTablesStrategy/BackupTablesStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;

abstract class BackupTablesStrategy
{
abstract public function backup($newTableName, $table): array;
}
20 changes: 20 additions & 0 deletions src/BackupTablesStrategy/MariaDbBackupStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;


use Illuminate\Support\Facades\DB;
use WatheqAlshowaiter\BackupTables\Constants;

class MariaDbBackupStrategy extends BackupTablesStrategy
{
public function backup($newTableName, $table): array
{
DB::statement(/**@lang MariaDB */ "CREATE TABLE $newTableName AS SELECT * FROM $table");

return [
'response' => "Table '$table' completed backup successfully.",
'newCreatedTables' => "Newly created table: $newTableName",
];
}
}
20 changes: 20 additions & 0 deletions src/BackupTablesStrategy/MysqlBackupStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;


use Illuminate\Support\Facades\DB;
use WatheqAlshowaiter\BackupTables\Constants;

class MysqlBackupStrategy extends BackupTablesStrategy
{
public function backup($newTableName, $table): array
{
DB::statement(/**@lang MariaDB*/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

return [
'response' => "Table '$table' completed backup successfully.",
'newCreatedTables' => "Newly created table: $newTableName",
];
}
}
20 changes: 20 additions & 0 deletions src/BackupTablesStrategy/PostgresBackupStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;


use Illuminate\Support\Facades\DB;

class PostgresBackupStrategy extends BackupTablesStrategy
{
public function backup($newTableName, $table): array
{
DB::statement(/**@lang PostgreSQL */ "CREATE TABLE $newTableName AS SELECT * FROM $table");


return [
'response' => "Table '$table' completed backup successfully.",
'newCreatedTables' => "Newly created table: $newTableName",
];
}
}
19 changes: 19 additions & 0 deletions src/BackupTablesStrategy/SqlServerBackupStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;


use Illuminate\Support\Facades\DB;

class SqlServerBackupStrategy extends BackupTablesStrategy
{
public function backup($newTableName, $table): array
{
DB::statement(/**@lang TSQL*/ "SELECT * INTO $newTableName FROM $table");

return [
'response' => "Table '$table' completed backup successfully.",
'newCreatedTables' => "Newly created table: $newTableName",
];
}
}
21 changes: 21 additions & 0 deletions src/BackupTablesStrategy/SqliteBackupStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace WatheqAlshowaiter\BackupTables\BackupTablesStrategy;


use Illuminate\Support\Facades\DB;

class SqliteBackupStrategy extends BackupTablesStrategy
{

public function backup($newTableName, $table): array
{
DB::statement(/**@lang SQLite */ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
DB::statement(/**@lang SQLite */ "INSERT INTO $newTableName SELECT * FROM $table");

return [
'response' => "Table '$table' completed backup successfully.",
'newCreatedTables' => "Newly created table: $newTableName",
];
}
}

0 comments on commit 29f5e3a

Please sign in to comment.