}
}
- foreach ($this->indicesToAdd as $tableName => $indices) {
+ foreach ($this->indicesToDrop as $tableName => $indices) {
foreach ($indices as $index) {
$appliedAnyChange = true;
- $this->prepareIndexLog($tableName, $index);
- $this->addIndex($tableName, $index);
- $this->finalizeIndexLog($tableName, $index);
+ $this->dropIndex($tableName, $index);
+ $this->deleteIndexLog($tableName, $index);
}
}
- foreach ($this->indicesToDrop as $tableName => $indices) {
+ foreach ($this->indicesToAdd as $tableName => $indices) {
foreach ($indices as $index) {
$appliedAnyChange = true;
- $this->dropIndex($tableName, $index);
- $this->deleteIndexLog($tableName, $index);
+ $this->prepareIndexLog($tableName, $index);
+ $this->addIndex($tableName, $index);
+ $this->finalizeIndexLog($tableName, $index);
}
}
foreach ($table->getIndices() as $index) {
$matchingExistingIndex = null;
foreach ($existingIndices as $existingIndex) {
- if (empty(array_diff($index->getData(), $existingIndex->getData()))) {
+ if (!$this->diffIndices($existingIndex, $index)) {
$matchingExistingIndex = $existingIndex;
break;
}
$this->deleteIndexLog($tableName, $index);
}
}
- else if ($matchingExistingIndex === null) {
+ else if ($matchingExistingIndex !== null) {
+ // updating index type and index columns is supported with an
+ // explicit index name is given (automatically generated index
+ // names are not deterministic)
+ if (!$index->hasGeneratedName() && !empty(array_diff($matchingExistingIndex->getData(), $index->getData()))) {
+ if (!isset($this->indicesToDrop[$tableName])) {
+ $this->indicesToDrop[$tableName] = [];
+ }
+ $this->indicesToDrop[$tableName][] = $matchingExistingIndex;
+
+ if (!isset($this->indicesToAdd[$tableName])) {
+ $this->indicesToAdd[$tableName] = [];
+ }
+ $this->indicesToAdd[$tableName][] = $index;
+ }
+ }
+ else {
if (!isset($this->indicesToAdd[$tableName])) {
$this->indicesToAdd[$tableName] = [];
}
return $oldColumn->getDefaultValue() != $newColumn->getDefaultValue();
}
+ /**
+ * Returns `true` if the two indices differ.
+ *
+ * @param DatabaseTableIndex $oldIndex
+ * @param DatabaseTableIndex $newIndex
+ * @return bool
+ */
+ protected function diffIndices(DatabaseTableIndex $oldIndex, DatabaseTableIndex $newIndex) {
+ if ($newIndex->hasGeneratedName()) {
+ return !empty(array_diff($oldIndex->getData(), $newIndex->getData()));
+ }
+
+ return $oldIndex->getName() !== $newIndex->getName();
+ }
+
/**
* Drops the given foreign key.
*
*/
protected $columns;
+ /**
+ * is `true` if index name has been automatically generated
+ * @var bool
+ */
+ protected $generatedName = false;
+
/**
* name of index
* @var string
return $this;
}
+
+ /**
+ * Sets the automatically generated name of the index.
+ *
+ * @param string $name index name
+ * @return $this this index
+ */
+ public function generatedName($name) {
+ $this->name($name);
+ $this->generatedName = true;
+
+ return $this;
+ }
/**
* Returns the index columns.
return $this->type;
}
+ /**
+ * Returns `true` if the name of the index has been automatically generated.
+ *
+ * @return bool
+ */
+ public function hasGeneratedName() {
+ return $this->generatedName;
+ }
+
/**
* Sets the name of the index.
*