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

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