From aa7e43346e71dcaf19eb1390f6222b93b136dc92 Mon Sep 17 00:00:00 2001 From: Aashish Sharma Date: Thu, 19 Nov 2026 18:51:43 +0530 Subject: [PATCH] Refactor inline CHECK constraint handling in schema builder and add corresponding tests --- ...s-wp-sqlite-information-schema-builder.php | 67 ++++- .../tests/WP_MySQL_On_SQLite_Tests.php | 280 ++++++++++++++++++ 2 files changed, 335 insertions(+), 12 deletions(-) diff --git a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php index f7a49423f..fa51d79fd 100644 --- a/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php +++ b/packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php @@ -611,10 +611,6 @@ public function record_create_table( WP_Parser_Node $node ): void { $table_name, $index_data['index_name'] ?? null ); - $check_constraint_data = $this->extract_check_constraint_data( - $column_node, - $table_name - ); // Save inline column constraints and indexes. if ( null !== $index_data ) { @@ -623,7 +619,9 @@ public function record_create_table( WP_Parser_Node $node ): void { $index_data ); } - if ( null !== $constraint_data ) { + + // Inline CHECK constraints are saved by "record_column_check_constraints()". + if ( null !== $constraint_data && 'CHECK' !== $constraint_data['constraint_type'] ) { $this->insert_values( $this->get_table_name( $table_is_temporary, 'table_constraints' ), $constraint_data @@ -641,12 +639,7 @@ public function record_create_table( WP_Parser_Node $node ): void { $key_column_usage_item ); } - if ( null !== $check_constraint_data ) { - $this->insert_values( - $this->get_table_name( $table_is_temporary, 'check_constraints' ), - $check_constraint_data - ); - } + $this->record_column_check_constraints( $table_is_temporary, $table_name, $column_node ); $column_position += 1; } @@ -956,11 +949,61 @@ private function record_add_column( $table_name, $index_data['index_name'] ?? null ); - if ( null !== $constraint_data ) { + + // Inline CHECK constraints are saved by "record_column_check_constraints()". + if ( null !== $constraint_data && 'CHECK' !== $constraint_data['constraint_type'] ) { + $this->insert_values( + $this->get_table_name( $table_is_temporary, 'table_constraints' ), + $constraint_data + ); + } + + $this->record_column_check_constraints( $table_is_temporary, $table_name, $node ); + } + + /** + * Analyze inline CHECK constraints of a column and record data in the information schema. + * + * A column can define multiple CHECK constraints, each in its own attribute + * with an optional name. A "[NOT] ENFORCED" attribute applies to the CHECK + * constraint right before it, as in MySQL. + * + * @param bool $table_is_temporary Whether the table is temporary. + * @param string $table_name The table name. + * @param WP_Parser_Node $node The "columnDefinition" or "fieldDefinition" AST node. + */ + private function record_column_check_constraints( + bool $table_is_temporary, + string $table_name, + WP_Parser_Node $node + ): void { + $attributes = $node->get_descendant_nodes( 'columnAttribute' ); + foreach ( $attributes as $i => $attribute ) { + if ( null === $attribute->get_first_child_node( 'checkConstraint' ) ) { + continue; + } + + // Extract the data before saving, so that a generated name is the same in both. + $constraint_data = $this->extract_table_constraint_data( $attribute, $table_name ); + $check_constraint_data = $this->extract_check_constraint_data( $attribute, $table_name ); + + // Apply the "[NOT] ENFORCED" attributes that follow the CHECK constraint. + for ( $j = $i + 1; isset( $attributes[ $j ] ); $j += 1 ) { + $enforcement = $attributes[ $j ]->get_first_child_node( 'constraintEnforcement' ); + if ( null === $enforcement ) { + break; + } + $constraint_data['enforced'] = $enforcement->has_child_token( WP_MySQL_Lexer::NOT_SYMBOL ) ? 'NO' : 'YES'; + } + $this->insert_values( $this->get_table_name( $table_is_temporary, 'table_constraints' ), $constraint_data ); + $this->insert_values( + $this->get_table_name( $table_is_temporary, 'check_constraints' ), + $check_constraint_data + ); } } diff --git a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php index 615959486..7e2f9fac1 100644 --- a/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php +++ b/packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php @@ -97,6 +97,21 @@ private function getLastColumnMeta(): array { return $column_meta; } + /** + * Get the CHECK constraints of table "t" from the information schema. + */ + private function getCheckConstraints(): array { + return $this->assertQuery( + "SELECT tc.CONSTRAINT_NAME AS name, tc.ENFORCED AS enforced, cc.CHECK_CLAUSE AS clause + FROM information_schema.TABLE_CONSTRAINTS tc + JOIN information_schema.CHECK_CONSTRAINTS cc + ON cc.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA + AND cc.CONSTRAINT_NAME = tc.CONSTRAINT_NAME + WHERE tc.TABLE_NAME = 't' AND tc.CONSTRAINT_TYPE = 'CHECK' + ORDER BY tc.CONSTRAINT_NAME" + ); + } + /** * Reconnect as if the database was configured by an older driver version, * so that the new connection configures the database again. @@ -15591,6 +15606,271 @@ public function testCheckConstraintNotEnforced(): void { ); } + public function testCreateTableWithNamedInlineCheckConstraint(): void { + $this->assertQuery( 'CREATE TABLE t (a INT CONSTRAINT positive CHECK (a > 0))' ); + + $this->assertEquals( + array( + (object) array( + 'name' => 'positive', + 'enforced' => 'YES', + 'clause' => 'a > 0', + ), + ), + $this->getCheckConstraints() + ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `a` int DEFAULT NULL,', + ' CONSTRAINT `positive` CHECK (a > 0)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertQuery( 'INSERT INTO t (a) VALUES (1)' ); + $this->assertQueryError( + 'INSERT INTO t (a) VALUES (0)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: positive' + ); + } + + public function testCreateTableWithMultipleInlineCheckConstraints(): void { + $this->assertQuery( + 'CREATE TABLE t ( + id INT, + CONSTRAINT c1 CHECK (id > 0), + a INT CHECK (a > 0) NOT ENFORCED CHECK (a < 10), + b INT CONSTRAINT b_pos CHECK (b > 0) + )' + ); + + // "NOT ENFORCED" applies only to the CHECK constraint right before it. + $this->assertEquals( + array( + (object) array( + 'name' => 'b_pos', + 'enforced' => 'YES', + 'clause' => 'b > 0', + ), + (object) array( + 'name' => 'c1', + 'enforced' => 'YES', + 'clause' => 'id > 0', + ), + (object) array( + 'name' => 't_chk_1', + 'enforced' => 'NO', + 'clause' => 'a > 0', + ), + (object) array( + 'name' => 't_chk_2', + 'enforced' => 'YES', + 'clause' => 'a < 10', + ), + ), + $this->getCheckConstraints() + ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `id` int DEFAULT NULL,', + ' `a` int DEFAULT NULL,', + ' `b` int DEFAULT NULL,', + ' CONSTRAINT `b_pos` CHECK (b > 0),', + ' CONSTRAINT `c1` CHECK (id > 0),', + ' CONSTRAINT `t_chk_1` CHECK (a > 0) /*!80016 NOT ENFORCED */,', + ' CONSTRAINT `t_chk_2` CHECK (a < 10)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertQuery( 'INSERT INTO t (id, a, b) VALUES (1, 0, 1)' ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b) VALUES (1, 10, 1)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: t_chk_2' + ); + $this->assertQueryError( + 'INSERT INTO t (id, a, b) VALUES (1, 1, 0)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: b_pos' + ); + } + + public function testCreateTableWithInlineCheckAndUniqueConstraints(): void { + $this->assertQuery( 'CREATE TABLE t (a INT UNIQUE CHECK (a > 0))' ); + + $this->assertEquals( + array( + (object) array( + 'name' => 't_chk_1', + 'enforced' => 'YES', + 'clause' => 'a > 0', + ), + ), + $this->getCheckConstraints() + ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `a` int DEFAULT NULL,', + ' UNIQUE KEY `a` (`a`),', + ' CONSTRAINT `t_chk_1` CHECK (a > 0)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertQuery( 'INSERT INTO t (a) VALUES (1)' ); + $this->assertQueryError( + 'INSERT INTO t (a) VALUES (-1)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: t_chk_1' + ); + $this->assertQueryError( + 'INSERT INTO t (a) VALUES (1)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: t.a' + ); + } + + public function testAlterTableAddColumnWithInlineCheckConstraint(): void { + $this->assertQuery( 'CREATE TABLE t (id INT, CHECK (id > 0))' ); + $this->assertQuery( 'ALTER TABLE t ADD a INT CHECK (a > 0)' ); + + $this->assertEquals( + array( + (object) array( + 'name' => 't_chk_1', + 'enforced' => 'YES', + 'clause' => 'id > 0', + ), + (object) array( + 'name' => 't_chk_2', + 'enforced' => 'YES', + 'clause' => 'a > 0', + ), + ), + $this->getCheckConstraints() + ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `id` int DEFAULT NULL,', + ' `a` int DEFAULT NULL,', + ' CONSTRAINT `t_chk_1` CHECK (id > 0),', + ' CONSTRAINT `t_chk_2` CHECK (a > 0)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertQuery( 'INSERT INTO t (id, a) VALUES (1, 1)' ); + $this->assertQueryError( + 'INSERT INTO t (id, a) VALUES (1, 0)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: t_chk_2' + ); + } + + public function testAlterTableAddColumnListWithInlineCheckConstraints(): void { + $this->assertQuery( 'CREATE TABLE t (id INT)' ); + $this->assertQuery( 'ALTER TABLE t ADD (a INT CHECK (a > 0), b INT CONSTRAINT b_pos CHECK (b > 0))' ); + + $this->assertEquals( + array( + (object) array( + 'name' => 'b_pos', + 'enforced' => 'YES', + 'clause' => 'b > 0', + ), + (object) array( + 'name' => 't_chk_1', + 'enforced' => 'YES', + 'clause' => 'a > 0', + ), + ), + $this->getCheckConstraints() + ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `id` int DEFAULT NULL,', + ' `a` int DEFAULT NULL,', + ' `b` int DEFAULT NULL,', + ' CONSTRAINT `b_pos` CHECK (b > 0),', + ' CONSTRAINT `t_chk_1` CHECK (a > 0)', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertQuery( 'INSERT INTO t (a, b) VALUES (1, 1)' ); + $this->assertQueryError( + 'INSERT INTO t (a, b) VALUES (0, 1)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: t_chk_1' + ); + $this->assertQueryError( + 'INSERT INTO t (a, b) VALUES (1, 0)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: b_pos' + ); + } + + public function testAlterTableAddColumnWithViolatedInlineCheckConstraint(): void { + $this->assertQuery( 'CREATE TABLE t (id INT)' ); + $this->assertQuery( 'INSERT INTO t (id) VALUES (1)' ); + + // The existing row gets the default value, which violates the check. + $this->assertQueryError( + 'ALTER TABLE t ADD a INT DEFAULT 0 CHECK (a > 0)', + 'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: t_chk_1' + ); + + // The table and the information schema remain unchanged. + $this->assertSame( array(), $this->getCheckConstraints() ); + + $result = $this->assertQuery( 'SHOW CREATE TABLE t' ); + $this->assertEquals( + implode( + "\n", + array( + 'CREATE TABLE `t` (', + ' `id` int DEFAULT NULL', + ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci', + ) + ), + $result[0]->{'Create Table'} + ); + + $this->assertEquals( + array( (object) array( 'id' => '1' ) ), + $this->assertQuery( 'SELECT * FROM t' ) + ); + } + public function testDynamicDatabaseName(): void { // Create a setter for the private property "$db_name". $set_db_name = Closure::bind(