UpdateController.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class UpdateController extends BaseController {
  5. //升级数据库
  6. public function db(){
  7. if (strtolower(C("DB_TYPE")) == 'mysql' ) {
  8. $this->mysql();
  9. }
  10. }
  11. //升级mysql数据库
  12. public function mysql(){
  13. //user表的username字段增大了长度,防止长邮箱的用户名注册不了
  14. $sql = "alter table ".C('DB_PREFIX')."user modify column username varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '' ";
  15. M("Catalog")->execute($sql);
  16. //item表增加last_update_time字段
  17. $columns = M("item")->getDbFields();
  18. if ($columns) {
  19. $has_it = 0 ;//是否存在该字段
  20. foreach ($columns as $key => $value) {
  21. if ($value == 'last_update_time') {
  22. $has_it = 1 ;
  23. }
  24. }
  25. if ($has_it === 0) {
  26. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD last_update_time INT( 11 ) NOT NULL DEFAULT '0' COMMENT '最后更新时间';";
  27. D("Item")->execute($sql);
  28. }
  29. }
  30. //更改catalog表的order字段名为s_number
  31. $columns = M("Catalog")->getDbFields();
  32. if ($columns) {
  33. foreach ($columns as $key => $value) {
  34. if ($value == 'order') {
  35. $sql = "ALTER TABLE `".C('DB_PREFIX')."catalog` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  36. M("Catalog")->execute($sql);
  37. }
  38. }
  39. }
  40. //更改page表的order字段名为s_number
  41. $columns = M("Page")->getDbFields();
  42. if ($columns) {
  43. foreach ($columns as $key => $value) {
  44. if ($value == 'order') {
  45. $sql = "ALTER TABLE `".C('DB_PREFIX')."page` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  46. M("Page")->execute($sql);
  47. }
  48. }
  49. }
  50. //更改page_history表的order字段名为s_number
  51. $columns = M("PageHistory")->getDbFields();
  52. if ($columns) {
  53. foreach ($columns as $key => $value) {
  54. if ($value == 'order') {
  55. $sql = "ALTER TABLE `".C('DB_PREFIX')."page_history` CHANGE `order` `s_number` INT( 10 ) NOT NULL DEFAULT '99' COMMENT '顺序号。数字越小越靠前。若此值全部相等时则按id排序';";
  56. M("PageHistory")->execute($sql);
  57. }
  58. }
  59. }
  60. //为catalog表增加addtime索引
  61. $indexs = M("Catalog")->query(" show index from ".C('DB_PREFIX')."catalog");
  62. if ($indexs) {
  63. $has_it = 0 ;//是否存在该索引
  64. foreach ($indexs as $key => $value) {
  65. if ($value['column_name'] =='addtime') {
  66. $has_it = 1 ;
  67. }
  68. }
  69. if ($has_it === 0 ) {
  70. M("Catalog")->execute("ALTER TABLE ".C('DB_PREFIX')."catalog ADD INDEX ( `addtime` ) ;");
  71. }
  72. }
  73. //为item表增加addtime索引
  74. $indexs = M("Item")->query(" show index from ".C('DB_PREFIX')."item");
  75. if ($indexs) {
  76. $has_it = 0 ;//是否存在该索引
  77. foreach ($indexs as $key => $value) {
  78. if ($value['column_name'] =='addtime') {
  79. $has_it = 1 ;
  80. }
  81. }
  82. if ($has_it === 0 ) {
  83. M("Item")->execute("ALTER TABLE ".C('DB_PREFIX')."item ADD INDEX ( `addtime` ) ;");
  84. }
  85. }
  86. //为page表增加addtime索引
  87. $indexs = M("Page")->query(" show index from ".C('DB_PREFIX')."page");
  88. if ($indexs) {
  89. $has_it = 0 ;//是否存在该索引
  90. foreach ($indexs as $key => $value) {
  91. if ($value['column_name'] =='addtime') {
  92. $has_it = 1 ;
  93. }
  94. }
  95. if ($has_it === 0 ) {
  96. M("page")->execute("ALTER TABLE ".C('DB_PREFIX')."page ADD INDEX ( `addtime` ) ;");
  97. }
  98. }
  99. //为page_history表增加addtime索引
  100. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  101. if ($indexs) {
  102. $has_it = 0 ;//是否存在该索引
  103. foreach ($indexs as $key => $value) {
  104. if ($value['column_name'] =='addtime') {
  105. $has_it = 1 ;
  106. }
  107. }
  108. if ($has_it === 0 ) {
  109. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `addtime` ) ;");
  110. }
  111. }
  112. //为page_history表增加page_id索引
  113. $indexs = M("PageHistory")->query(" show index from ".C('DB_PREFIX')."page_history");
  114. if ($indexs) {
  115. $has_it = 0 ;//是否存在该索引
  116. foreach ($indexs as $key => $value) {
  117. if ($value['column_name'] =='page_id') {
  118. $has_it = 1 ;
  119. }
  120. }
  121. if ($has_it === 0 ) {
  122. M("PageHistory")->execute("ALTER TABLE ".C('DB_PREFIX')."page_history ADD INDEX ( `page_id` ) ;");
  123. }
  124. }
  125. echo "OK!";
  126. }
  127. }