Skip to content

Commit e8a62ca

Browse files
committed
Merge branch 'develop'
2 parents cae963e + 48f8c29 commit e8a62ca

1 file changed

Lines changed: 39 additions & 41 deletions

File tree

src/Command/Models/SyncAttachments.php

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
130130

131131
$createCount = 0;
132132
$updateCount = 0;
133+
$attachmentsToUpdate = [];
133134
$fieldCache = [];
134135

135136
foreach ($attachments as $attachment) {
@@ -139,9 +140,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
139140
$createCount++;
140141
} elseif ($do_update) {
141142
// Check for conflicts with existing fields
142-
$this->updateTable($attachment, $output, $fieldCache);
143+
$updateAttachment = $this->updateTable($attachment, $output, $fieldCache);
144+
$attachmentsToUpdate[$attachment::objType()] = $updateAttachment;
143145

144-
$updateCount++;
146+
if ($updateAttachment) {
147+
$updateCount++;
148+
}
145149
}
146150
} catch (\Throwable $th) {
147151
$this->writeError($th->getMessage(), $output);
@@ -171,54 +175,54 @@ protected function execute(InputInterface $input, OutputInterface $output): int
171175
$output->writeln('');
172176
$output->writeln(sprintf('Property <fg=yellow;options=bold>%s</>:', $key));
173177
foreach ($value['type'] as $type => $attachments) {
174-
$output->writeln(sprintf('- These attachments define the type as: <fg=yellow;options=bold>%s</>', $type));
178+
$output->writeln(sprintf('These attachments define the type as: <fg=yellow;options=bold>%s</>', $type));
175179
foreach ($attachments as $attachment) {
176-
$output->writeln(sprintf('-- %s', $attachment));
180+
$output->writeln(sprintf('- %s', $attachment));
177181
}
178182
}
179183
}
184+
185+
return self::$FAILURE;
180186
} else {
181187
// Do actual update here.
182-
if (!$this->isDryRun) {
183-
foreach ($attachments as $attachment) {
184-
$this->timer()->start();
188+
foreach ($attachments as $attachment) {
189+
$this->timer()->start();
190+
191+
$needToUdpate = $attachmentsToUpdate[$attachment::objType()] ?? false;
192+
$class_name = get_class($attachment);
193+
194+
if (empty($needToUdpate)) {
195+
$output->writeln(sprintf(
196+
'Skipping <fg=yellow;options=bold>%s</>: already up-to-date',
197+
$class_name
198+
));
199+
$this->timer()->stop();
200+
continue;
201+
}
202+
203+
$output->writeln(sprintf('<fg=blue>Updating attachments table</> for <fg=yellow;options=bold>%s</>', $class_name));
204+
if ($needToUdpate instanceof Table) {
205+
$needToUdpate->render();
206+
}
207+
208+
if (!$this->isDryRun) {
185209
/** @var DatabaseSource $source */
186210
$source = $attachment->source();
187211
$source->alterTable();
188-
189212
$output->writeln(sprintf(
190-
'<fg=green>Updated %s - %ss</>',
213+
'<fg=green>Updated table: %s for <fg=yellow;options=bold>%s</> - %ss</>',
191214
$source->table(),
215+
get_class($attachment),
192216
$this->timer()->stop()
193217
));
194218
}
195219
}
196-
}
197220

198-
$output->writeln('<info>All Done!</info>');
221+
$output->writeln('<info>All Done!</info>');
199222

200-
$messages = [];
201-
202-
if ($createCount) {
203-
$messages[] = sprintf(
204-
'created %s %s',
205-
$createCount,
206-
$createCount == 1 ? 'table' : 'tables'
207-
);
208-
}
209-
210-
if ($updateCount) {
211-
$messages[] = sprintf(
212-
'updated %s %s',
213-
$updateCount,
214-
$updateCount == 1 ? 'table' : 'tables'
215-
);
216-
}
217-
218-
$message = ucfirst(implode(' and ', $messages));
219-
220-
if (!empty($message)) {
223+
$message = $createCount ? 'Created attachments table' : 'Updated attachments table';
221224
$output->writeln(sprintf('<info>%s</info>', $message));
225+
return self::$SUCCESS;
222226
}
223227

224228
return self::$SUCCESS;
@@ -236,8 +240,7 @@ private function createTable(ModelInterface $attachment, OutputInterface $output
236240

237241
$output->writeln(sprintf(
238242
'<fg=green>Created table: %s - %ss</>',
239-
$source->table(),
240-
$this->timer()->stop()
243+
$source->table()
241244
));
242245
}
243246
}
@@ -247,13 +250,6 @@ private function updateTable(ModelInterface $attachment, OutputInterface $output
247250
$class_name = get_class($attachment);
248251
$changes = $this->getChanges($attachment, $output);
249252

250-
if (!empty($changes['alterations'])) {
251-
$output->writeln(sprintf('<fg=blue>Updating attachments table</> for <fg=yellow;options=bold>%s</>', $class_name));
252-
$changes['table']->render();
253-
} else {
254-
$output->writeln(sprintf('Skipping <fg=yellow;options=bold>%s</>: already up-to-date', $class_name));
255-
}
256-
257253
if (!empty($changes['properties'])) {
258254
// Check for conflicts
259255
foreach ($changes['properties'] as $property => $aspects) {
@@ -266,6 +262,8 @@ private function updateTable(ModelInterface $attachment, OutputInterface $output
266262
}
267263
}
268264
}
265+
266+
return !empty($changes['alterations']) ? $changes['table'] : false;
269267
}
270268

271269
private function getChanges(ModelInterface $attachment, OutputInterface $output): ?array

0 commit comments

Comments
 (0)