Skip to content

Commit 5b0693a

Browse files
committed
Implement DROP CONSTRAINT for CHECK constraints
1 parent dc09446 commit 5b0693a

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

tests/WP_SQLite_Driver_Tests.php

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

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)