Code Duplication    Length = 37-38 lines in 2 locations

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

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

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

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