Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MySQL] Custom collation for just one column in table #662

Closed
wants to merge 9 commits into from
9 changes: 9 additions & 0 deletions docs/migrations.rst
Original file line number Diff line number Diff line change
@@ -932,6 +932,15 @@ scale combine with ``precision`` to set decimial accuracy
signed enable or disable the ``unsigned`` option *(only applies to MySQL)*
========= ===========

For ``string`` and ``text`` columns:

========= ===========
Option Description
========= ===========
collation set collation that differs from table defaults
encoding set character set that differs from table defaults
========= ===========

For ``enum`` and ``set`` columns:

========= ===========
2 changes: 2 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
@@ -1007,6 +1007,8 @@ protected function getColumnSqlDefinition(Column $column)
if (($values = $column->getValues()) && is_array($values)) {
$def .= "('" . implode("', '", $values) . "')";
}
$def .= $column->getEncoding() ? ' CHARACTER SET ' . $column->getEncoding() : '';
$def .= $column->getCollation() ? ' COLLATE ' . $column->getCollation() : '';
$def .= (!$column->isSigned() && isset($this->signedColumnTypes[$column->getType()])) ? ' unsigned' : '' ;
$def .= ($column->isNull() == false) ? ' NOT NULL' : ' NULL';
$def .= ($column->isIdentity()) ? ' AUTO_INCREMENT' : '';
79 changes: 79 additions & 0 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@
* @subpackage Phinx\Db
*/
namespace Phinx\Db\Table;
use Phinx\Db\Adapter\AdapterInterface;

/**
*
@@ -89,6 +90,82 @@ class Column
*/
protected $comment;

/**
* @var string
*/
protected $collation;

/**
* @var string
*/
protected $encoding;

/**
* Gets the column collation.
*
* @return string
*/
public function getCollation()
{
return $this->collation;
}

/**
* Sets the column collation.
*
* @param string $collation
*
* @throws \UnexpectedValueException If collation not allowed for type
* @return $this
*/
public function setCollation($collation)
{
$allowedTypes = array(
AdapterInterface::PHINX_TYPE_CHAR,
AdapterInterface::PHINX_TYPE_STRING,
AdapterInterface::PHINX_TYPE_TEXT,
);
if (!in_array($this->getType(), $allowedTypes))
throw new \UnexpectedValueException("Collation may be set only for types: ". implode(', ', $allowedTypes));

$this->collation = $collation;

return $this;
}

/**
* Gets the column character set.
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}

/**
* Sets the column character set.
*
* @param string $encoding
*
* @throws \UnexpectedValueException If character set not allowed for type
* @return $this
*/
public function setEncoding($encoding)
{
$allowedTypes = array(
AdapterInterface::PHINX_TYPE_CHAR,
AdapterInterface::PHINX_TYPE_STRING,
AdapterInterface::PHINX_TYPE_TEXT,
);
if (!in_array($this->getType(), $allowedTypes))
throw new \UnexpectedValueException("Character set may be set only for types: ". implode(', ', $allowedTypes));

$this->encoding = $encoding;

return $this;
}

/**
* @var boolean
*/
@@ -506,6 +583,8 @@ protected function getValidOptions()
'timezone',
'properties',
'values',
'collation',
'encoding',
);
}

13 changes: 13 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
@@ -402,6 +402,19 @@ public function testAddStringColumnWithSignedEqualsFalse()
$this->assertEquals('varchar(255)', $rows[1]['Type']);
}

public function testAddStringColumnWithCustonCollation()
{
$table = new \Phinx\Db\Table('table1', array('collation' => 'utf8_general_ci'), $this->adapter);
$table->save();
$this->assertFalse($table->hasColumn('string_collation_default'));
$this->assertFalse($table->hasColumn('string_collation_custom'));
$table->addColumn('string_collation_default', 'string', array())->save();
$table->addColumn('string_collation_custom', 'string', array('collation' => 'utf8mb4_unicode_ci'))->save();
$rows = $this->adapter->fetchAll('SHOW FULL COLUMNS FROM table1');
$this->assertEquals('utf8_general_ci', $rows[2]['Collation']);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just can't run these tests on my environment. Please, check them and fix is there are some issues with this test.

$this->assertEquals('utf8mb4_unicode_ci', $rows[3]['Collation']);
}

public function testRenameColumn()
{
$table = new \Phinx\Db\Table('t', array(), $this->adapter);