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

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