Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implementing after support

  • Loading branch information...
commit 1c9276ab1410ec7ba8c4643b291df71caa9f31e8 1 parent bc219c7
@robmorgan authored
View
16 src/Phinx/Db/Adapter/MysqlAdapter.php
@@ -221,13 +221,17 @@ public function hasColumn($tableName, $columnName, $options = array())
*/
public function addColumn(Table $table, Column $column)
{
- return $this->execute(
- sprintf('ALTER TABLE %s ADD %s %s',
- $this->quoteTableName($table->getName()),
- $this->quoteColumnName($column->getName()),
- $this->getColumnSqlDefinition($column)
- )
+ $sql = sprintf('ALTER TABLE %s ADD %s %s',
+ $this->quoteTableName($table->getName()),
+ $this->quoteColumnName($column->getName()),
+ $this->getColumnSqlDefinition($column)
);
+
+ if ($column->getAfter()) {
+ $sql .= ' AFTER ' . $this->quoteColumnName($column->getAfter());
+ }
+
+ return $this->execute($sql);
}
/**
View
29 src/Phinx/Db/Table/Column.php
@@ -65,6 +65,11 @@ class Column
protected $identity = false;
/**
+ * @var string
+ */
+ protected $after;
+
+ /**
* Sets the column name.
*
* @param string $name
@@ -217,6 +222,28 @@ public function isIdentity()
}
/**
+ * Sets the name of the column to add this column after.
+ *
+ * @param string $after After
+ * @return Column
+ */
+ public function setAfter($after)
+ {
+ $this->after = $after;
+ return $this;
+ }
+
+ /**
+ * Returns the name of the column to add this column after.
+ *
+ * @return string
+ */
+ public function getAfter()
+ {
+ return $this->after;
+ }
+
+ /**
* Utility method that maps an array of column options to this objects methods.
*
* @param array $options Options
@@ -225,7 +252,7 @@ public function isIdentity()
public function setOptions($options)
{
// Valid Options
- $validOptions = array('limit', 'length', 'default', 'null', 'precision', 'scale');
+ $validOptions = array('limit', 'length', 'default', 'null', 'precision', 'scale', 'after');
foreach ($options as $option => $value) {
if (!in_array($option, $validOptions)) {
throw new \RuntimeException('\'' . $option . '\' is not a valid column option.');
View
4 tests/Phinx/Db/Adapter/MysqlAdapterTest.php
@@ -171,6 +171,10 @@ public function testAddColumn()
$table->addColumn('email', 'string')
->save();
$this->assertTrue($table->hasColumn('email'));
+ $table->addColumn('realname', 'string', array('after' => 'id'))
+ ->save();
+ $rows = $this->adapter->fetchAll('SHOW COLUMNS FROM table1');
+ $this->assertEquals('realname', $rows[1]['Field']);
}
public function testRenameColumn()
Please sign in to comment.
Something went wrong with that request. Please try again.