Skip to content

Commit

Permalink
Record ALTER TABLE DROP COLUMN in information schema
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Dec 19, 2024
1 parent 2dfcf9f commit 0025f57
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ public function record_alter_table( WP_Parser_Node $node ): void {
);
continue;
}

// DROP
if ( WP_MySQL_Lexer::DROP_SYMBOL === $first_token->id ) {
// DROP [COLUMN]
$column_ref = $action->get_child_node( 'columnInternalRef' );
if ( null !== $column_ref ) {
$name = $this->get_value( $column_ref );
$this->record_drop_column( $table_name, $name );
}
}
}
}

Expand Down Expand Up @@ -554,6 +564,41 @@ private function record_modify_column(
$this->record_change_column( $table_name, $column_name, $column_name, $node );
}

private function record_drop_column( $table_name, $column_name ): void {
$this->delete_values(
'_mysql_information_schema_columns',
array(
'table_name' => $table_name,
'column_name' => $column_name,
)
);

/**
* From MySQL documentation:
*
* If columns are dropped from a table, the columns are also removed
* from any index of which they are a part. If all columns that make up
* an index are dropped, the index is dropped as well.
*
* This means we need to remove the records from the STATISTICS table,
* renumber the SEQ_IN_INDEX values, and resync the column key info.
*
* See:
* - https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
*/
$this->delete_values(
'_mysql_information_schema_statistics',
array(
'table_name' => $table_name,
'column_name' => $column_name,
)
);

// @TODO: Renumber SEQ_IN_INDEX values.

$this->sync_column_key_info( $table_name );
}

private function record_add_constraint( string $table_name, WP_Parser_Node $node ): void {
// Get first constraint keyword.
$children = $node->get_children();
Expand Down Expand Up @@ -1416,6 +1461,21 @@ private function update_values( string $table_name, array $data, array $where ):
);
}

private function delete_values( string $table_name, array $where ): void {
$where_clause = array();
foreach ( $where as $column => $value ) {
$where_clause[] = $column . ' = ?';
}

$this->query(
'
DELETE FROM ' . $table_name . '
WHERE ' . implode( ' AND ', $where_clause ) . '
',
array_values( $where )
);
}

/**
* @param string $query
* @param array $params
Expand Down

0 comments on commit 0025f57

Please sign in to comment.