Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit 98bc3d7

Browse files
committed
Add unique constraints to information schema tables
1 parent 5004c07 commit 98bc3d7

9 files changed

+512
-141
lines changed

tests/WP_SQLite_Driver_Tests.php

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2313,7 +2313,7 @@ public function testNestedTransactionWorkComplexModify() {
23132313
} catch ( Throwable $e ) {
23142314
$error = $e->getMessage();
23152315
}
2316-
$this->assertStringContainsString( 'duplicate column name: test', $error );
2316+
$this->assertStringContainsString( "Duplicate column name 'test'", $error );
23172317

23182318
// Commit the transaction.
23192319
$this->assertQuery( 'COMMIT' );
@@ -3048,7 +3048,7 @@ public function testCreateTableIfNotExists(): void {
30483048
'CREATE TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
30493049
);
30503050

3051-
$this->expectExceptionMessage( 'table `t` already exists' );
3051+
$this->expectExceptionMessage( "Table 't' already exists" );
30523052
$this->assertQuery(
30533053
'CREATE TABLE t (ID INTEGER, name TEXT)'
30543054
);
@@ -3062,7 +3062,7 @@ public function testCreateTemporaryTableIfNotExists(): void {
30623062
'CREATE TEMPORARY TABLE IF NOT EXISTS t (ID INTEGER, name TEXT)'
30633063
);
30643064

3065-
$this->expectExceptionMessage( 'table `t` already exists' );
3065+
$this->expectExceptionMessage( "Table 't' already exists" );
30663066
$this->assertQuery(
30673067
'CREATE TEMPORARY TABLE t (ID INTEGER, name TEXT)'
30683068
);
@@ -4469,4 +4469,132 @@ public function testMultiQueryNotSupported(): void {
44694469
$this->expectExceptionMessage( 'Multi-query is not supported.' );
44704470
$this->assertQuery( 'SELECT 1; SELECT 2' );
44714471
}
4472+
4473+
public function testCreateTableDuplicateTableName(): void {
4474+
$exception = null;
4475+
try {
4476+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
4477+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
4478+
} catch ( WP_SQLite_Driver_Exception $e ) {
4479+
$exception = $e;
4480+
}
4481+
4482+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4483+
$this->assertSame( "SQLSTATE[42S01]: Base table or view already exists: 1050 Table 't' already exists", $exception->getMessage() );
4484+
$this->assertSame( '42S01', $exception->getCode() );
4485+
}
4486+
4487+
public function testCreateTableDuplicateColumnName(): void {
4488+
$exception = null;
4489+
try {
4490+
$this->assertQuery( 'CREATE TABLE t (col INT, col INT)' );
4491+
} catch ( WP_SQLite_Driver_Exception $e ) {
4492+
$exception = $e;
4493+
}
4494+
4495+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4496+
$this->assertSame( "SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'col'", $exception->getMessage() );
4497+
$this->assertSame( '42S21', $exception->getCode() );
4498+
}
4499+
4500+
public function testCreateTableDuplicateKeyName(): void {
4501+
$exception = null;
4502+
try {
4503+
$this->assertQuery( 'CREATE TABLE t (id1 INT, id2 INT, INDEX idx (id1), INDEX idx (id2))' );
4504+
} catch ( WP_SQLite_Driver_Exception $e ) {
4505+
$exception = $e;
4506+
}
4507+
4508+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4509+
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
4510+
$this->assertSame( '42S21', $exception->getCode() );
4511+
}
4512+
4513+
public function testCreateTableDuplicateKeyNameWithUnique(): void {
4514+
$exception = null;
4515+
try {
4516+
$this->assertQuery( 'CREATE TABLE t (id1 INT, id2 INT, INDEX idx (id1), UNIQUE idx (id2))' );
4517+
} catch ( WP_SQLite_Driver_Exception $e ) {
4518+
$exception = $e;
4519+
}
4520+
4521+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4522+
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
4523+
$this->assertSame( '42S21', $exception->getCode() );
4524+
}
4525+
4526+
public function testCreateTableDuplicateKeyNameWithPrimaryKey(): void {
4527+
$this->assertQuery( 'CREATE TABLE t (id1 INT, id2 INT, PRIMARY KEY idx (id1), INDEX idx (id2))' );
4528+
// No exception. In MySQL, PRIMARY KEY names are ignored.
4529+
}
4530+
4531+
public function testAlterTableDuplicateColumnName(): void {
4532+
$exception = null;
4533+
try {
4534+
$this->assertQuery( 'CREATE TABLE t (col INT)' );
4535+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN col INT' );
4536+
} catch ( WP_SQLite_Driver_Exception $e ) {
4537+
$exception = $e;
4538+
}
4539+
4540+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4541+
$this->assertSame( "SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'col'", $exception->getMessage() );
4542+
$this->assertSame( '42S21', $exception->getCode() );
4543+
}
4544+
4545+
public function testAlterTableDuplicateColumnNameWithMultipleOperations(): void {
4546+
$exception = null;
4547+
try {
4548+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
4549+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN col INT, ADD COLUMN col INT' );
4550+
} catch ( WP_SQLite_Driver_Exception $e ) {
4551+
$exception = $e;
4552+
}
4553+
4554+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4555+
$this->assertSame( "SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'col'", $exception->getMessage() );
4556+
$this->assertSame( '42S21', $exception->getCode() );
4557+
}
4558+
4559+
public function testAlterTableDuplicateKeyName(): void {
4560+
$exception = null;
4561+
try {
4562+
$this->assertQuery( 'CREATE TABLE t (id INT, INDEX idx (id))' );
4563+
$this->assertQuery( 'ALTER TABLE t ADD INDEX idx (id)' );
4564+
} catch ( WP_SQLite_Driver_Exception $e ) {
4565+
$exception = $e;
4566+
}
4567+
4568+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4569+
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
4570+
$this->assertSame( '42S21', $exception->getCode() );
4571+
}
4572+
4573+
public function testAlterTableDuplicateKeyNameWithMultipleOperations(): void {
4574+
$exception = null;
4575+
try {
4576+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
4577+
$this->assertQuery( 'ALTER TABLE t ADD INDEX idx (id), ADD INDEX idx (id)' );
4578+
} catch ( WP_SQLite_Driver_Exception $e ) {
4579+
$exception = $e;
4580+
}
4581+
4582+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4583+
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
4584+
$this->assertSame( '42S21', $exception->getCode() );
4585+
}
4586+
4587+
public function testAlterTableDuplicateKeyNameWithUnique(): void {
4588+
$exception = null;
4589+
try {
4590+
$this->assertQuery( 'CREATE TABLE t (id INT, INDEX idx (id))' );
4591+
$this->assertQuery( 'ALTER TABLE t ADD UNIQUE idx (id)' );
4592+
} catch ( WP_SQLite_Driver_Exception $e ) {
4593+
$exception = $e;
4594+
}
4595+
4596+
$this->assertInstanceOf( WP_SQLite_Driver_Exception::class, $exception );
4597+
$this->assertSame( "SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'idx'", $exception->getMessage() );
4598+
$this->assertSame( '42S21', $exception->getCode() );
4599+
}
44724600
}

0 commit comments

Comments
 (0)