Code Duplication    Length = 37-38 lines in 2 locations

src/Phinx/Db/Adapter/SqlServerAdapter.php 1 location

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

src/Phinx/Db/Adapter/PostgresAdapter.php 1 location

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