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 compatibility for use uniq index #38

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
45 changes: 41 additions & 4 deletions Dbal/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,6 +24,12 @@ final class Schema extends BaseSchema
{
protected $options;

/*
* Uniq type stockage
*/

protected $columnUniqList = array();

/**
* Constructor.
*
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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'));
}

/**
Expand All @@ -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'));
Expand All @@ -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'));
Expand Down Expand Up @@ -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'));
}
}