Skip to content

Commit

Permalink
fix: father_id make it dynamically in test_generate_2_single_table_ba…
Browse files Browse the repository at this point in the history
…ckup_all_table_data
  • Loading branch information
WatheqAlshowaiter committed Aug 23, 2024
1 parent fd80ebe commit ecf356e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
22 changes: 8 additions & 14 deletions database/migrations/2024_07_13_100247_create_sons_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,16 @@
use WatheqAlshowaiter\BackupTables\Constants;

class CreateSonsTable extends Migration
{
public function up(): void
{
Schema::create('sons', function (Blueprint $table) {
$table->bigIncrements('id');
public function up(): void
{
Schema::create('sons', function (Blueprint $table) {
$table->bigIncrements('id');

$table->unsignedBigInteger('father_id');
$table->foreign('father_id')->references('id')->on('fathers');
//
//if((float) App::version() >= Constants::VERSION_AFTER_FOREIGN_ID_SUPPORT){
// $table->foreignId('father_id');
//}else {
// $table->unsignedBigInteger('father_id');
//}
});
}
$table->unsignedBigInteger('father_id');
$table->foreign('father_id')->references('id')->on('fathers');
});
}

public function down(): void
{
Expand Down
18 changes: 9 additions & 9 deletions tests/BackupTablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,31 @@ public function test_generate_2_single_table_backup_all_table_data()
$fatherTable = 'fathers';
$sonTable = 'sons';

Father::create([
$father = Father::create([
'id' => 1,
'first_name' => 'Ahmed',
'last_name' => 'Saleh',
'email' => '[email protected]',
]);

Son::create([
'father_id' => 1,
'father_id' => $father->id,
]);

BackupTables::generateBackup($fatherTable);

$currentDateTime = now()->format('Y_m_d_H_i_s');
$newTableName = $fatherTable . '_backup_' . $currentDateTime;
$newTableName2 = $sonTable . '_backup_' . $currentDateTime;
$newFatherTable = $fatherTable . '_backup_' . $currentDateTime;
$newSonTable = $sonTable . '_backup_' . $currentDateTime;

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

$this->assertEquals(DB::table('fathers')->value('first_name'), DB::table($newTableName)->value('first_name'));
$this->assertEquals(DB::table('fathers')->value('email'), DB::table($newTableName)->value('email'));
$this->assertEquals(DB::table('fathers')->value('first_name'), DB::table($newFatherTable)->value('first_name'));
$this->assertEquals(DB::table('fathers')->value('email'), DB::table($newFatherTable)->value('email'));

BackupTables::generateBackup($sonTable);
$this->assertTrue(Schema::hasTable($newTableName2));
$this->assertEquals(DB::table('sons')->value('father_id'), DB::table($newTableName2)->value('father_id'));
$this->assertTrue(Schema::hasTable($newSonTable));
$this->assertEquals(DB::table('sons')->value('father_id'), DB::table($newSonTable)->value('father_id'));
}

//public function test_generate_multiple_table_backup()
Expand Down

0 comments on commit ecf356e

Please sign in to comment.