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

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

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