Skip to content

Commit

Permalink
Refactor SeedSpotter.php: Improve data handling and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulmannans committed Sep 19, 2024
1 parent f92b81c commit 481e150
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/SeedSpotter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,35 @@ public function compare()

protected function getSeederData()
{
$originalData = DB::table($this->table)->get()->toArray();

DB::beginTransaction();
// Create a temporary table to store the original data
$tempTable = $this->table . '_temp_' . time();
DB::statement("CREATE TABLE {$tempTable} LIKE {$this->table}");
DB::statement("INSERT INTO {$tempTable} SELECT * FROM {$this->table}");

try {
DB::table($this->table)->delete();
// Run the seeder
$this->seeder->run();
$seederData = DB::table($this->table)->get()->toArray();
DB::rollBack();

// Get the seeded data
$seededData = DB::table($this->table)->get();

// Restore the original data
DB::statement("TRUNCATE TABLE {$this->table}");
DB::statement("INSERT INTO {$this->table} SELECT * FROM {$tempTable}");

// Drop the temporary table
DB::statement("DROP TABLE {$tempTable}");

return $seededData->toArray();
} catch (\Exception $e) {
DB::rollBack();
// If an error occurs, make sure we restore the original data and drop the temp table
if (DB::getSchemaBuilder()->hasTable($tempTable)) {
DB::statement("TRUNCATE TABLE {$this->table}");
DB::statement("INSERT INTO {$this->table} SELECT * FROM {$tempTable}");
DB::statement("DROP TABLE {$tempTable}");
}
throw $e;
}

return $seederData;
}

protected function getDatabaseData()
Expand Down Expand Up @@ -92,8 +106,9 @@ protected function findMatchingRow($row, $dataSet)

protected function rowsDiffer($row1, $row2)
{

foreach ($row1 as $key => $value) {
if ($row2->$key !== $value) {
if ($row2->$key !== $value && $key !== 'updated_at' && $key !== 'created_at') {
return true;
}
}
Expand Down

0 comments on commit 481e150

Please sign in to comment.