Skip to content

Instantly share code, notes, and snippets.

@eukras
Last active August 29, 2015 14:23
Show Gist options
  • Save eukras/35d13689e5909f77015d to your computer and use it in GitHub Desktop.
Save eukras/35d13689e5909f77015d to your computer and use it in GitHub Desktop.
Make doctrine indexes unique in .orm.yml files.
<?php
// When doctrine/orm 2.4 reverse-engineers a MySQL DB, it doesn't make the index names globally unique.
// This is a one-off hack script to fix up unique index names if theer are too many .orm.yml files to fix easily by hand.
// USAGE:
// for f in $(ls -1 ./src/Whatever/Bundle/WhateverBundle/Resources/config/doctrine/*.orm.yml); do \
// php fix-yml.php $f > "${f/doctrine/doctrine-fix}"; \
// done
$file = $argv[1];
if (file_exists($file)) {
$matching = false;
$table = '';
foreach (file($file) as $line) {
if (preg_match('/^ table: (.*)$/', $line, $matches) > 0) {
$table = $matches[1];
} elseif (preg_match('/^ indexes:/', $line) > 0) {
$matching = true;
} elseif (preg_match('/^ uniqueConstraints:/', $line) > 0) {
$matching = false;
} elseif (preg_match('/^ fields:/', $line) > 0) {
$matching = false;
}
if ($matching && preg_match('/^ ([^ ]*):/', $line, $matches) > 0) {
$index = $matches[1];
if (empty($table)) {
throw new Exception("TABLE NOT SET: $line");
}
echo " ${table}_${index}_index:\n";
} else {
echo $line;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment