public function test()
 {
     $this->adapter->expects($this->atLeastOnce())->method('hasTable')->will($this->returnValueMap([['users', true], ['users_new', true]]));
     $this->adapter->expects($this->atLeastOnce())->method('quoteTableName')->will($this->returnCallback(function ($name) {
         return "`{$name}`";
     }));
     $expected = ["set @lhm_auto_commit = @@session.autocommit /* large hadron migration (php) */", "set session autocommit = 0 /* large hadron migration (php) */", "LOCK TABLE `users` write, `users_new` write /* large hadron migration (php) */", "ALTER TABLE  `users` rename `users_archive` /* large hadron migration (php) */", "ALTER TABLE `users_new` rename `users` /* large hadron migration (php) */", "COMMIT /* large hadron migration (php) */", "UNLOCK TABLES /* large hadron migration (php) */", "set session autocommit = @lhm_auto_commit /* large hadron migration (php) */"];
     $matcher = $this->any();
     $this->adapter->expects($matcher)->method('query')->will($this->returnCallback(function ($query) use($matcher, $expected) {
         $this->assertEquals($expected[$matcher->getInvocationCount() - 1], $query);
     }));
     $this->origin->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('users'));
     $this->destination->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('users_new'));
     $this->switcher->run();
 }
示例#2
0
 /**
  * @param callable $migration Closure that receives the table to operate on.
  *
  *  <example>
  *  $migration->execute(function($table) {
  *      $table
  *          ->removeColumn('name')
  *          ->save();
  *  });
  *  </example>
  */
 public function execute(callable $migration)
 {
     $this->logger->info("Starting LHM run on table={$this->origin->getName()}");
     $sqlHelper = new SqlHelper($this->adapter);
     if (!isset($options['atomic_switch'])) {
         if ($sqlHelper->supportsAtomicSwitch()) {
             $this->options['atomic_switch'] = true;
         } else {
             $version = $sqlHelper->versionString();
             throw new \RuntimeException("Using mysql {$version}. You must explicitly set `options['atomic_switch']` (re SqlHelper::supportsAtomicSwitch)");
         }
     }
     if (!$this->destination) {
         $this->destination = $this->temporaryTable();
     }
     $this->setSessionLockWaitTimeouts();
     $entangler = new Entangler($this->adapter, $this->origin, $this->destination, $sqlHelper);
     $entangler->setLogger($this->logger);
     if ($this->options['atomic_switch']) {
         $switcher = new AtomicSwitcher($this->adapter, $this->origin, $this->destination, $this->options);
     } else {
         $switcher = new LockedSwitcher($this->adapter, $this->origin, $this->destination, $this->options);
     }
     $switcher->setLogger($this->logger);
     $chunker = new Chunker($this->adapter, $this->origin, $this->destination, $sqlHelper, $this->options);
     $chunker->setLogger($this->logger);
     $migration($this->destination);
     $entangler->run(function () use($chunker, $switcher) {
         $chunker->run();
         $switcher->run();
     });
 }
 public function testItRenamesDestinationToOrigin()
 {
     $this->assertTrue($this->adapter->hasTable($this->destination->getName()));
     $this->switcher->run();
     $this->assertFalse($this->adapter->hasTable($this->destination->getName()));
 }