/** * @throws \RuntimeException */ protected function validate() { if ($this->adapter->hasTable($this->origin->getName()) && $this->adapter->hasTable($this->destination->getName())) { return; } throw new \RuntimeException("Table `{$this->origin->getName()}` and `{$this->destination->getName()}` must exist."); }
public function testRun() { /** @var Column[] $originColumns */ $originColumns = [new Column(), new Column(), new Column()]; $originColumns[0]->setName('id'); $originColumns[1]->setName('name'); $originColumns[2]->setName('something'); /** @var Column[] $destinationColumns */ $destinationColumns = [new Column(), new Column(), new Column()]; $destinationColumns[0]->setName('id'); $destinationColumns[1]->setName('name'); $destinationColumns[2]->setName('something_else'); $this->origin->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('users')); $this->origin->expects($this->atLeastOnce())->method('getColumns')->will($this->returnValue($originColumns)); $this->destination->expects($this->atLeastOnce())->method('getName')->will($this->returnValue('users_new')); $this->destination->expects($this->atLeastOnce())->method('getColumns')->will($this->returnValue($destinationColumns)); $this->destination->expects($this->atLeastOnce())->method('getRenamedColumns')->will($this->returnValue([])); $matcher = $this->atLeastOnce(); $this->adapter->expects($matcher)->method('fetchRow')->will($this->returnCallback(function ($query) use($matcher) { switch ($matcher->getInvocationCount()) { case 1: $this->assertEquals("SELECT MIN(id) FROM 'users'", $query); return [1]; case 2: $this->assertEquals("SELECT MAX(id) FROM 'users'", $query); return [500]; default: return null; break; } })); $matcher = $this->atLeastOnce(); $this->adapter->expects($matcher)->method('query')->will($this->returnCallback(function ($query) use($matcher) { switch ($matcher->getInvocationCount()) { case 1: $this->assertEquals("SELECT `COLUMN_NAME` FROM `information_schema`.`COLUMNS` WHERE (`TABLE_SCHEMA` = '') AND (`TABLE_NAME` = 'users') AND (`COLUMN_KEY` = 'PRI');", $query); return 'id'; case 2: $this->assertEquals("INSERT IGNORE INTO 'users_new' (`id`,`name`) SELECT 'users'.`id`,'users'.`name` FROM 'users' WHERE 'users'.`id` BETWEEN 1 AND 200", $query); break; case 3: $this->assertEquals("INSERT IGNORE INTO 'users_new' (`id`,`name`) SELECT 'users'.`id`,'users'.`name` FROM 'users' WHERE 'users'.`id` BETWEEN 201 AND 400", $query); break; case 4: $this->assertEquals("INSERT IGNORE INTO 'users_new' (`id`,`name`) SELECT 'users'.`id`,'users'.`name` FROM 'users' WHERE 'users'.`id` BETWEEN 401 AND 500", $query); break; default: $this->fail('Unexpected query: ' . $query); break; } })); $chunker = new Chunker($this->adapter, $this->origin, $this->destination, $this->sqlHelper, ['stride' => 200]); $chunker->run(); }
/** * Extract the primary key of a table. * * @param \Phinx\Db\Table $table * @return string */ public function extractPrimaryKey(\Phinx\Db\Table $table) { $tableName = $table->getName(); $databaseName = $this->adapter->getOption('name'); $query = implode(" ", ['SELECT `COLUMN_NAME`', 'FROM `information_schema`.`COLUMNS`', "WHERE (`TABLE_SCHEMA` = '{$databaseName}')", "AND (`TABLE_NAME` = '{$tableName}')", "AND (`COLUMN_KEY` = 'PRI');"]); $result = $this->adapter->query($query); if ($result instanceof \PDOStatement) { return $result->fetchColumn(0); } if (is_array($result)) { return $result[0]; } return $result; }
/** * {@inheritdoc} */ public function removeColumn($columnName) { if (isset($this->renames[$columnName])) { unset($this->renames[$columnName]); } return parent::removeColumn($columnName); }
/** * {@inheritdoc} */ public function create() { if ((!isset($this->options['id']) || $this->options['id'] === false) && !empty($this->primaryKey)) { $this->options['primary_key'] = $this->primaryKey; $this->filterPrimaryKey(); } parent::create(); }
protected function setUp() { parent::setUp(); $this->origin = $this->getMockBuilder(\Phinx\Db\Table::class)->disableOriginalConstructor()->getMock(); $this->destination = $this->getMockBuilder(\Lhm\Table::class)->disableOriginalConstructor()->getMock(); /** @var Column[] $originColumns */ $originColumns = [new Column(), new Column(), new Column()]; $originColumns[0]->setName('id'); $originColumns[1]->setName('name'); $originColumns[2]->setName('something'); /** @var Column[] $destinationColumns */ $destinationColumns = [new Column(), new Column(), new Column()]; $destinationColumns[0]->setName('id'); $destinationColumns[1]->setName('name'); $destinationColumns[2]->setName('something_else'); $this->origin->expects($this->atLeastOnce())->method('getColumns')->will($this->returnValue($originColumns)); $this->destination->expects($this->atLeastOnce())->method('getColumns')->will($this->returnValue($destinationColumns)); $this->intersection = new Intersection($this->origin, $this->destination); }
/** * {@inheritdoc} */ public function addForeignKey(Table $table, ForeignKey $foreignKey) { $adapterTable = clone $table; $adapterTableName = $this->getAdapterTableName($table->getName()); $adapterTable->setName($adapterTableName); return parent::addForeignKey($adapterTable, $foreignKey); }
/** * {@inheritdoc} */ public function createSchemaTable() { try { $options = array('id' => false, 'primary_key' => 'version'); $table = new Table($this->getSchemaTableName(), $options, $this); if ($this->getConnection()->getAttribute(\PDO::ATTR_DRIVER_NAME) === 'mysql' && version_compare($this->getConnection()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.6.0', '>=')) { $table->addColumn('version', 'biginteger', array('limit' => 14))->addColumn('start_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))->addColumn('end_time', 'timestamp', array('default' => 'CURRENT_TIMESTAMP'))->save(); } else { $table->addColumn('version', 'biginteger')->addColumn('start_time', 'timestamp')->addColumn('end_time', 'timestamp')->save(); } } catch (\Exception $exception) { throw new \InvalidArgumentException('There was a problem creating the schema table: ' . $exception->getMessage()); } }
/** * Gets the SQLite Index Definition for an Index object. * * @param Index $index Index * @return string */ protected function getIndexSqlDefinition(Table $table, Index $index) { if ($index->getType() == Index::UNIQUE) { $def = 'UNIQUE INDEX'; } else { $def = 'INDEX'; } if (is_string($index->getName())) { $indexName = $index->getName(); } else { $indexName = $table->getName() . '_'; foreach ($index->getColumns() as $column) { $indexName .= $column . '_'; } $indexName .= 'index'; } $def .= ' `' . $indexName . '`'; return $def; }
/** * {@inheritdoc} */ public function insert(Table $table, $row) { $adapterTable = clone $table; $adapterTableName = $this->getAdapterTableName($table->getName()); $adapterTable->setName($adapterTableName); return parent::insert($adapterTable, $row); }
public function testInsertData() { $row = array('column1' => 'value3'); $this->mock->expects($this->once())->method('insert')->with($this->callback(function ($table) { return $table->getName() == 'pre_table_suf'; }, $this->equalTo($row))); $table = new Table('table', array(), $this->adapter); $table->insert($row)->save(); }
/** * {@inheritdoc} */ public function insert($table, $data) { // convert to table object if (is_string($table)) { $table = new Table($table, array(), $this->getAdapter()); } return $table->insert($data)->save(); }
public function testItRenamesDestinationToOrigin() { $this->assertTrue($this->adapter->hasTable($this->destination->getName())); $this->switcher->run(); $this->assertFalse($this->adapter->hasTable($this->destination->getName())); }
/** * {@inheritdoc} */ public function addForeignKey(Table $table, ForeignKey $foreignKey) { // TODO: DRY this up.... $this->startCommandTimer(); $this->writeCommand('addForeignKey', array($table->getName(), $foreignKey->getColumns())); $this->execute('pragma foreign_keys = ON'); $tmpTableName = 'tmp_' . $table->getName(); $rows = $this->fetchAll('select * from sqlite_master where `type` = \'table\''); $sql = ''; foreach ($rows as $row) { if ($row['tbl_name'] == $table->getName()) { $sql = $row['sql']; } } $this->fetchAll(sprintf('pragma table_info(%s)', $this->quoteTableName($table->getName()))); $columns = array(); foreach ($columns as $column) { $columns[] = $this->quoteColumnName($column['name']); } $this->execute(sprintf('ALTER TABLE %s RENAME TO %s', $this->quoteTableName($table->getName()), $tmpTableName)); $sql = substr($sql, 0, -1) . ',' . $this->getForeignKeySqlDefinition($foreignKey) . ')'; $this->execute($sql); $sql = sprintf('INSERT INTO %s(%s) SELECT %s FROM %s', $table->getName(), implode(', ', $columns), implode(', ', $columns), $tmpTableName); $this->execute($sql); $this->execute(sprintf('DROP TABLE %s', $this->quoteTableName($tmpTableName))); $this->endCommandTimer(); }
/** * Commits the table changes. * * If the table does not exist it is created otherwise it is updated. * * @return mixed */ public function save() { $this->table->save(); return $this; }
public function testAddTableWithForeignKey() { $this->mock->expects($this->any())->method('isValidColumnType')->with($this->callback(function ($column) { return in_array($column->getType(), array('string', 'integer')); }))->will($this->returnValue(true)); $table = new Table('table', array(), $this->adapter); $table->addColumn('bar', 'string')->addColumn('relation', 'integer')->addForeignKey('relation', 'target_table', array('id')); $this->mock->expects($this->once())->method('createTable')->with($this->callback(function ($table) { if ($table->getName() !== 'pre_table_suf') { throw new \Exception(sprintf('Table::getName was not prefixed/suffixed properly: "%s"', $table->getName())); } $fks = $table->getForeignKeys(); if (count($fks) !== 1) { throw new \Exception(sprintf('Table::getForeignKeys count was incorrect: %d', count($fks))); } foreach ($fks as $fk) { if ($fk->getReferencedTable()->getName() !== 'pre_target_table_suf') { throw new \Exception(sprintf('ForeignKey::getReferencedTable was not prefixed/suffixed properly: "%s"', $fk->getReferencedTable->getName())); } } return true; })); $table->create(); }
/** * {@inheritdoc} * * After a table update, the TableRegistry should be cleared in order to prevent issues with * table schema stored in Table objects having columns that might have been renamed or removed during * the update process. */ public function update() { parent::update(); TableRegistry::clear(); }
public function testTrigger() { $this->origin->expects($this->once())->method('getName')->will($this->returnValue('avatars')); $this->assertEquals('lhmt_test_avatars', $this->entangler->trigger('test')); }
/** * {@inheritdoc} */ public function addForeignKey(Table $table, ForeignKey $foreignKey) { $this->startCommandTimer(); $this->writeCommand('addForeignKey', array($table->getName(), $foreignKey->getColumns())); $this->execute(sprintf('ALTER TABLE %s ADD %s', $this->quoteTableName($table->getName()), $this->getForeignKeySqlDefinition($foreignKey, $table->getName()))); $this->endCommandTimer(); }
/** * {@inheritdoc} */ public function createSchemaTable() { try { $options = array('id' => false); $table = new \Phinx\Db\Table($this->getSchemaTableName(), $options, $this); $table->addColumn('version', 'biginteger', array('limit' => 14))->addColumn('start_time', 'timestamp')->addColumn('end_time', 'timestamp')->save(); } catch (\Exception $exception) { throw new \InvalidArgumentException('There was a problem creating the schema table'); } }
/** * {@inheritdoc} */ public function createTable(Table $table) { $this->recordCommand('createTable', array($table->getName())); }