Skip to content

Commit

Permalink
Merge branch 'main' of github.com:WatheqAlshowaiter/backup-tables
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Aug 19, 2024
2 parents 13d3e3d + c905860 commit f86ed0d
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Schema;
use WatheqAlshowaiter\BackupTablesServiceProvider\Constants;

class CreateFathersTable extends Migration
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
class CreateMothersTable extends Migration
{

public function up(): void
{
Schema::create('mothers', function (Blueprint $table) {
Expand Down
3 changes: 0 additions & 3 deletions database/migrations/2024_07_13_100247_create_sons_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use WatheqAlshowaiter\BackupTablesServiceProvider\Constants;

class CreateSonsTable extends Migration
{
Expand Down
2 changes: 0 additions & 2 deletions src/BackupTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Illuminate\Support\Facades\Facade;



/**
* public function backupTables($tablesToBackup = []): bool
* protected function processBackup(array $tablesToBackup = []): array
Expand Down
19 changes: 8 additions & 11 deletions src/BackupTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Symfony\Component\Console\Output\ConsoleOutput;

use function Exception;

class BackupTablesService
{

public array $response = [];

public function backupTables($tablesToBackup)
Expand All @@ -19,19 +19,19 @@ public function backupTables($tablesToBackup)

if (empty($tablesToBackup)) {
$this->response[] = 'No tables specified to clone.';

return false;
}


$result = $this->processBackup($tablesToBackup);

$output = new ConsoleOutput();
$output = new ConsoleOutput;

foreach ($result['response'] as $message) {
$output->writeln($message);
}

if (!empty($result['newCreatedTables'])) {
if (! empty($result['newCreatedTables'])) {
$output->writeln('All tables cloned successfully ..');
$output->writeln('Newly created tables:');
foreach ($result['newCreatedTables'] as $tableName) {
Expand All @@ -52,7 +52,7 @@ protected function processBackup(array $tablesToBackup = [])
$currentDateTime = now()->format('Y_m_d_H_i_s');

foreach ($tablesToBackup as $table) {
$newTableName = $table . '_backup_' . $currentDateTime;
$newTableName = $table.'_backup_'.$currentDateTime;
$newTableName = str_replace(['-', ':'], '_', $newTableName);

if (Schema::hasTable($newTableName)) {
Expand All @@ -61,12 +61,12 @@ protected function processBackup(array $tablesToBackup = [])
continue;
}

if (!Schema::hasTable($table)) {
if (! Schema::hasTable($table)) {
$this->response[] = "Table `$table` is not exists. check the table name again";

continue;
}


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

switch ($databaseDriver) {
Expand Down Expand Up @@ -98,7 +98,6 @@ function restoreTable($tableName, $backupName)
// todo
}


}

protected function backupTablesForSqlite($newTableName, $table)
Expand All @@ -110,11 +109,9 @@ protected function backupTablesForSqlite($newTableName, $table)

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


$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";


return [
'response' => $response,
'newCreatedTables' => $newCreatedTables,
Expand All @@ -137,7 +134,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr

// Step 3: Escape reserved keywords and construct the column list
$escapedColumns = array_map(function ($column) {
return '`' . $column . '`'; // Escape column names with backticks
return '`'.$column.'`'; // Escape column names with backticks
}, $nonGeneratedColumns);

// Convert array to comma-separated string
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Constants
{
public const VERSION_AFTER_ULID_SUPPORT = 9.40;

public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0;
public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0;

public const VERSION_AFTER_UUID_SUPPORT = 7.0;

Expand Down
2 changes: 0 additions & 2 deletions src/Models/Father.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace WatheqAlshowaiter\BackupTablesServiceProvider\Models;

use Illuminate\Database\Eloquent\Model;


class Father extends Model
{
protected $guarded = ['id'];
Expand Down
5 changes: 1 addition & 4 deletions src/Models/Mother.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

use Illuminate\Database\Eloquent\Model;


/**
* @deprecated
*/
class Mother extends Model
{
}
class Mother extends Model {}
1 change: 1 addition & 0 deletions src/Models/Son.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
class Son extends Model
{
protected $guarded = ['id'];

public $timestamps = false;
}
16 changes: 7 additions & 9 deletions tests/BackupTablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ public function test_return_when_table_string_empty()
$this->assertFalse(BackupTables::backupTables($emptyArray));
}



public function test_generate_single_table_backup_with_proper_name()
{
Carbon::setTestNow();

$tableName = 'fathers';
BackupTables::backupTables($tableName);

$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($newTableName));
}
Expand All @@ -53,12 +51,12 @@ public function test_generate_single_table_backup_all_table_data()
'id' => 1,
'first_name' => 'Ahmed',
'last_name' => 'Saleh',
'email' => 'father@email.com'
'email' => 'father@email.com',
]);

BackupTables::backupTables($tableName);

$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($newTableName));

Expand All @@ -77,17 +75,17 @@ public function test_generate_multiple_table_backup()
'id' => 1,
'first_name' => 'Ahmed',
'last_name' => 'Saleh',
'email' => 'father@email.com'
'email' => 'father@email.com',
]);

Son::create([
'father_id' => 1,
]);

BackupTables::backupTables([ $tableName2, $tableName]);
BackupTables::backupTables([$tableName2, $tableName]);

$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
$newTableName2 = $tableName2 . '_backup_' . now()->format('Y_m_d_H_i_s');
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
$newTableName2 = $tableName2.'_backup_'.now()->format('Y_m_d_H_i_s');

$this->assertTrue(Schema::hasTable($newTableName));
$this->assertTrue(Schema::hasTable($newTableName2));
Expand Down

0 comments on commit f86ed0d

Please sign in to comment.