You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 rivejä
15 KiB

3 vuotta sitten
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class UpdateController extends BaseController {
  5. //检测数据库并更新
  6. public function checkDb($showBack = true){
  7. $version_num = 5 ;
  8. $db_version_num = D("Options")->get("db_version_num");
  9. if(!$db_version_num || $db_version_num < $version_num ){
  10. $r = $this->updateSqlite();
  11. if($r){
  12. D("Options")->set("db_version_num" , $version_num );
  13. }
  14. //echo '执行数据库升级';
  15. }
  16. if($showBack){
  17. $this->sendResult(array());
  18. }
  19. }
  20. public function updateSqlite(){
  21. //catalog表增加parent_cat_id字段
  22. if (!$this->_is_column_exist("catalog","parent_cat_id")) {
  23. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD parent_cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  24. D("catalog")->execute($sql);
  25. }
  26. //catalog表增加level字段
  27. if (!$this->_is_column_exist("catalog","level")) {
  28. $sql = "ALTER TABLE ".C('DB_PREFIX')."catalog ADD level INT( 10 ) NOT NULL DEFAULT '2' ;";
  29. D("catalog")->execute($sql);
  30. }
  31. //item表增加item_domain字段
  32. if (!$this->_is_column_exist("item","item_domain")) {
  33. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_domain text NOT NULL DEFAULT '';";
  34. D("catalog")->execute($sql);
  35. }
  36. //创建user_token表
  37. $sql = "CREATE TABLE IF NOT EXISTS `user_token` (
  38. `id` INTEGER PRIMARY KEY ,
  39. `uid` int(10) NOT NULL DEFAULT '0',
  40. `token` CHAR(200) NOT NULL DEFAULT '',
  41. `token_expire` int(11) NOT NULL DEFAULT '0' ,
  42. `ip` CHAR(200) NOT NULL DEFAULT '',
  43. `addtime` int(11) NOT NULL DEFAULT '0'
  44. )";
  45. D("UserToken")->execute($sql);
  46. //创建template表
  47. $sql = "CREATE TABLE IF NOT EXISTS `template` (
  48. `id` INTEGER PRIMARY KEY ,
  49. `uid` int(10) NOT NULL DEFAULT '0',
  50. `username` CHAR(200) NOT NULL DEFAULT '',
  51. `template_title` CHAR(200) NOT NULL DEFAULT '' ,
  52. `template_content` text NOT NULL DEFAULT '',
  53. `addtime` int(11) NOT NULL DEFAULT '0'
  54. )";
  55. D("UserToken")->execute($sql);
  56. //page表增加page_comments字段
  57. if (!$this->_is_column_exist("page","page_comments")) {
  58. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD page_comments text NOT NULL DEFAULT '' ;";
  59. D("catalog")->execute($sql);
  60. }
  61. //page_history 表增加page_comments字段
  62. if (!$this->_is_column_exist("PageHistory","page_comments")) {
  63. $sql = "ALTER TABLE ".C('DB_PREFIX')."page_history ADD page_comments text NOT NULL DEFAULT '';";
  64. D("catalog")->execute($sql);
  65. }
  66. //item_member表增加member_group_id字段
  67. if (!$this->_is_column_exist("ItemMember","member_group_id")) {
  68. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_member ADD member_group_id INT( 1 ) NOT NULL DEFAULT '1' ;";
  69. D("ItemMember")->execute($sql);
  70. }
  71. //item表增加item_type字段
  72. if (!$this->_is_column_exist("Item","item_type")) {
  73. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD item_type INT( 1 ) NOT NULL DEFAULT '1' ;";
  74. D("ItemMember")->execute($sql);
  75. }
  76. //创建options表
  77. $sql = "CREATE TABLE IF NOT EXISTS `options` (
  78. `option_id` INTEGER PRIMARY KEY ,
  79. `option_name` CHAR(200) NOT NULL UNIQUE ,
  80. `option_value` CHAR(200) NOT NULL
  81. )";
  82. D("UserToken")->execute($sql);
  83. //创建item_token表
  84. $sql = "CREATE TABLE IF NOT EXISTS `item_token` (
  85. `id` INTEGER PRIMARY KEY ,
  86. `item_id` int(11) NOT NULL DEFAULT '0' ,
  87. `api_key` CHAR(200) NOT NULL UNIQUE ,
  88. `api_token` CHAR(200) NOT NULL ,
  89. `addtime` int(11) NOT NULL DEFAULT '0' ,
  90. `last_check_time` int(11) NOT NULL DEFAULT '0'
  91. )";
  92. D("UserToken")->execute($sql);
  93. //创建item_top表
  94. $sql = "CREATE TABLE IF NOT EXISTS `item_top` (
  95. `id` INTEGER PRIMARY KEY ,
  96. `item_id` int(11) NOT NULL DEFAULT '0' ,
  97. `uid` int(11) NOT NULL DEFAULT '0' ,
  98. `addtime` int(11) NOT NULL DEFAULT '0'
  99. )";
  100. D("UserToken")->execute($sql);
  101. //item表增加is_archived字段
  102. if (!$this->_is_column_exist("Item","is_archived")) {
  103. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_archived INT( 1 ) NOT NULL DEFAULT '0' ;";
  104. D("ItemMember")->execute($sql);
  105. }
  106. //管理员账户和权限
  107. if(D("User")->where("username = 'showdoc' ")->find()){
  108. D("User")->where("username = 'showdoc' ")->save(array("groupid"=> 1)) ;
  109. }else{
  110. D("User")->add(array('username'=>"showdoc" ,"groupid"=>1,'password'=>"a89da13684490eb9ec9e613f91d24d00" , 'reg_time'=>time()));
  111. }
  112. //item表增加is_del字段
  113. if (!$this->_is_column_exist("Item","is_del")) {
  114. $sql = "ALTER TABLE ".C('DB_PREFIX')."item ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  115. D("ItemMember")->execute($sql);
  116. }
  117. //page表增加is_del字段
  118. if (!$this->_is_column_exist("Page","is_del")) {
  119. $sql = "ALTER TABLE ".C('DB_PREFIX')."page ADD is_del INT( 1 ) NOT NULL DEFAULT '0' ;";
  120. D("ItemMember")->execute($sql);
  121. }
  122. //创建team表
  123. $sql = "CREATE TABLE IF NOT EXISTS `team` (
  124. `id` INTEGER PRIMARY KEY ,
  125. `team_name` CHAR(200) NOT NULL DEFAULT '',
  126. `uid` int(11) NOT NULL DEFAULT '0' ,
  127. `username` CHAR(200) NOT NULL DEFAULT '',
  128. `addtime` int(11) NOT NULL DEFAULT '0' ,
  129. `last_update_time` int(11) NOT NULL DEFAULT '0'
  130. )";
  131. D("User")->execute($sql);
  132. //创建team_item表
  133. $sql = "CREATE TABLE IF NOT EXISTS `team_item` (
  134. `id` INTEGER PRIMARY KEY ,
  135. `team_id` int(11) NOT NULL DEFAULT '0' ,
  136. `item_id` int(11) NOT NULL DEFAULT '0' ,
  137. `addtime` int(11) NOT NULL DEFAULT '0' ,
  138. `last_update_time` int(11) NOT NULL DEFAULT '0'
  139. )";
  140. D("User")->execute($sql);
  141. //创建team_item_member表
  142. $sql = "CREATE TABLE IF NOT EXISTS `team_item_member` (
  143. `id` INTEGER PRIMARY KEY ,
  144. `team_id` int(11) NOT NULL DEFAULT '0' ,
  145. `item_id` int(11) NOT NULL DEFAULT '0' ,
  146. `member_group_id` int(11) NOT NULL DEFAULT '0' ,
  147. `member_uid` int(11) NOT NULL DEFAULT '0' ,
  148. `member_username` CHAR(200) NOT NULL DEFAULT '',
  149. `addtime` int(11) NOT NULL DEFAULT '0' ,
  150. `last_update_time` int(11) NOT NULL DEFAULT '0'
  151. )";
  152. D("User")->execute($sql);
  153. //创建team_member表
  154. $sql = "CREATE TABLE IF NOT EXISTS `team_member` (
  155. `id` INTEGER PRIMARY KEY ,
  156. `team_id` int(11) NOT NULL DEFAULT '0' ,
  157. `member_uid` int(11) NOT NULL DEFAULT '0' ,
  158. `member_username` CHAR(200) NOT NULL DEFAULT '',
  159. `addtime` int(11) NOT NULL DEFAULT '0' ,
  160. `last_update_time` int(11) NOT NULL DEFAULT '0'
  161. )";
  162. D("User")->execute($sql);
  163. //创建upload_file表
  164. $sql = "CREATE TABLE IF NOT EXISTS `upload_file` (
  165. `file_id` INTEGER PRIMARY KEY ,
  166. `sign` CHAR(200) NOT NULL DEFAULT '',
  167. `display_name` CHAR(200) NOT NULL DEFAULT '',
  168. `file_type` CHAR(200) NOT NULL DEFAULT '',
  169. `file_size` CHAR(200) NOT NULL DEFAULT '',
  170. `uid` int(11) NOT NULL DEFAULT '0' ,
  171. `page_id` int(11) NOT NULL DEFAULT '0' ,
  172. `item_id` int(11) NOT NULL DEFAULT '0' ,
  173. `visit_times` int(11) NOT NULL DEFAULT '0' ,
  174. `addtime` int(11) NOT NULL DEFAULT '0' ,
  175. `real_url` CHAR(200) NOT NULL DEFAULT '',
  176. `last_update_time` int(11) NOT NULL DEFAULT '0'
  177. )";
  178. D("User")->execute($sql);
  179. //创建item_sort表
  180. $sql = "CREATE TABLE IF NOT EXISTS `item_sort` (
  181. `id` INTEGER PRIMARY KEY ,
  182. `uid` int(10) NOT NULL DEFAULT '0',
  183. `item_sort_data` text NOT NULL DEFAULT '',
  184. `addtime` int(11) NOT NULL DEFAULT '0'
  185. )";
  186. D("UserToken")->execute($sql);
  187. //创建single_page表
  188. $sql = "CREATE TABLE IF NOT EXISTS `single_page` (
  189. `id` INTEGER PRIMARY KEY ,
  190. `unique_key` CHAR(200) NOT NULL DEFAULT '',
  191. `page_id` int(11) NOT NULL DEFAULT '0'
  192. )";
  193. D("User")->execute($sql);
  194. //创建captcha表
  195. $sql = "CREATE TABLE IF NOT EXISTS `captcha` (
  196. `captcha_id` INTEGER PRIMARY KEY ,
  197. `mobile` CHAR(200) NOT NULL DEFAULT '',
  198. `captcha` CHAR(200) NOT NULL DEFAULT '',
  199. `expire_time` int(11) NOT NULL DEFAULT '0'
  200. )";
  201. D("User")->execute($sql);
  202. //创建recycle表
  203. $sql = "CREATE TABLE IF NOT EXISTS `recycle` (
  204. `id` INTEGER PRIMARY KEY ,
  205. `item_id` int(11) NOT NULL DEFAULT '0',
  206. `page_id` int(11) NOT NULL DEFAULT '0',
  207. `page_title` CHAR(200) NOT NULL DEFAULT '',
  208. `del_by_uid` int(11) NOT NULL DEFAULT '0',
  209. `del_by_username` CHAR(200) NOT NULL DEFAULT '',
  210. `del_time` int(11) NOT NULL DEFAULT '0'
  211. )";
  212. D("User")->execute($sql);
  213. //创建page_lock表
  214. $sql = "CREATE TABLE IF NOT EXISTS `page_lock` (
  215. `id` INTEGER PRIMARY KEY ,
  216. `page_id` int(11) NOT NULL DEFAULT '0',
  217. `lock_uid` int(11) NOT NULL DEFAULT '0',
  218. `lock_username` CHAR(200) NOT NULL DEFAULT '',
  219. `lock_to` int(11) NOT NULL DEFAULT '0',
  220. `addtime` int(11) NOT NULL DEFAULT '0'
  221. )";
  222. D("User")->execute($sql);
  223. //item_member表增加cat_id字段
  224. if (!$this->_is_column_exist("item_member","cat_id")) {
  225. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_member ADD cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  226. D("User")->execute($sql);
  227. }
  228. //team_item_member表增加cat_id字段
  229. if (!$this->_is_column_exist("team_item_member","cat_id")) {
  230. $sql = "ALTER TABLE ".C('DB_PREFIX')."team_item_member ADD cat_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  231. D("User")->execute($sql);
  232. }
  233. //创建item_variable表
  234. $sql = "CREATE TABLE IF NOT EXISTS `item_variable` (
  235. `id` INTEGER PRIMARY KEY ,
  236. `var_name` CHAR(2000) NOT NULL DEFAULT '',
  237. `var_value` CHAR(2000) NOT NULL DEFAULT '',
  238. `uid` int(11) NOT NULL DEFAULT '0',
  239. `item_id` int(11) NOT NULL DEFAULT '0',
  240. `addtime` int(11) NOT NULL DEFAULT '0'
  241. )";
  242. D("User")->execute($sql);
  243. //创建file_flow表
  244. $sql = "CREATE TABLE IF NOT EXISTS `file_flow` (
  245. `id` INTEGER PRIMARY KEY ,
  246. `uid` int(11) NOT NULL DEFAULT '0',
  247. `used` int(11) NOT NULL DEFAULT '0',
  248. `date_month` CHAR(2000) NOT NULL DEFAULT ''
  249. )";
  250. D("User")->execute($sql);
  251. //item_variable表增加env_id字段
  252. if (!$this->_is_column_exist("item_variable","env_id")) {
  253. $sql = "ALTER TABLE ".C('DB_PREFIX')."item_variable ADD env_id INT( 10 ) NOT NULL DEFAULT '0' ;";
  254. D("User")->execute($sql);
  255. }
  256. //创建runapi_env表
  257. $sql = "CREATE TABLE IF NOT EXISTS `runapi_env` (
  258. `id` INTEGER PRIMARY KEY ,
  259. `env_name` CHAR(2000) NOT NULL DEFAULT '',
  260. `item_id` int(11) NOT NULL DEFAULT '0',
  261. `uid` int(11) NOT NULL DEFAULT '0',
  262. `addtime` CHAR(2000) NOT NULL DEFAULT '',
  263. `last_update_time` CHAR(2000) NOT NULL DEFAULT ''
  264. )";
  265. D("User")->execute($sql);
  266. //创建runapi_env_selectd表
  267. $sql = "CREATE TABLE IF NOT EXISTS `runapi_env_selectd` (
  268. `id` INTEGER PRIMARY KEY ,
  269. `item_id` int(11) NOT NULL DEFAULT '0',
  270. `uid` int(11) NOT NULL DEFAULT '0',
  271. `env_id` int(11) NOT NULL DEFAULT '0'
  272. )";
  273. D("User")->execute($sql);
  274. //创建runapi_global_param表
  275. $sql = "CREATE TABLE IF NOT EXISTS `runapi_global_param` (
  276. `id` INTEGER PRIMARY KEY ,
  277. `item_id` int(11) NOT NULL DEFAULT '0',
  278. `param_type` CHAR(2000) NOT NULL DEFAULT '',
  279. `content_json_str` CHAR(2000) NOT NULL DEFAULT '',
  280. `addtime` CHAR(2000) NOT NULL DEFAULT '',
  281. `last_update_time` CHAR(2000) NOT NULL DEFAULT ''
  282. )";
  283. D("User")->execute($sql);
  284. //创建mock表
  285. $sql = "CREATE TABLE IF NOT EXISTS `mock` (
  286. `id` INTEGER PRIMARY KEY ,
  287. `unique_key` CHAR(2000) NOT NULL DEFAULT '',
  288. `uid` int(11) NOT NULL DEFAULT '0',
  289. `page_id` int(11) NOT NULL DEFAULT '0',
  290. `item_id` int(11) NOT NULL DEFAULT '0',
  291. `view_times` int(11) NOT NULL DEFAULT '0',
  292. `template` CHAR(2000) NOT NULL DEFAULT '',
  293. `addtime` CHAR(2000) NOT NULL DEFAULT '',
  294. `last_update_time` CHAR(2000) NOT NULL DEFAULT ''
  295. )";
  296. D("User")->execute($sql);
  297. //创建file_page表
  298. $sql = "CREATE TABLE IF NOT EXISTS `file_page` (
  299. `id` INTEGER PRIMARY KEY ,
  300. `file_id` int(11) NOT NULL DEFAULT '0',
  301. `page_id` int(11) NOT NULL DEFAULT '0',
  302. `item_id` int(11) NOT NULL DEFAULT '0',
  303. `addtime` CHAR(2000) NOT NULL DEFAULT ''
  304. )";
  305. D("User")->execute($sql);
  306. // 如果file_page尚未有数据,则把upload_file表的数据转换过去
  307. if( !D("FilePage")->find()){
  308. $files = D("UploadFile")->select() ;
  309. if($files){
  310. foreach ($files as $key => $value) {
  311. D("FilePage")->add( array(
  312. "file_id" => $value['file_id'] ,
  313. "page_id" => $value['page_id'] ,
  314. "item_id" => $value['item_id'] ,
  315. "addtime" => $value['addtime'] ,
  316. )) ;
  317. }
  318. }
  319. }
  320. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  321. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  322. //留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
  323. return true ;
  324. }
  325. private function _is_column_exist($table , $column){
  326. $has_it = false ;//是否存在该字段
  327. $columns = M($table)->getDbFields();
  328. if ($columns) {
  329. foreach ($columns as $key => $value) {
  330. if ($value == $column) {
  331. $has_it = true ;
  332. }
  333. }
  334. }
  335. return $has_it ;
  336. }
  337. }