20211210160700_user_dict_copy.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /*
  3. 从旧数据表中提取数据插入到新的表
  4. 插入时用uuid判断是否曾经插入
  5. 曾经插入就不插入了
  6. */
  7. require_once __DIR__."/../../app/config.php";
  8. # 更新索引表
  9. $src_db=_FILE_SRC_WBW_;#源数据库
  10. $src_table=_TABLE_SRC_DICT_WBW_;#源表名
  11. $dest_db=_FILE_DB_WBW_;#目标数据库
  12. $dest_table=_TABLE_DICT_WBW_;#目标表名
  13. echo "migarate user dict".PHP_EOL;
  14. #打开源数据库
  15. $PDO_SRC = new PDO($src_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  16. $PDO_SRC->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  17. echo "open src".PHP_EOL;
  18. #打开目标数据库
  19. $PDO_DEST = new PDO($dest_db,_DB_USERNAME_,_DB_PASSWORD_,array(PDO::ATTR_PERSISTENT=>true));
  20. $PDO_DEST->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  21. echo "open dest".PHP_EOL;
  22. #删除源数据表中全部数据
  23. fwrite(STDOUT,"delete dest".PHP_EOL);
  24. $query = "delete from $dest_table where true;";
  25. $stmtDest = $PDO_DEST->prepare($query);
  26. $stmtDest->execute();
  27. $queryInsert = "INSERT INTO ".$dest_table."
  28. (
  29. word ,
  30. type,
  31. gramma,
  32. parent,
  33. mean,
  34. note,
  35. factors,
  36. factormean,
  37. status,
  38. source,
  39. language,
  40. confidence,
  41. creator_id,
  42. ref_counter,
  43. create_time,
  44. created_at,
  45. updated_at
  46. )
  47. VALUES ( ? , ? , ? ,? ,? ,? ,? ,? ,? ,? , ?,?,?,?,?,to_timestamp(?),to_timestamp(?))";
  48. echo "read from orginal".PHP_EOL;
  49. $commitData = [];
  50. $allInsertCount = 0;
  51. $allSrcCount = 0;
  52. $count = 0;
  53. #从源数据表中读取
  54. $query = "SELECT * FROM ".$src_table." WHERE true ";
  55. $stmtSrc = $PDO_SRC->prepare($query);
  56. $stmtSrc->execute();
  57. while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
  58. $allSrcCount++;
  59. #插入目标表
  60. //if(strlen($srcData["id"])>10 && strlen($srcData["owner"])>30)
  61. {
  62. {
  63. #没有相同数据
  64. $commitData[] = array(
  65. $srcData["pali"],
  66. $srcData["type"],
  67. $srcData["gramma"],
  68. $srcData["parent"],
  69. $srcData["mean"],
  70. $srcData["note"],
  71. $srcData["factors"],
  72. $srcData["factormean"],
  73. (int)$srcData["status"],
  74. "_SYS_USER_WBW_",
  75. $srcData["language"],
  76. (int)$srcData["confidence"],
  77. (int)$srcData["creator"],
  78. $srcData["ref_counter"],
  79. $srcData["time"],
  80. $srcData["time"],
  81. $srcData["time"]
  82. );
  83. $count++;
  84. $allInsertCount++;
  85. }
  86. if($count ==10000){
  87. #10000行插入一次
  88. $PDO_DEST->beginTransaction();
  89. $stmtDEST = $PDO_DEST->prepare($queryInsert);
  90. foreach ($commitData as $key => $value) {
  91. $stmtDEST->execute($value);
  92. if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
  93. $error = $PDO_DEST->errorInfo();
  94. echo "error - $error[2] ";
  95. exit;
  96. }
  97. }
  98. // 提交更改
  99. $PDO_DEST->commit();
  100. $commitData = [];
  101. echo "finished $count".PHP_EOL;
  102. $count=0;
  103. }
  104. }
  105. }
  106. if($count>0){
  107. #最后的没有到10000的数据插入
  108. $PDO_DEST->beginTransaction();
  109. $stmtDEST = $PDO_DEST->prepare($queryInsert);
  110. foreach ($commitData as $key => $value) {
  111. $stmtDEST->execute($value);
  112. if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
  113. $error = $PDO_DEST->errorInfo();
  114. echo "error - $error[2] ";
  115. exit;
  116. }
  117. }
  118. // 提交更改
  119. $PDO_DEST->commit();
  120. $commitData = [];
  121. echo "finished $count".PHP_EOL;
  122. }
  123. echo "insert done $allInsertCount in $allSrcCount ".PHP_EOL;
  124. # 更新索引表
  125. $src_table=_TABLE_SRC_DICT_WBW_INDEX_;#源表名
  126. $src_word_table=_TABLE_SRC_DICT_WBW_;#源word表名
  127. echo "migarating usr dict index ".PHP_EOL;
  128. $queryInsert = "INSERT INTO ".$dest_table."
  129. (
  130. word ,
  131. type,
  132. gramma,
  133. parent,
  134. mean,
  135. note,
  136. factors,
  137. factormean,
  138. status,
  139. source,
  140. language,
  141. confidence,
  142. creator_id,
  143. ref_counter,
  144. create_time,
  145. created_at,
  146. updated_at
  147. )
  148. VALUES ( ? , ? , ? ,? ,? ,? ,? ,? ,? ,? , ?,?,?,?,?, to_timestamp(?), to_timestamp(?))";
  149. // 开始一个事务,关闭自动提交
  150. $commitData = [];
  151. $allInsertCount = 0;
  152. $allSrcCount = 0;
  153. $count = 0;
  154. #从源数据表中读取
  155. $query = "SELECT * FROM ".$src_table." WHERE true ";
  156. $stmtSrc = $PDO_SRC->prepare($query);
  157. $stmtSrc->execute();
  158. while($srcData = $stmtSrc->fetch(PDO::FETCH_ASSOC)){
  159. $allSrcCount++;
  160. #插入目标表
  161. $wordIndex = $srcData["word_index"];
  162. #查询目标表中的数据
  163. $queryExsit = "SELECT * FROM ".$src_word_table." WHERE id = ? ";
  164. $getWord = $PDO_SRC->prepare($queryExsit);
  165. $getWord->execute(array($wordIndex));
  166. $exist = $getWord->fetch(PDO::FETCH_ASSOC);
  167. if($exist){
  168. $commitData[] = array(
  169. $exist["pali"],
  170. $exist["type"],
  171. $exist["gramma"],
  172. $exist["parent"],
  173. $exist["mean"],
  174. $exist["note"],
  175. $exist["factors"],
  176. $exist["factormean"],
  177. (int)$exist["status"],
  178. "_USER_WBW_",
  179. $exist["language"],
  180. (int)$exist["confidence"],
  181. (int)$srcData["user_id"],
  182. $exist["ref_counter"],
  183. $exist["time"],
  184. $exist["time"],
  185. $exist["time"]
  186. );
  187. $count++;
  188. $allInsertCount++;
  189. if($count === 10000){
  190. $PDO_DEST->beginTransaction();
  191. $stmtDEST = $PDO_DEST->prepare($queryInsert);
  192. foreach ($commitData as $key => $value) {
  193. # code...
  194. $stmtDEST->execute($value);
  195. if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
  196. $error = $PDO_DEST->errorInfo();
  197. echo "error - $error[2] ";
  198. exit;
  199. }
  200. }
  201. // 提交更改
  202. $PDO_DEST->commit();
  203. echo "finished $count".PHP_EOL;
  204. $count = 0;
  205. }
  206. }else{
  207. fwrite(STDERR,"error: no word index - $wordIndex".PHP_EOL);
  208. }
  209. if($allSrcCount % 10000 ==0){
  210. fwrite(STDOUT,"find from src table $allSrcCount / $allInsertCount is new.".PHP_EOL) ;
  211. }
  212. }
  213. if($count>0){
  214. #最后的没有到10000的数据插入
  215. $PDO_DEST->beginTransaction();
  216. $stmtDEST = $PDO_DEST->prepare($queryInsert);
  217. foreach ($commitData as $key => $value) {
  218. # code...
  219. $stmtDEST->execute($value);
  220. if (!$stmtDEST || ($stmtDEST && $stmtDEST->errorCode() != 0)) {
  221. $error = $PDO_DEST->errorInfo();
  222. fwrite(STDOUT,"error - $error[2] ");
  223. exit;
  224. }
  225. }
  226. // 提交更改
  227. $PDO_DEST->commit();
  228. fwrite(STDOUT,"finished $count".PHP_EOL);
  229. }
  230. fwrite(STDOUT,"insert done $allInsertCount in $allSrcCount ".PHP_EOL);
  231. fwrite(STDOUT,"all done".PHP_EOL);