Make WordPress Core

Ticket #34877: 34877_patch_assert_primary_key.diff

File 34877_patch_assert_primary_key.diff, 1.0 KB (added by charlestonsw, 9 years ago)

Adds an assert for checking primary key and runs this on existing test.

  • dbdelta.php

     
    169169                );
    170170
    171171                $this->assertTableHasColumn( 'column_1', $wpdb->prefix . 'dbdelta_test' );
     172                $this->assertTableHasPrimaryKey( 'id' , $wpdb->prefix . 'dbdelta_test' );
    172173        }
    173174
    174175        /**
     
    289290        }
    290291
    291292        /**
     293         * Assert that a table has a primary key.
     294         *
     295         * Checks for single-column primary keys.  May not work for multi-column primary keys.
     296         *
     297         * @param string $column The column for the primary key.
     298         * @param string $table  The database table name.
     299         */
     300        protected function assertTableHasPrimaryKey( $column , $table ) {
     301                global $wpdb;
     302                $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};");
     303                $this->assertCount( 1, wp_list_filter( $tableindices, array( 'Key_name' => 'PRIMARY' , 'Column_name' => $column ) , 'AND' ) );
     304        }
     305
     306        /**
    292307         * Assert that a table doesn't have a column.
    293308         *
    294309         * @param string $column The field name.