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