Code Duplication    Length = 23-38 lines in 7 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/PostgresAdapter.php 2 locations

@@ 679-712 (lines=34) @@
676
    /**
677
     * {@inheritdoc}
678
     */
679
    public function dropForeignKey($tableName, $columns, $constraint = null)
680
    {
681
        if (is_string($columns)) {
682
            $columns = [$columns]; // str to array
683
        }
684
685
        if ($constraint) {
686
            $this->execute(
687
                sprintf(
688
                    'ALTER TABLE %s DROP CONSTRAINT %s',
689
                    $this->quoteTableName($tableName),
690
                    $constraint
691
                )
692
            );
693
        } else {
694
            foreach ($columns as $column) {
695
                $rows = $this->fetchAll(sprintf(
696
                    "SELECT CONSTRAINT_NAME
697
                      FROM information_schema.KEY_COLUMN_USAGE
698
                      WHERE TABLE_SCHEMA = CURRENT_SCHEMA()
699
                        AND 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
707
                foreach ($rows as $row) {
708
                    $this->dropForeignKey($tableName, $columns, $row['constraint_name']);
709
                }
710
            }
711
        }
712
    }
713
714
    /**
715
     * {@inheritdoc}
@@ 567-589 (lines=23) @@
564
    /**
565
     * {@inheritdoc}
566
     */
567
    public function dropIndex($tableName, $columns)
568
    {
569
        if (is_string($columns)) {
570
            $columns = [$columns]; // str to array
571
        }
572
573
        $indexes = $this->getIndexes($tableName);
574
        $columns = array_map('strtolower', $columns);
575
576
        foreach ($indexes as $indexName => $index) {
577
            $a = array_diff($columns, $index['columns']);
578
            if (empty($a)) {
579
                $this->execute(
580
                    sprintf(
581
                        'DROP INDEX IF EXISTS %s',
582
                        $this->quoteColumnName($indexName)
583
                    )
584
                );
585
586
                return;
587
            }
588
        }
589
    }
590
591
    /**
592
     * {@inheritdoc}

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/OracleAdapter.php 2 locations

@@ 561-583 (lines=23) @@
558
    /**
559
     * {@inheritdoc}
560
     */
561
    public function dropIndex($tableName, $columns)
562
    {
563
        if (is_string($columns)) {
564
            $columns = [$columns]; // str to array
565
        }
566
567
        $indexes = $this->getIndexes($tableName);
568
        $columns = array_map('strtoupper', $columns);
569
570
        foreach ($indexes as $indexName => $index) {
571
            $a = array_diff($columns, $index['columns']);
572
            if (empty($a)) {
573
                $this->execute(
574
                    sprintf(
575
                        'DROP INDEX %s',
576
                        $this->quoteColumnName($indexName)
577
                    )
578
                );
579
580
                return;
581
            }
582
        }
583
    }
584
585
    /**
586
     * {@inheritdoc}
@@ 683-719 (lines=37) @@
680
    /**
681
     * {@inheritdoc}
682
     */
683
    public function dropForeignKey($tableName, $columns, $constraint = null)
684
    {
685
        if (is_string($columns)) {
686
            $columns = [$columns]; // str to array
687
        }
688
689
        if ($constraint) {
690
            $this->execute(
691
                sprintf(
692
                    'ALTER TABLE %s DROP CONSTRAINT %s',
693
                    $this->quoteTableName($tableName),
694
                    $constraint
695
                )
696
            );
697
698
            return;
699
        } else {
700
            foreach ($columns as $column) {
701
                $rows = $this->fetchAll(sprintf(
702
                    "SELECT a.CONSTRAINT_NAME, a.TABLE_NAME, b.COLUMN_NAME, 
703
                    (SELECT c.TABLE_NAME from ALL_CONS_COLUMNS c 
704
                    WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_table_name,
705
                    (SELECT c.COLUMN_NAME from ALL_CONS_COLUMNS c 
706
                    WHERE c.CONSTRAINT_NAME = a.R_CONSTRAINT_NAME) referenced_column_name
707
                    FROM all_constraints a JOIN ALL_CONS_COLUMNS b ON a.CONSTRAINT_NAME = b.CONSTRAINT_NAME
708
                    WHERE a.table_name = '%s'
709
                    AND CONSTRAINT_TYPE = 'R'
710
                    AND COLUMN_NAME = '%s'",
711
                    $tableName,
712
                    $column
713
                ));
714
                foreach ($rows as $row) {
715
                    $this->dropForeignKey($tableName, $columns, $row['CONSTRAINT_NAME']);
716
                }
717
            }
718
        }
719
    }
720
721
    /**
722
     * {@inheritdoc}