UpdateController.class.php 4.8 KB

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