1
0
Эх сурвалжийг харах

Update TransformImports command

Daniel Supernault 2 долоо хоног өмнө
parent
commit
c692c7655e

+ 74 - 9
app/Console/Commands/TransformImports.php

@@ -10,6 +10,8 @@ use App\Services\ImportService;
 use App\Services\MediaPathService;
 use App\Status;
 use Illuminate\Console\Command;
+use Illuminate\Database\QueryException;
+use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Str;
 use Storage;
 
@@ -78,6 +80,52 @@ class TransformImports extends Command
                 continue;
             }
 
+            $originalIncr = $idk['incr'];
+            $attempts = 0;
+            while ($attempts < 999) {
+                $duplicateCheck = ImportPost::where('user_id', $id)
+                    ->where('creation_year', $ip->creation_year)
+                    ->where('creation_month', $ip->creation_month)
+                    ->where('creation_day', $ip->creation_day)
+                    ->where('creation_id', $idk['incr'])
+                    ->where('id', '!=', $ip->id)
+                    ->exists();
+
+                if (! $duplicateCheck) {
+                    break;
+                }
+
+                $idk['incr']++;
+                $attempts++;
+
+                if ($idk['incr'] > 999) {
+                    $this->warn("Could not find unique creation_id for ImportPost ID {$ip->id} on {$ip->creation_year}-{$ip->creation_month}-{$ip->creation_day}");
+                    $ip->skip_missing_media = true;
+                    $ip->save();
+
+                    continue 2;
+                }
+            }
+
+            if ($attempts >= 999) {
+                $this->warn("Exhausted attempts finding unique creation_id for ImportPost ID {$ip->id}");
+                $ip->skip_missing_media = true;
+                $ip->save();
+
+                continue;
+            }
+
+            if ($idk['incr'] !== $originalIncr) {
+                $uid = str_pad($id, 6, 0, STR_PAD_LEFT);
+                $yearStr = str_pad($ip->creation_year, 2, 0, STR_PAD_LEFT);
+                $monthStr = str_pad($ip->creation_month, 2, 0, STR_PAD_LEFT);
+                $dayStr = str_pad($ip->creation_day, 2, 0, STR_PAD_LEFT);
+                $zone = $yearStr.$monthStr.$dayStr.str_pad($idk['incr'], 3, 0, STR_PAD_LEFT);
+                $idk['id'] = '1'.$uid.$zone;
+
+                $this->info("Adjusted creation_id from {$originalIncr} to {$idk['incr']} for ImportPost ID {$ip->id}");
+            }
+
             if (Storage::exists('imports/'.$id.'/'.$ip->filename) === false) {
                 ImportService::clearAttempts($profile->id);
                 ImportService::getPostCount($profile->id, true);
@@ -103,7 +151,7 @@ class TransformImports extends Command
                 continue;
             }
 
-            $caption = $ip->caption ?? "";
+            $caption = $ip->caption ?? '';
             $status = new Status;
             $status->profile_id = $pid;
             $status->caption = $caption;
@@ -141,17 +189,34 @@ class TransformImports extends Command
                 $media->save();
             }
 
-            $ip->status_id = $status->id;
-            $ip->creation_id = $idk['incr'];
-            $ip->save();
+            try {
+                DB::transaction(function () use ($ip, $status, $profile, $idk) {
+                    $ip->status_id = $status->id;
+                    $ip->creation_id = $idk['incr'];
+                    $ip->save();
+
+                    $profile->status_count = $profile->status_count + 1;
+                    $profile->save();
+                });
+
+                AccountService::del($profile->id);
+                ImportService::clearAttempts($profile->id);
+                ImportService::getPostCount($profile->id, true);
 
-            $profile->status_count = $profile->status_count + 1;
-            $profile->save();
+            } catch (QueryException $e) {
+                if ($e->getCode() === '23000') {
+                    $this->warn("Constraint violation for ImportPost ID {$ip->id}: ".$e->getMessage());
+                    $ip->skip_missing_media = true;
+                    $ip->save();
 
-            AccountService::del($profile->id);
+                    Media::where('status_id', $status->id)->delete();
+                    $status->delete();
 
-            ImportService::clearAttempts($profile->id);
-            ImportService::getPostCount($profile->id, true);
+                    continue;
+                } else {
+                    throw $e;
+                }
+            }
         }
     }
 }