Skip to content

Commit 31023de

Browse files
committed
refactor: extract base class for email/phone format migration steps
- Create MigrateStringToJsonFormatStep abstract base class - Reduce MigrateEmailFormatStep and MigratePhoneFormatStep from 89 lines to 21 lines each - Clean up unused variable in UpgradeCommand
1 parent 6317aa9 commit 31023de

File tree

4 files changed

+108
-157
lines changed

4 files changed

+108
-157
lines changed

src/Console/Commands/Upgrade/Steps/MigrateEmailFormatStep.php

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,18 @@
44

55
namespace Relaticle\CustomFields\Console\Commands\Upgrade\Steps;
66

7-
use Illuminate\Console\Command;
8-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
9-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
10-
use Relaticle\CustomFields\CustomFields;
11-
use Throwable;
12-
137
/**
148
* Migrates email field values from single string format (v2) to array format (v3).
159
*/
16-
final class MigrateEmailFormatStep implements UpgradeStep
10+
final class MigrateEmailFormatStep extends MigrateStringToJsonFormatStep
1711
{
18-
public function name(): string
19-
{
20-
return 'Migrate Email Format';
21-
}
22-
23-
public function description(): string
12+
protected function fieldType(): string
2413
{
25-
return 'Convert email field values from single string to array format';
14+
return 'email';
2615
}
2716

28-
public function execute(bool $dryRun, Command $command): UpgradeStepResult
17+
protected function fieldTypeLabel(): string
2918
{
30-
$customFieldModel = CustomFields::newCustomFieldModel();
31-
$customFieldValueModel = CustomFields::newValueModel();
32-
33-
$emailFields = $customFieldModel::query()
34-
->withoutGlobalScopes()
35-
->where('type', 'email')
36-
->get();
37-
38-
if ($emailFields->isEmpty()) {
39-
return UpgradeStepResult::skipped('No email fields found');
40-
}
41-
42-
$valuesToMigrate = $customFieldValueModel::query()
43-
->withoutGlobalScopes()
44-
->whereIn('custom_field_id', $emailFields->pluck('id'))
45-
->whereNotNull('string_value')
46-
->where('string_value', '!=', '')
47-
->whereNull('json_value')
48-
->get();
49-
50-
if ($valuesToMigrate->isEmpty()) {
51-
return UpgradeStepResult::skipped('No legacy email values found');
52-
}
53-
54-
$command->line(sprintf(' Found %d email value(s) to migrate', $valuesToMigrate->count()));
55-
56-
if ($dryRun) {
57-
return UpgradeStepResult::success($valuesToMigrate->count());
58-
}
59-
60-
$migrated = 0;
61-
$failed = 0;
62-
$errors = [];
63-
64-
foreach ($valuesToMigrate as $value) {
65-
try {
66-
$originalValue = $value->string_value;
67-
68-
$value->json_value = [$originalValue];
69-
$value->string_value = null;
70-
$value->save();
71-
72-
$migrated++;
73-
} catch (Throwable $e) {
74-
$command->line(sprintf(' <error>✗</error> Value ID %s: %s', $value->id, $e->getMessage()));
75-
$errors[] = sprintf('Value ID %s: %s', $value->id, $e->getMessage());
76-
$failed++;
77-
}
78-
}
79-
80-
$command->line(sprintf(' <info>✓</info> Migrated %d email value(s)', $migrated));
81-
82-
return new UpgradeStepResult(
83-
success: $failed === 0,
84-
itemsProcessed: $migrated,
85-
itemsFailed: $failed,
86-
errors: $errors,
87-
);
19+
return 'Email';
8820
}
8921
}

src/Console/Commands/Upgrade/Steps/MigratePhoneFormatStep.php

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,86 +4,18 @@
44

55
namespace Relaticle\CustomFields\Console\Commands\Upgrade\Steps;
66

