From eb223005c36efae86cc506c57c1e9bce560edd7a Mon Sep 17 00:00:00 2001 From: caleb Date: Sat, 5 May 2018 13:33:26 -0400 Subject: [PATCH] mysql compatibility for use uniq index --- Dbal/Schema.php | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/Dbal/Schema.php b/Dbal/Schema.php index ed9327c..106ecc1 100644 --- a/Dbal/Schema.php +++ b/Dbal/Schema.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Security\Acl\Dbal; +use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Schema as BaseSchema; use Doctrine\DBAL\Connection; @@ -23,6 +24,12 @@ final class Schema extends BaseSchema { protected $options; + /* + * Uniq type stockage + */ + + protected $columnUniqList = array(); + /** * Constructor. * @@ -60,6 +67,36 @@ public function addToSchema(BaseSchema $schema) } } + public function toSql(AbstractPlatform $platform) + { + $sqlAllQuery = parent::toSql($platform); + foreach ($this->columnUniqList as $column){ + $sql = 'ALTER TABLE `'.$column['table_name'].'` ADD UNIQUE('.join(', ',$column['column']).')'; + array_push($sqlAllQuery, $sql); + } + return $sqlAllQuery; + } + + private function addUniqueIndex($table, array $columnNames, $indexName = null, $flags = [], array $options = []) + { + $table->addIndex($columnNames, $indexName, $flags, $options); + $columnList = []; + foreach ($columnNames as $col){ + $colType = $table->getColumn($col)->getType()->getName(); + $colVal = $col; + if($colType == 'string'){ + $len = $table->getColumn($col)->getLength(); + $len = ($len > 180)? 180: $len; + $colVal = $col.' ('.$len.')'; + } + array_push($columnList, $colVal); + } + array_push($this->columnUniqList, [ + 'table_name' => $table->getName(), + 'column' => $columnList + ]); + } + /** * Adds the class table to the schema. */ @@ -69,7 +106,7 @@ protected function addClassTable() $table->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => 'auto')); $table->addColumn('class_type', 'string', array('length' => 200)); $table->setPrimaryKey(array('id')); - $table->addUniqueIndex(array('class_type')); + $this->addUniqueIndex($table, array('class_type')); } /** @@ -92,7 +129,7 @@ protected function addEntryTable() $table->addColumn('audit_failure', 'boolean'); $table->setPrimaryKey(array('id')); - $table->addUniqueIndex(array('class_id', 'object_identity_id', 'field_name', 'ace_order')); + $this->addUniqueIndex($table, array('class_id', 'object_identity_id', 'field_name', 'ace_order')); $table->addIndex(array('class_id', 'object_identity_id', 'security_identity_id')); $table->addForeignKeyConstraint($this->getTable($this->options['class_table_name']), array('class_id'), array('id'), array('onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE')); @@ -114,7 +151,7 @@ protected function addObjectIdentitiesTable() $table->addColumn('entries_inheriting', 'boolean'); $table->setPrimaryKey(array('id')); - $table->addUniqueIndex(array('object_identifier', 'class_id')); + $this->addUniqueIndex($table, array('object_identifier', 'class_id')); $table->addIndex(array('parent_object_identity_id')); $table->addForeignKeyConstraint($table, array('parent_object_identity_id'), array('id')); @@ -149,6 +186,6 @@ protected function addSecurityIdentitiesTable() $table->addColumn('username', 'boolean'); $table->setPrimaryKey(array('id')); - $table->addUniqueIndex(array('identifier', 'username')); + $this->addUniqueIndex($table, array('identifier', 'username')); } }