Migration.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace ModulesGarden\Servers\ZimbraEmail\App\Cron;
  3. use ModulesGarden\Servers\ZimbraEmail\App\Models\ProductConfiguration;
  4. use Symfony\Component\Console\Input\InputArgument;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Input\InputOption;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. use \Illuminate\Database\Capsule\Manager as DB;
  10. use ModulesGarden\Servers\ZimbraEmail\App\Libs\Migrations\Drivers\Version1To2;
  11. /**
  12. *
  13. * Created by PhpStorm.
  14. * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
  15. * Date: 13.11.19
  16. * Time: 09:49
  17. * Class Migration
  18. */
  19. class Migration extends \ModulesGarden\Servers\ZimbraEmail\Core\CommandLine\AbstractCommand
  20. {
  21. /**
  22. * Command name
  23. * @var string
  24. */
  25. protected $name = 'migration:v1_v2';
  26. /**
  27. * Command description
  28. * @var string
  29. */
  30. protected $description = 'Migrate product settings from version 1.x to version 2.x';
  31. /**
  32. * @param InputInterface $input
  33. * @param OutputInterface $output
  34. * @param SymfonyStyle $io
  35. */
  36. protected function process(InputInterface $input, OutputInterface $output, SymfonyStyle $io)
  37. {
  38. $migration = new Version1To2\Settings();
  39. $exists = DB::schema()->hasTable($migration->getFromTable());
  40. if(!$exists)
  41. {
  42. $io->warning("Previous version of module can not be found.");
  43. }
  44. $previous = DB::table($migration->getFromTable())->get();
  45. /**
  46. *
  47. */
  48. $prodManager = new ProductConfiguration();
  49. /**
  50. *
  51. * storage all params per product id
  52. */
  53. foreach($previous as $setting)
  54. {
  55. $settings[$setting->product_id][$setting->setting] = $setting->value;
  56. $products[] = $setting->product_id;
  57. }
  58. /**
  59. *
  60. * update setting data & save
  61. */
  62. foreach($settings as $prodId => $settingsArray)
  63. {
  64. $attrs = $migration->updateValues($settingsArray, $prodId);
  65. foreach($attrs as $key => $value)
  66. {
  67. $prodManager->updateOrCreate(['product_id' => $prodId, 'setting' => $key],['value' => $value]);
  68. }
  69. }
  70. /**
  71. *
  72. * set new fields
  73. */
  74. foreach($migration->getNewFields() as $key => $value)
  75. {
  76. foreach($products as $id)
  77. {
  78. $prodManager->updateOrCreate(['product_id' => $id, 'setting' => $key],['value' => $value]);
  79. }
  80. }
  81. /**
  82. *
  83. */
  84. $io->success("Migration has been finished successfully.");
  85. }
  86. }