| @@ 275-311 (lines=37) @@ | ||
| 272 | /** |
|
| 273 | * {@inheritdoc} |
|
| 274 | */ |
|
| 275 | protected function getChangePrimaryKeyInstructions(Table $table, $newColumns) |
|
| 276 | { |
|
| 277 | $instructions = new AlterInstructions(); |
|
| 278 | ||
| 279 | // Drop the existing primary key |
|
| 280 | $primaryKey = $this->getPrimaryKey($table->getName()); |
|
| 281 | if (!empty($primaryKey['constraint'])) { |
|
| 282 | $sql = sprintf( |
|
| 283 | 'DROP CONSTRAINT %s', |
|
| 284 | $this->quoteColumnName($primaryKey['constraint']) |
|
| 285 | ); |
|
| 286 | $instructions->addAlter($sql); |
|
| 287 | } |
|
| 288 | ||
| 289 | // Add the primary key(s) |
|
| 290 | if (!empty($newColumns)) { |
|
| 291 | $sql = sprintf( |
|
| 292 | 'ALTER TABLE %s ADD CONSTRAINT %s PRIMARY KEY (', |
|
| 293 | $this->quoteTableName($table->getName()), |
|
| 294 | $this->quoteColumnName('PK_' . $table->getName()) |
|
| 295 | ); |
|
| 296 | if (is_string($newColumns)) { // handle primary_key => 'id' |
|
| 297 | $sql .= $this->quoteColumnName($newColumns); |
|
| 298 | } elseif (is_array($newColumns)) { // handle primary_key => array('tag_id', 'resource_id') |
|
| 299 | $sql .= implode(',', array_map([$this, 'quoteColumnName'], $newColumns)); |
|
| 300 | } else { |
|
| 301 | throw new \InvalidArgumentException(sprintf( |
|
| 302 | "Invalid value for primary key: %s", |
|
| 303 | json_encode($newColumns) |
|
| 304 | )); |
|
| 305 | } |
|
| 306 | $sql .= ')'; |
|
| 307 | $instructions->addPostStep($sql); |
|
| 308 | } |
|
| 309 | ||
| 310 | return $instructions; |
|
| 311 | } |
|
| 312 | ||
| 313 | /** |
|
| 314 | * {@inheritdoc} |
|
| @@ 270-307 (lines=38) @@ | ||
| 267 | /** |
|
| 268 | * {@inheritdoc} |
|
| 269 | */ |
|
| 270 | protected function getChangePrimaryKeyInstructions(Table $table, $newColumns) |
|
| 271 | { |
|
| 272 | $parts = $this->getSchemaName($table->getName()); |
|
| 273 | ||
| 274 | $instructions = new AlterInstructions(); |
|
| 275 | ||
| 276 | // Drop the existing primary key |
|
| 277 | $primaryKey = $this->getPrimaryKey($table->getName()); |
|
| 278 | if (!empty($primaryKey['constraint'])) { |
|
| 279 | $sql = sprintf( |
|
| 280 | 'DROP CONSTRAINT %s', |
|
| 281 | $this->quoteColumnName($primaryKey['constraint']) |
|
| 282 | ); |
|
| 283 | $instructions->addAlter($sql); |
|
| 284 | } |
|
| 285 | ||
| 286 | // Add the new primary key |
|
| 287 | if (!empty($newColumns)) { |
|
| 288 | $sql = sprintf( |
|
| 289 | 'ADD CONSTRAINT %s PRIMARY KEY (', |
|
| 290 | $this->quoteColumnName($parts['table'] . '_pkey') |
|
| 291 | ); |
|
| 292 | if (is_string($newColumns)) { // handle primary_key => 'id' |
|
| 293 | $sql .= $this->quoteColumnName($newColumns); |
|
| 294 | } elseif (is_array($newColumns)) { // handle primary_key => array('tag_id', 'resource_id') |
|
| 295 | $sql .= implode(',', array_map([$this, 'quoteColumnName'], $newColumns)); |
|
| 296 | } else { |
|
| 297 | throw new \InvalidArgumentException(sprintf( |
|
| 298 | "Invalid value for primary key: %s", |
|
| 299 | json_encode($newColumns) |
|
| 300 | )); |
|
| 301 | } |
|
| 302 | $sql .= ')'; |
|
| 303 | $instructions->addAlter($sql); |
|
| 304 | } |
|
| 305 | ||
| 306 | return $instructions; |
|
| 307 | } |
|
| 308 | ||
| 309 | /** |
|
| 310 | * {@inheritdoc} |
|