M250416105120UpdateAttachment.php 1023 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\migrations;
  3. use yii\db\Migration;
  4. /**
  5. * Class M250416105120UpdateAttachment
  6. */
  7. class M250416105120UpdateAttachment extends Migration
  8. {
  9. /**
  10. * {@inheritdoc}
  11. */
  12. public function safeUp()
  13. {
  14. try {
  15. // 1. 添加联合索引
  16. $sql = <<<SQL
  17. ALTER TABLE `qimall_attachment`
  18. ADD INDEX `idx_mall_delete_recycle_url` (`mall_id`, `is_delete`, `is_recycle`, `url`(255));
  19. SQL;
  20. $this->execute($sql);
  21. // 2. 删除冗余索引
  22. $sql = <<<SQL
  23. DROP INDEX `idx_mall_id` ON `qimall_attachment`;
  24. DROP INDEX `idx_is_delete` ON `qimall_attachment`;
  25. SQL;
  26. $this->execute($sql);
  27. } catch (\Exception $e) {
  28. // 静默处理异常
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function safeDown()
  35. {
  36. echo "M250416105120UpdateAttachment cannot be reverted.\n";
  37. return false;
  38. }
  39. }