Code Duplication    Length = 23-38 lines in 4 locations

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
    /**

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

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

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

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