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

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