Code Duplication    Length = 23-38 lines in 4 locations

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

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

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

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

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

@@ 682-704 (lines=23) @@
679
    /**
680
     * {@inheritdoc}
681
     */
682
    public function dropIndex($tableName, $columns)
683
    {
684
        if (is_string($columns)) {
685
            $columns = [$columns]; // str to array
686
        }
687
688
        $indexes = $this->getIndexes($tableName);
689
        $columns = array_map('strtolower', $columns);
690
691
        foreach ($indexes as $indexName => $index) {
692
            $a = array_diff($columns, $index['columns']);
693
            if (empty($a)) {
694
                $this->execute(
695
                    sprintf(
696
                        'DROP INDEX %s ON %s',
697
                        $this->quoteColumnName($indexName),
698
                        $this->quoteTableName($tableName)
699
                    )
700
                );
701
702
                return;
703
            }
704
        }
705
    }
706
707
    /**
@@ 806-843 (lines=38) @@
803
    /**
804
     * {@inheritdoc}
805
     */
806
    public function dropForeignKey($tableName, $columns, $constraint = null)
807
    {
808
        if (is_string($columns)) {
809
            $columns = [$columns]; // str to array
810
        }
811
812
        if ($constraint) {
813
            $this->execute(
814
                sprintf(
815
                    'ALTER TABLE %s DROP CONSTRAINT %s',
816
                    $this->quoteTableName($tableName),
817
                    $constraint
818
                )
819
            );
820
821
            return;
822
        } else {
823
            foreach ($columns as $column) {
824
                $rows = $this->fetchAll(sprintf(
825
                    "SELECT
826
                    tc.constraint_name,
827
                    tc.table_name, kcu.column_name,
828
                    ccu.table_name AS referenced_table_name,
829
                    ccu.column_name AS referenced_column_name
830
                FROM
831
                    information_schema.table_constraints AS tc
832
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
833
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
834
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s' and ccu.column_name='%s'
835
                ORDER BY kcu.ordinal_position",
836
                    $tableName,
837
                    $column
838
                ));
839
                foreach ($rows as $row) {
840
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
841
                }
842
            }
843
        }
844
    }
845
846
    /**