Guest User

Untitled

a guest
Dec 10th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. <?php
  2.  
  3. const DBHOST = 'localhost';
  4. CONST DBPORT = '3306';
  5. const DBNAME = 'test';
  6. const DBUSER = 'root';
  7. const DBPASS = '';
  8.  
  9.  
  10. withDb(function($db) {
  11. $total = 0;
  12. foreach (uidGenerator(1000) as $uid) {
  13. migrateUser($uid);
  14. $total++;
  15. }
  16. echo "Total users migrated:$total\n";
  17. });
  18.  
  19. function uidGenerator($limit) {
  20. $uids = [];
  21. $lastId = 0;
  22.  
  23. while (true) {
  24. $uids = [];
  25.  
  26. withDb(function($db) use (&$uids, $lastId, $limit) {
  27. $selectUsers = $db->prepare('SELECT uid from tbl_member WHERE uid > :lastId AND col1 > 0 ORDER BY uid ASC LIMIT :limit');
  28. $selectUsers->bindParam(':lastId', $lastId, PDO::PARAM_INT);
  29. $selectUsers->bindParam(':limit', $limit, PDO::PARAM_INT);
  30. $selectUsers->execute();
  31. foreach($selectUsers->fetchAll() as $user) {
  32. $uids []= $user['uid'];
  33. }
  34. $selectUsers->closeCursor();
  35. });
  36.  
  37. if (! $uids) {
  38. break;
  39. }
  40.  
  41. foreach ($uids as $uid) {
  42. yield $uid;
  43. }
  44.  
  45. $lastId = end($uids);
  46. }
  47. }
  48.  
  49. function migrateUser($uid) {
  50. echo "Migrating user: $uid \n";
  51.  
  52. withTransaction(function(PDO $db) use ($uid) {
  53. $selectMember = $db->prepare('SELECT col1 FROM tbl_member WHERE uid = :uid FOR UPDATE');
  54. $updateMember = $db->prepare('UPDATE tbl_member SET col1 = :col1 WHERE uid = :uid');
  55. $insertLog = $db->prepare('INSERT INTO tbl_member_log (uid, oldCol1, newCol1) VALUES (:uid, :oldCol1, newCol1)');
  56.  
  57. $selectMember->execute([':uid' => $uid]);
  58. $member = $selectMember->fetch();
  59.  
  60. $newCol1 = bcadd($member['col1'], 100, 2);
  61.  
  62. $updateMember->execute([
  63. ':uid' => $uid,
  64. ':col1' => $newCol1,
  65. ]);
  66.  
  67. $insertLog->execute([
  68. ':uid' => $uid,
  69. ':oldCol1' => $member['col1'],
  70. ':newCol1' => $newCol1,
  71. ]);
  72.  
  73. $selectMember->closeCursor();
  74. $updateMember->closeCursor();
  75. $insertLog->closeCursor();
  76. });
  77. }
  78.  
  79. function withTransaction($func) {
  80. withDb(function(PDO $db) use ($func) {
  81. try {
  82. $db->beginTransaction();
  83. $func($db);
  84. $db->commit();
  85. } catch (Exception $e) {
  86. $db->rollBack();
  87. throw $e;
  88. }
  89. });
  90. }
  91.  
  92. function withDb($func) {
  93. static $dbh = null;
  94. try {
  95. if ($dbh === null) {
  96. $dbh = new PDO(sprintf('mysql:host=%s;dbname=%s;charset=UTF8;port=%s', DBHOST, DBNAME, DBPORT), DBUSER, DBPASS);
  97. if (! $dbh) {
  98. throw new \Exception('Can not connect to database!');
  99. }
  100. }
  101.  
  102. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  103.  
  104. $func($dbh);
  105. } catch (PDOException $e) {
  106. print "Error!: " . $e->getMessage() . PHP_EOL;
  107. die();
  108. }
  109. }
Add Comment
Please, Sign In to add comment