Skip to content

Commit 0025f57

Browse files
committed
Record ALTER TABLE DROP COLUMN in information schema
1 parent 2dfcf9f commit 0025f57

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-builder.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,16 @@ public function record_alter_table( WP_Parser_Node $node ): void {
484484
);
485485
continue;
486486
}
487+
488+
// DROP
489+
if ( WP_MySQL_Lexer::DROP_SYMBOL === $first_token->id ) {
490+
// DROP [COLUMN]
491+
$column_ref = $action->get_child_node( 'columnInternalRef' );
492+
if ( null !== $column_ref ) {
493+
$name = $this->get_value( $column_ref );
494+
$this->record_drop_column( $table_name, $name );
495+
}
496+
}
487497
}
488498
}
489499

@@ -554,6 +564,41 @@ private function record_modify_column(
554564
$this->record_change_column( $table_name, $column_name, $column_name, $node );
555565
}
556566

567+
private function record_drop_column( $table_name, $column_name ): void {
568+
$this->delete_values(
569+
'_mysql_information_schema_columns',
570+
array(
571+
'table_name' => $table_name,
572+
'column_name' => $column_name,
573+
)
574+
);
575+
576+
/**
577+
* From MySQL documentation:
578+
*
579+
* If columns are dropped from a table, the columns are also removed
580+
* from any index of which they are a part. If all columns that make up
581+
* an index are dropped, the index is dropped as well.
582+
*
583+
* This means we need to remove the records from the STATISTICS table,
584+
* renumber the SEQ_IN_INDEX values, and resync the column key info.
585+
*
586+
* See:
587+
* - https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
588+
*/
589+
$this->delete_values(
590+
'_mysql_information_schema_statistics',
591+
array(
592+
'table_name' => $table_name,
593+
'column_name' => $column_name,
594+
)
595+
);
596+
597+
// @TODO: Renumber SEQ_IN_INDEX values.
598+
599+
$this->sync_column_key_info( $table_name );
600+
}
601+
557602
private function record_add_constraint( string $table_name, WP_Parser_Node $node ): void {
558603
// Get first constraint keyword.
559604
$children = $node->get_children();
@@ -1416,6 +1461,21 @@ private function update_values( string $table_name, array $data, array $where ):
14161461
);
14171462
}
14181463

1464+
private function delete_values( string $table_name, array $where ): void {
1465+
$where_clause = array();
1466+
foreach ( $where as $column => $value ) {
1467+
$where_clause[] = $column . ' = ?';
1468+
}
1469+
1470+
$this->query(
1471+
'
1472+
DELETE FROM ' . $table_name . '
1473+
WHERE ' . implode( ' AND ', $where_clause ) . '
1474+
',
1475+
array_values( $where )
1476+
);
1477+
}
1478+
14191479
/**
14201480
* @param string $query
14211481
* @param array $params

0 commit comments

Comments
 (0)