Code Duplication    Length = 23-38 lines in 4 locations

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
708
                return;
709
            }
710
        }
711
    }
712
713
    /**
@@ 812-849 (lines=38) @@
809
    /**
810
     * {@inheritdoc}
811
     */
812
    public function dropForeignKey($tableName, $columns, $constraint = null)
813
    {
814
        if (is_string($columns)) {
815
            $columns = [$columns]; // str to array
816
        }
817
818
        if ($constraint) {
819
            $this->execute(
820
                sprintf(
821
                    'ALTER TABLE %s DROP CONSTRAINT %s',
822
                    $this->quoteTableName($tableName),
823
                    $constraint
824
                )
825
            );
826
827
            return;
828
        } else {
829
            foreach ($columns as $column) {
830
                $rows = $this->fetchAll(sprintf(
831
                    "SELECT
832
                    tc.constraint_name,
833
                    tc.table_name, kcu.column_name,
834
                    ccu.table_name AS referenced_table_name,
835
                    ccu.column_name AS referenced_column_name
836
                FROM
837
                    information_schema.table_constraints AS tc
838
                    JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
839
                    JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
840
                WHERE constraint_type = 'FOREIGN KEY' AND tc.table_name = '%s' and ccu.column_name='%s'
841
                ORDER BY kcu.ordinal_position",
842
                    $tableName,
843
                    $column
844
                ));
845
                foreach ($rows as $row) {
846
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
847
                }
848
            }
849
        }
850
    }
851
852
    /**

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}