Skip to content

Instantly share code, notes, and snippets.

@tompedals
Created December 5, 2014 22:36
Show Gist options
  • Save tompedals/67bbe158fe846c42ae46 to your computer and use it in GitHub Desktop.
Save tompedals/67bbe158fe846c42ae46 to your computer and use it in GitHub Desktop.
Migration to convert database, table and column charset to utf8mb4 (for Emoji support)
<?php
namespace HeyUpdate\Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
class Version20141205212145 extends AbstractMigration
{
public function up(Schema $schema)
{
$platform = $this->connection->getDatabasePlatform();
$this->addSql(sprintf('ALTER DATABASE %s CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci', $this->connection->getDatabase()));
foreach ($schema->getTables() as $table) {
if ($table->getName() === 'migration_versions') {
// Ignore the migrations table
continue;
}
$this->addSql(sprintf('ALTER TABLE %s CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci', $table->getName()));
foreach ($table->getColumns() as $column) {
if (in_array($column->getType()->getName(), array('string', 'text'))) {
$this->addSql(sprintf('ALTER TABLE %s CHANGE %s %s %s CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci',
$table->getName(),
$column->getName(),
$column->getName(),
$column->getType()->getSQLDeclaration(array(
'length' => $column->getLength()
), $platform)
));
}
}
$this->addSql(sprintf('REPAIR TABLE %s', $table->getName()));
$this->addSql(sprintf('OPTIMIZE TABLE %s', $table->getName()));
}
}
public function down(Schema $schema)
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment