M250916155432ModifyAgentLevel.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace addons\Agent\console\migrations;
  3. use Yii;
  4. use yii\db\Migration;
  5. /**
  6. * 修改渠道代理等级表中 关联商品字段的数据类型
  7. * Class M250916155432ModifyAgentLevel
  8. */
  9. class M250916155432ModifyAgentLevel extends Migration
  10. {
  11. /**
  12. * {@inheritdoc}
  13. */
  14. public function safeUp()
  15. {
  16. try {
  17. $res = $this->columnExists('qimall_addons_agent_level', 'goods_list');
  18. if ($res) {
  19. $sql=<<<SQL
  20. ALTER TABLE `qimall_addons_agent_level`
  21. MODIFY COLUMN `goods_list` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '商品列表json' ;
  22. SQL;
  23. }
  24. $this->execute($sql);
  25. } catch (\Exception $e) {
  26. echo $e->getMessage();
  27. }
  28. }
  29. protected function columnExists($tableName, $columnName)
  30. {
  31. $tableSchema = Yii::$app->db->getTableSchema($tableName, true);
  32. return $tableSchema && in_array($columnName, $tableSchema->columnNames);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function safeDown()
  38. {
  39. echo "M250916155432ModifyAgentLevel cannot be reverted.\n";
  40. return false;
  41. }
  42. }