Code Duplication    Length = 23-38 lines in 4 locations

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

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

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

@@ 683-705 (lines=23) @@
680
    /**
681
     * {@inheritdoc}
682
     */
683
    public function dropIndex($tableName, $columns)
684
    {
685
        if (is_string($columns)) {
686
            $columns = [$columns]; // str to array
687
        }
688
689
        $indexes = $this->getIndexes($tableName);
690
        $columns = array_map('strtolower', $columns);
691
692
        foreach ($indexes as $indexName => $index) {
693
            $a = array_diff($columns, $index['columns']);
694
            if (empty($a)) {
695
                $this->execute(
696
                    sprintf(
697
                        'DROP INDEX %s ON %s',
698
                        $this->quoteColumnName($indexName),
699
                        $this->quoteTableName($tableName)
700
                    )
701
                );
702
703
                return;
704
            }
705
        }
706
    }
707
708
    /**
@@ 807-844 (lines=38) @@
804
    /**
805
     * {@inheritdoc}
806
     */
807
    public function dropForeignKey($tableName, $columns, $constraint = null)
808
    {
809
        if (is_string($columns)) {
810
            $columns = [$columns]; // str to array
811
        }
812
813
        if ($constraint) {
814
            $this->execute(
815
                sprintf(
816
                    'ALTER TABLE %s DROP CONSTRAINT %s',
817
                    $this->quoteTableName($tableName),
818
                    $constraint
819
                )
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
    /**

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

@@ 712-745 (lines=34) @@
709
    /**
710
     * {@inheritdoc}
711
     */
712
    public function dropForeignKey($tableName, $columns, $constraint = null)
713
    {
714
        if (is_string($columns)) {
715
            $columns = [$columns]; // str to array
716
        }
717
718
        if ($constraint) {
719
            $this->execute(
720
                sprintf(
721
                    'ALTER TABLE %s DROP CONSTRAINT %s',
722
                    $this->quoteTableName($tableName),
723
                    $constraint
724
                )
725
            );
726
        } else {
727
            foreach ($columns as $column) {
728
                $rows = $this->fetchAll(sprintf(
729
                    "SELECT CONSTRAINT_NAME
730
                      FROM information_schema.KEY_COLUMN_USAGE
731
                      WHERE TABLE_SCHEMA = CURRENT_SCHEMA()
732
                        AND TABLE_NAME IS NOT NULL
733
                        AND TABLE_NAME = '%s'
734
                        AND COLUMN_NAME = '%s'
735
                      ORDER BY POSITION_IN_UNIQUE_CONSTRAINT",
736
                    $tableName,
737
                    $column
738
                ));
739
740
                foreach ($rows as $row) {
741
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
742
                }
743
            }
744
        }
745
    }
746
747
    /**
748
     * {@inheritdoc}