Minor fixes for batch migrations

This commit is contained in:
David Bomba 2020-11-30 18:43:33 +11:00
parent 792262dee4
commit a8958f4b09
4 changed files with 47 additions and 6 deletions

View File

@ -12,7 +12,15 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
use App\Exceptions\MigrationValidatorFailed;
use App\Exceptions\NonExistingMigrationFile;
use App\Exceptions\ProcessingMigrationArchiveFailed;
use App\Exceptions\ResourceDependencyMissing;
use App\Exceptions\ResourceNotAvailableForMigration;
use App\Jobs\Util\Import;
use App\Jobs\Util\StartMigration; use App\Jobs\Util\StartMigration;
use App\Libraries\MultiDB;
use App\Mail\MigrationFailed;
use App\Models\Account; use App\Models\Account;
use App\Models\Company; use App\Models\Company;
use App\Models\CompanyToken; use App\Models\CompanyToken;
@ -24,6 +32,7 @@ use Faker\Factory;
use Faker\Generator; use Faker\Generator;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use ZipArchive;
class ImportMigrations extends Command class ImportMigrations extends Command
{ {
@ -69,17 +78,45 @@ class ImportMigrations extends Command
public function handle() public function handle()
{ {
$this->buildCache(); $this->buildCache();
$path = $this->option('path') ?? storage_path('migrations/import'); $path = $this->option('path') ?? public_path('storage/migrations/import');
$directory = new DirectoryIterator($path); $directory = new DirectoryIterator($path);
foreach ($directory as $file) { foreach ($directory as $file) {
if ($file->getExtension() === 'zip') { if ($file->getExtension() === 'zip') {
$user = $this->getUser();
$company = $this->getUser()->companies()->first();
$this->info('Started processing: '.$file->getBasename().' at '.now()); $this->info('Started processing: '.$file->getBasename().' at '.now());
StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first());
} $zip = new ZipArchive();
} $archive = $zip->open($file->getRealPath());
try {
if (! $archive) {
throw new ProcessingMigrationArchiveFailed('Processing migration archive failed. Migration file is possibly corrupted.');
}
$filename = pathinfo($file->getRealPath(), PATHINFO_FILENAME);
$zip->extractTo(public_path("storage/migrations/{$filename}"));
$zip->close();
$import_file = public_path("storage/migrations/$filename/migration.json");
Import::dispatch($import_file, $this->getUser()->companies()->first(), $this->getUser());
// StartMigration::dispatch($file->getRealPath(), $this->getUser(), $this->getUser()->companies()->first());
}
catch (NonExistingMigrationFile | ProcessingMigrationArchiveFailed | ResourceNotAvailableForMigration | MigrationValidatorFailed | ResourceDependencyMissing $e) {
\Mail::to($this->user)->send(new MigrationFailed($e, $e->getMessage()));
if (app()->environment() !== 'production') {
info($e->getMessage());
}
}}}
} }
public function getUser(): User public function getUser(): User

View File

@ -55,7 +55,7 @@ class CompanySettings extends BaseSettings
public $default_task_rate = 0; // @TODO Where do we inject this? public $default_task_rate = 0; // @TODO Where do we inject this?
public $payment_terms = ''; //@implemented public $payment_terms = ''; //@implemented
public $send_reminders = false; //@TODO public $send_reminders = true; //@TODO
public $custom_message_dashboard = ''; // @TODO There currently is no dashboard so this is pending public $custom_message_dashboard = ''; // @TODO There currently is no dashboard so this is pending
public $custom_message_unpaid_invoice = ''; public $custom_message_unpaid_invoice = '';

View File

@ -177,6 +177,9 @@ class Import implements ShouldQueue
{ {
set_time_limit(0); set_time_limit(0);
auth()->login($this->user, false);
auth()->user()->setCompany($this->company);
// $jsonStream = \JsonMachine\JsonMachine::fromFile($this->file_path, "/data"); // $jsonStream = \JsonMachine\JsonMachine::fromFile($this->file_path, "/data");
$array = json_decode(file_get_contents($this->file_path), 1); $array = json_decode(file_get_contents($this->file_path), 1);
$data = $array['data']; $data = $array['data'];

View File

@ -39,6 +39,7 @@ class ActivityTransformer extends EntityTransformer
'company_id' => $activity->company_id ? (string) $this->encodePrimaryKey($activity->company_id) : '', 'company_id' => $activity->company_id ? (string) $this->encodePrimaryKey($activity->company_id) : '',
'user_id' => (string) $this->encodePrimaryKey($activity->user_id), 'user_id' => (string) $this->encodePrimaryKey($activity->user_id),
'invoice_id' => $activity->invoice_id ? (string) $this->encodePrimaryKey($activity->invoice_id) : '', 'invoice_id' => $activity->invoice_id ? (string) $this->encodePrimaryKey($activity->invoice_id) : '',
'quote_id' => $activity->quote_id ? (string) $this->encodePrimaryKey($activity->quote_id) : '',
'payment_id' => $activity->payment_id ? (string) $this->encodePrimaryKey($activity->payment_id) : '', 'payment_id' => $activity->payment_id ? (string) $this->encodePrimaryKey($activity->payment_id) : '',
'credit_id' => $activity->credit_id ? (string) $this->encodePrimaryKey($activity->credit_id) : '', 'credit_id' => $activity->credit_id ? (string) $this->encodePrimaryKey($activity->credit_id) : '',
'updated_at' => (int) $activity->updated_at, 'updated_at' => (int) $activity->updated_at,