7-
use Illuminate\Console\Command;
8-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
9-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
10-
use Relaticle\CustomFields\CustomFields;
11-
use Throwable;
12-
137
/**
148
* Migrates phone field values from single string format (v2) to array format (v3).
159
*/
16-
final class MigratePhoneFormatStep implements UpgradeStep
10+
final class MigratePhoneFormatStep extends MigrateStringToJsonFormatStep
1711
{
18-
public function name(): string
19-
{
20-
return 'Migrate Phone Format';
21-
}
22-
23-
public function description(): string
12+
protected function fieldType(): string
2413
{
25-
return 'Convert phone field values from single string to array format';
14+
return 'phone';
2615
}
2716

28-
public function execute(bool $dryRun, Command $command): UpgradeStepResult
17+
protected function fieldTypeLabel(): string
2918
{
30-
$customFieldModel = CustomFields::newCustomFieldModel();
31-
$customFieldValueModel = CustomFields::newValueModel();
32-
33-
$phoneFields = $customFieldModel::query()
34-
->withoutGlobalScopes()
35-
->where('type', 'phone')
36-
->get();
37-
38-
if ($phoneFields->isEmpty()) {
39-
return UpgradeStepResult::skipped('No phone fields found');
40-
}
41-
42-
$valuesToMigrate = $customFieldValueModel::query()
43-
->withoutGlobalScopes()
44-
->whereIn('custom_field_id', $phoneFields->pluck('id'))
45-
->whereNotNull('string_value')
46-
->where('string_value', '!=', '')
47-
->whereNull('json_value')
48-
->get();
49-
50-
if ($valuesToMigrate->isEmpty()) {
51-
return UpgradeStepResult::skipped('No legacy phone values found');
52-
}
53-
54-
$command->line(sprintf(' Found %d phone value(s) to migrate', $valuesToMigrate->count()));
55-
56-
if ($dryRun) {
57-
return UpgradeStepResult::success($valuesToMigrate->count());
58-
}
59-
60-
$migrated = 0;
61-
$failed = 0;
62-
$errors = [];
63-
64-
foreach ($valuesToMigrate as $value) {
65-
try {
66-
$originalValue = $value->string_value;
67-
68-
$value->json_value = [$originalValue];
69-
$value->string_value = null;
70-
$value->save();
71-
72-
$migrated++;
73-
} catch (Throwable $e) {
74-
$command->line(sprintf(' <error>✗</error> Value ID %s: %s', $value->id, $e->getMessage()));
75-
$errors[] = sprintf('Value ID %s: %s', $value->id, $e->getMessage());
76-
$failed++;
77-
}
78-
}
79-
80-
$command->line(sprintf(' <info>✓</info> Migrated %d phone value(s)', $migrated));
81-
82-
return new UpgradeStepResult(
83-
success: $failed === 0,
84-
itemsProcessed: $migrated,
85-
itemsFailed: $failed,
86-
errors: $errors,
87-
);
19+
return 'Phone';
8820
}
8921
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Relaticle\CustomFields\Console\Commands\Upgrade\Steps;
6+
7+
use Illuminate\Console\Command;
8+
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
9+
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
10+
use Relaticle\CustomFields\CustomFields;
11+
use Throwable;
12+
13+
/**
14+
* Base class for migrating field values from single string format (v2) to array format (v3).
15+
*/
16+
abstract class MigrateStringToJsonFormatStep implements UpgradeStep
17+
{
18+
abstract protected function fieldType(): string;
19+
20+
abstract protected function fieldTypeLabel(): string;
21+
22+
public function name(): string
23+
{
24+
return "Migrate {$this->fieldTypeLabel()} Format";
25+
}
26+
27+
public function description(): string
28+
{
29+
return "Convert {$this->fieldType()} field values from single string to array format";
30+
}
31+
32+
public function execute(bool $dryRun, Command $command): UpgradeStepResult
33+
{
34+
$customFieldModel = CustomFields::newCustomFieldModel();
35+
$customFieldValueModel = CustomFields::newValueModel();
36+
$fieldType = $this->fieldType();
37+
38+
$fields = $customFieldModel::query()
39+
->withoutGlobalScopes()
40+
->where('type', $fieldType)
41+
->get();
42+
43+
if ($fields->isEmpty()) {
44+
return UpgradeStepResult::skipped("No {$fieldType} fields found");
45+
}
46+
47+
$valuesToMigrate = $customFieldValueModel::query()
48+
->withoutGlobalScopes()
49+
->whereIn('custom_field_id', $fields->pluck('id'))
50+
->whereNotNull('string_value')
51+
->where('string_value', '!=', '')
52+
->whereNull('json_value')
53+
->get();
54+
55+
if ($valuesToMigrate->isEmpty()) {
56+
return UpgradeStepResult::skipped("No legacy {$fieldType} values found");
57+
}
58+
59+
$command->line(sprintf(' Found %d %s value(s) to migrate', $valuesToMigrate->count(), $fieldType));
60+
61+
if ($dryRun) {
62+
return UpgradeStepResult::success($valuesToMigrate->count());
63+
}
64+
65+
$migrated = 0;
66+
$failed = 0;
67+
$errors = [];
68+
69+
foreach ($valuesToMigrate as $value) {
70+
try {
71+
$originalValue = $value->string_value;
72+
73+
$value->json_value = [$originalValue];
74+
$value->string_value = null;
75+
$value->save();
76+
77+
$migrated++;
78+
} catch (Throwable $e) {
79+
$command->line(sprintf(' <error>✗</error> Value ID %s: %s', $value->id, $e->getMessage()));
80+
$errors[] = sprintf('Value ID %s: %s', $value->id, $e->getMessage());
81+
$failed++;
82+
}
83+
}
84+
85+
$command->line(sprintf(' <info>✓</info> Migrated %d %s value(s)', $migrated, $fieldType));
86+
87+
return new UpgradeStepResult(
88+
success: $failed === 0,
89+
itemsProcessed: $migrated,
90+
itemsFailed: $failed,
91+
errors: $errors,
92+
);
93+
}
94+
}

src/Console/Commands/UpgradeCommand.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
namespace Relaticle\CustomFields\Console\Commands;
66

77
use Illuminate\Console\Command;
8-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
9-
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
108
use Relaticle\CustomFields\Console\Commands\Upgrade\Steps\ClearCachesStep;
119
use Relaticle\CustomFields\Console\Commands\Upgrade\Steps\MigrateEmailFormatStep;
1210
use Relaticle\CustomFields\Console\Commands\Upgrade\Steps\MigrateLookupFieldsStep;
1311
use Relaticle\CustomFields\Console\Commands\Upgrade\Steps\MigratePhoneFormatStep;
1412
use Relaticle\CustomFields\Console\Commands\Upgrade\Steps\ValidateSchemaStep;
13+
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStep;
14+
use Relaticle\CustomFields\Console\Commands\Upgrade\UpgradeStepResult;
1515

1616
/**
1717
* Main upgrade command for custom-fields 2.x → 3.x migration.
@@ -91,8 +91,7 @@ private function runSteps(bool $isDryRun, array $stepsToSkip): array
9191
{
9292
$results = [];
9393
$stepNumber = 1;
94-
$activeSteps = array_diff_key(self::STEPS, array_flip($stepsToSkip));
95-
$totalSteps = count($activeSteps);
94+
$totalSteps = count(self::STEPS) - count($stepsToSkip);
9695

9796
foreach (self::STEPS as $key => $stepClass) {
9897
if (in_array($key, $stepsToSkip, true)) {
@@ -196,12 +195,6 @@ private function displaySummary(array $results, bool $isDryRun): void
196195
*/
197196
private function hasErrors(array $results): bool
198197
{
199-
foreach ($results as $result) {
200-
if (! $result->success) {
201-
return true;
202-
}
203-
}
204-
205-
return false;
198+
return collect($results)->contains(fn (UpgradeStepResult $result): bool => ! $result->success);
206199
}
207200
}

0 commit comments

Comments
 (0)