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 #885

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/migrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ Option Description
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 foreign key definitions:

====== ===========
Expand Down
2 changes: 2 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,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' : '';
Expand Down
80 changes: 80 additions & 0 deletions src/Phinx/Db/Table/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*/
namespace Phinx\Db\Table;

use Phinx\Db\Adapter\AdapterInterface;

/**
*
* This object is based loosely on: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/Table.html.
Expand Down Expand Up @@ -104,6 +106,16 @@ class Column
*/
protected $properties = array();

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

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

/**
* @var array
*/
Expand Down Expand Up @@ -485,6 +497,72 @@ public function getValues()
return $this->values;
}

/**
* 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 collation.
*
* @return string
*/
public function getCollation()
{
return $this->collation;
}

/**
* 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;
}

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

/**
* Gets all allowed options. Each option must have a corresponding `setFoo` method.
*
Expand All @@ -506,6 +584,8 @@ protected function getValidOptions()
'timezone',
'properties',
'values',
'collation',
'encoding',
);
}

Expand Down
15 changes: 14 additions & 1 deletion tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,20 @@ public function testAddStringColumnWithSignedEqualsFalse()
$this->assertEquals('varchar(255)', $rows[1]['Type']);
}

public function testRenameColumn()
public function testAddStringColumnWithCustomCollation()
{
$table = new \Phinx\Db\Table('table_custom_collation', 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 table_custom_collation');
$this->assertEquals('utf8_general_ci', $rows[1]['Collation']);
$this->assertEquals('utf8mb4_unicode_ci', $rows[2]['Collation']);
}

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