Skip to content

Commit ea0826c

Browse files
committed
Implement DROP CONSTRAINT for CHECK constraints
1 parent b2377fa commit ea0826c

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

tests/WP_SQLite_Driver_Tests.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9183,4 +9183,24 @@ public function testAlterTableAddCheckConstraint(): void {
91839183
$this->expectExceptionMessage( 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: c' );
91849184
$this->assertQuery( 'INSERT INTO t (id) VALUES (0)' );
91859185
}
9186+
9187+
public function testAlterTableDropCheckConstraint(): void {
9188+
$this->assertQuery( 'CREATE TABLE t (id INT, CONSTRAINT c CHECK (id > 0))' );
9189+
$this->assertQuery( 'ALTER TABLE t DROP CONSTRAINT c' );
9190+
9191+
// SHOW CREATE TABLE
9192+
$this->assertQuery( 'SHOW CREATE TABLE t' );
9193+
$result = $this->engine->get_query_results();
9194+
$this->assertEquals(
9195+
implode(
9196+
"\n",
9197+
array(
9198+
'CREATE TABLE `t` (',
9199+
' `id` int DEFAULT NULL',
9200+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
9201+
)
9202+
),
9203+
$result[0]->{'Create Table'}
9204+
);
9205+
}
91869206
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,6 +1301,8 @@ private function record_drop_constraint(
13011301
$this->record_drop_key( $table_is_temporary, $table_name, $name );
13021302
} elseif ( 'FOREIGN KEY' === $constraint_type ) {
13031303
$this->record_drop_foreign_key( $table_is_temporary, $table_name, $name );
1304+
} elseif ( 'CHECK' === $constraint_type ) {
1305+
$this->record_drop_check_constraint( $table_is_temporary, $table_name, $name );
13041306
} else {
13051307
throw new \Exception(
13061308
"DROP CONSTRAINT for constraint type '$constraint_type' is not supported."
@@ -1398,6 +1400,37 @@ private function record_drop_foreign_key(
13981400
);
13991401
}
14001402

1403+
/**
1404+
* Analyze DROP CHECK statement and record data in the information schema.
1405+
*
1406+
* @param bool $table_is_temporary Whether the table is temporary.
1407+
* @param string $table_name The table name.
1408+
* @param string $name The check constraint name.
1409+
*/
1410+
private function record_drop_check_constraint(
1411+
bool $table_is_temporary,
1412+
string $table_name,
1413+
string $name
1414+
): void {
1415+
$this->delete_values(
1416+
$this->get_table_name( $table_is_temporary, 'table_constraints' ),
1417+
array(
1418+
'CONSTRAINT_SCHEMA' => $this->db_name,
1419+
'TABLE_NAME' => $table_name,
1420+
'CONSTRAINT_TYPE' => 'CHECK',
1421+
'CONSTRAINT_NAME' => $name,
1422+
)
1423+
);
1424+
1425+
$this->delete_values(
1426+
$this->get_table_name( $table_is_temporary, 'check_constraints' ),
1427+
array(
1428+
'CONSTRAINT_SCHEMA' => $this->db_name,
1429+
'CONSTRAINT_NAME' => $name,
1430+
)
1431+
);
1432+
}
1433+
14011434
/**
14021435
* Analyze "columnDefinition" or "fieldDefinition" AST node and extract column data.
14031436
*

0 commit comments

Comments
 (0)