Code Duplication    Length = 23-38 lines in 4 locations

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

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

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

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

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

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