Code Duplication    Length = 23-38 lines in 4 locations

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

@@ 677-712 (lines=36) @@
674
    /**
675
     * {@inheritdoc}
676
     */
677
    public function dropForeignKey($tableName, $columns, $constraint = null)
678
    {
679
        if (is_string($columns)) {
680
            $columns = [$columns]; // str to array
681
        }
682
683
        if ($constraint) {
684
            $this->execute(
685
                sprintf(
686
                    'ALTER TABLE %s DROP FOREIGN KEY %s',
687
                    $this->quoteTableName($tableName),
688
                    $constraint
689
                )
690
            );
691
692
            return;
693
        } else {
694
            foreach ($columns as $column) {
695
                $rows = $this->fetchAll(sprintf(
696
                    "SELECT
697
                        CONSTRAINT_NAME
698
                      FROM information_schema.KEY_COLUMN_USAGE
699
                      WHERE REFERENCED_TABLE_SCHEMA = DATABASE()
700
                        AND REFERENCED_TABLE_NAME IS NOT NULL
701
                        AND TABLE_NAME = '%s'
702
                        AND COLUMN_NAME = '%s'
703
                      ORDER BY POSITION_IN_UNIQUE_CONSTRAINT",
704
                    $tableName,
705
                    $column
706
                ));
707
                foreach ($rows as $row) {
708
                    $this->dropForeignKey($tableName, $columns, $row['CONSTRAINT_NAME']);
709
                }
710
            }
711
        }
712
    }
713
714
    /**
715
     * {@inheritdoc}

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

@@ 685-718 (lines=34) @@
682
    /**
683
     * {@inheritdoc}
684
     */
685
    public function dropForeignKey($tableName, $columns, $constraint = null)
686
    {
687
        if (is_string($columns)) {
688
            $columns = [$columns]; // str to array
689
        }
690
691
        if ($constraint) {
692
            $this->execute(
693
                sprintf(
694
                    'ALTER TABLE %s DROP CONSTRAINT %s',
695
                    $this->quoteTableName($tableName),
696
                    $constraint
697
                )
698
            );
699
        } else {
700
            foreach ($columns as $column) {
701
                $rows = $this->fetchAll(sprintf(
702
                    "SELECT CONSTRAINT_NAME
703
                      FROM information_schema.KEY_COLUMN_USAGE
704
                      WHERE TABLE_SCHEMA = CURRENT_SCHEMA()
705
                        AND TABLE_NAME IS NOT NULL
706
                        AND TABLE_NAME = '%s'
707
                        AND COLUMN_NAME = '%s'
708
                      ORDER BY POSITION_IN_UNIQUE_CONSTRAINT",
709
                    $tableName,
710
                    $column
711
                ));
712
713
                foreach ($rows as $row) {
714
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
715
                }
716
            }
717
        }
718
    }
719
720
    /**
721
     * {@inheritdoc}

src/Phinx/Db/Adapter/SqlServerAdapter.php 2 locations

@@ 692-714 (lines=23) @@
689
    /**
690
     * {@inheritdoc}
691
     */
692
    public function dropIndex($tableName, $columns)
693
    {
694
        if (is_string($columns)) {
695
            $columns = [$columns]; // str to array
696
        }
697
698
        $indexes = $this->getIndexes($tableName);
699
        $columns = array_map('strtolower', $columns);
700
701
        foreach ($indexes as $indexName => $index) {
702
            $a = array_diff($columns, $index['columns']);
703
            if (empty($a)) {
704
                $this->execute(
705
                    sprintf(
706
                        'DROP INDEX %s ON %s',
707
                        $this->quoteColumnName($indexName),
708
                        $this->quoteTableName($tableName)
709
                    )
710
                );
711
712
                return;
713
            }
714
        }
715
    }
716
717
    /**
@@ 816-853 (lines=38) @@
813
    /**
814
     * {@inheritdoc}
815
     */
816
    public function dropForeignKey($tableName, $columns, $constraint = null)
817
    {
818
        if (is_string($columns)) {
819
            $columns = [$columns]; // str to array
820
        }
821
822
        if ($constraint) {
823
            $this->execute(
824
                sprintf(
825
                    'ALTER TABLE %s DROP CONSTRAINT %s',
826
                    $this->quoteTableName($tableName),
827
                    $constraint
828
                )
829
            );
830
831
            return;
832
        } else {
833
            foreach ($columns as $column) {
834
                $rows = $this->fetchAll(sprintf(
835
                    "SELECT
836
                    tc.constraint_name,
837
                    tc.table_name, kcu.column_name,
838
                    ccu.table_name AS referenced_table_name,
839
                    ccu.column_name AS referenced_column_name
840
                FROM
841
                    information_schema.table_constraints AS tc
842
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
843
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
844
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s' and ccu.column_name='%s'
845
                ORDER BY kcu.ordinal_position",
846
                    $tableName,
847
                    $column
848
                ));
849
                foreach ($rows as $row) {
850
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
851
                }
852
            }
853
        }
854
    }
855
856
    /**