Skip to content

Commit 745a9af

Browse files
committed
Fix ALTER TABLE ADD with a parenthesized column list
"ALTER TABLE t ADD [COLUMN] (a INT, b INT)" failed with a TypeError. The column name in a "columnDefinition" node is a "fieldIdentifier", not an "identifier", so the name lookup returned null. The list can also hold indexes and constraints, e.g. "ADD (a INT, INDEX (a))". These were skipped when the list had a column, and only the first one was recorded when it had none. Record all list items, columns first, like CREATE TABLE does.
1 parent b5dc0cc commit 745a9af

2 files changed

Lines changed: 194 additions & 4 deletions

File tree

‎packages/mysql-on-sqlite/src/sqlite/class-wp-sqlite-information-schema-builder.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -665,12 +665,15 @@ public function record_alter_table( WP_Parser_Node $node ): void {
665665
// ADD
666666
if ( WP_MySQL_Lexer::ADD_SYMBOL === $first_token->id ) {
667667
// ADD [COLUMN] (...[, ...])
668-
$column_definitions = $action->get_descendant_nodes( 'columnDefinition' );
669-
if ( count( $column_definitions ) > 0 ) {
670-
foreach ( $column_definitions as $column_definition ) {
671-
$name = $this->get_value( $column_definition->get_first_child_node( 'identifier' ) );
668+
$table_element_list = $action->get_first_child_node( 'tableElementList' );
669+
if ( null !== $table_element_list ) {
670+
foreach ( $table_element_list->get_descendant_nodes( 'columnDefinition' ) as $column_definition ) {
671+
$name = $this->get_value( $column_definition->get_first_child_node( 'fieldIdentifier' ) );
672672
$this->record_add_column( $table_is_temporary, $table_name, $name, $column_definition );
673673
}
674+
foreach ( $table_element_list->get_descendant_nodes( 'tableConstraintDef' ) as $constraint ) {
675+
$this->record_add_constraint_or_index( $table_is_temporary, $table_name, $constraint );
676+
}
674677
continue;
675678
}
676679

‎packages/mysql-on-sqlite/tests/WP_MySQL_On_SQLite_Tests.php‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1778,6 +1778,193 @@ public function testAlterTableAddNotNullVarcharColumn() {
17781778
);
17791779
}
17801780

1781+
public function testAlterTableAddColumnList(): void {
1782+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
1783+
1784+
// ADD (...) syntax.
1785+
$this->assertQuery( "ALTER TABLE t ADD (a INT, b VARCHAR(10) NOT NULL DEFAULT 'x')" );
1786+
1787+
// ADD COLUMN (...) syntax.
1788+
$this->assertQuery( 'ALTER TABLE t ADD COLUMN (c INT)' );
1789+
1790+
$this->assertQuery( 'SHOW COLUMNS FROM t' );
1791+
$this->assertEquals(
1792+
array(
1793+
(object) array(
1794+
'Field' => 'id',
1795+
'Type' => 'int',
1796+
'Null' => 'YES',
1797+
'Key' => '',
1798+
'Default' => null,
1799+
'Extra' => '',
1800+
),
1801+
(object) array(
1802+
'Field' => 'a',
1803+
'Type' => 'int',
1804+
'Null' => 'YES',
1805+
'Key' => '',
1806+
'Default' => null,
1807+
'Extra' => '',
1808+
),
1809+
(object) array(
1810+
'Field' => 'b',
1811+
'Type' => 'varchar(10)',
1812+
'Null' => 'NO',
1813+
'Key' => '',
1814+
'Default' => 'x',
1815+
'Extra' => '',
1816+
),
1817+
(object) array(
1818+
'Field' => 'c',
1819+
'Type' => 'int',
1820+
'Null' => 'YES',
1821+
'Key' => '',
1822+
'Default' => null,
1823+
'Extra' => '',
1824+
),
1825+
),
1826+
$this->last_result
1827+
);
1828+
1829+
$this->assertQuery( 'SHOW CREATE TABLE t' );
1830+
$this->assertEquals(
1831+
implode(
1832+
"\n",
1833+
array(
1834+
'CREATE TABLE `t` (',
1835+
' `id` int DEFAULT NULL,',
1836+
' `a` int DEFAULT NULL,',
1837+
" `b` varchar(10) NOT NULL DEFAULT 'x',",
1838+
' `c` int DEFAULT NULL',
1839+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
1840+
)
1841+
),
1842+
$this->last_result[0]->{'Create Table'}
1843+
);
1844+
1845+
$this->assertQuery( 'INSERT INTO t (id, a, c) VALUES (1, 2, 3)' );
1846+
$this->assertQuery( 'SELECT * FROM t' );
1847+
$this->assertEquals(
1848+
array(
1849+
(object) array(
1850+
'id' => '1',
1851+
'a' => '2',
1852+
'b' => 'x',
1853+
'c' => '3',
1854+
),
1855+
),
1856+
$this->last_result
1857+
);
1858+
}
1859+
1860+
public function testAlterTableAddColumnListWithConstraints(): void {
1861+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
1862+
$this->assertQuery(
1863+
'ALTER TABLE t ADD (
1864+
a INT AUTO_INCREMENT PRIMARY KEY,
1865+
b INT UNIQUE,
1866+
c INT,
1867+
INDEX idx_c (c),
1868+
CONSTRAINT chk_c CHECK (c > 0)
1869+
)'
1870+
);
1871+
1872+
$this->assertQuery( 'SHOW CREATE TABLE t' );
1873+
$this->assertEquals(
1874+
implode(
1875+
"\n",
1876+
array(
1877+
'CREATE TABLE `t` (',
1878+
' `id` int DEFAULT NULL,',
1879+
' `a` int NOT NULL AUTO_INCREMENT,',
1880+
' `b` int DEFAULT NULL,',
1881+
' `c` int DEFAULT NULL,',
1882+
' PRIMARY KEY (`a`),',
1883+
' UNIQUE KEY `b` (`b`),',
1884+
' KEY `idx_c` (`c`),',
1885+
' CONSTRAINT `chk_c` CHECK (c > 0)',
1886+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
1887+
)
1888+
),
1889+
$this->last_result[0]->{'Create Table'}
1890+
);
1891+
1892+
// Insert valid data.
1893+
$this->assertQuery( 'INSERT INTO t (c) VALUES (1)' );
1894+
$this->assertQuery( 'SELECT * FROM t' );
1895+
$this->assertEquals(
1896+
array(
1897+
(object) array(
1898+
'id' => null,
1899+
'a' => '1',
1900+
'b' => null,
1901+
'c' => '1',
1902+
),
1903+
),
1904+
$this->last_result
1905+
);
1906+
1907+
// Insert invalid data.
1908+
$this->assertQueryError(
1909+
'INSERT INTO t (c) VALUES (0)',
1910+
'SQLSTATE[23000]: Integrity constraint violation: 19 CHECK constraint failed: chk_c'
1911+
);
1912+
}
1913+
1914+
public function testAlterTableAddIndexList(): void {
1915+
$this->assertQuery( 'CREATE TABLE t (a INT, b INT)' );
1916+
$this->assertQuery( 'ALTER TABLE t ADD (INDEX idx_a (a), UNIQUE KEY uk_b (b))' );
1917+
1918+
$this->assertQuery( 'SHOW CREATE TABLE t' );
1919+
$this->assertEquals(
1920+
implode(
1921+
"\n",
1922+
array(
1923+
'CREATE TABLE `t` (',
1924+
' `a` int DEFAULT NULL,',
1925+
' `b` int DEFAULT NULL,',
1926+
' UNIQUE KEY `uk_b` (`b`),',
1927+
' KEY `idx_a` (`a`)',
1928+
') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci',
1929+
)
1930+
),
1931+
$this->last_result[0]->{'Create Table'}
1932+
);
1933+
}
1934+
1935+
public function testAlterTableAddColumnListErrors(): void {
1936+
$this->assertQuery( 'CREATE TABLE t (id INT)' );
1937+
1938+
$this->assertQueryError(
1939+
'ALTER TABLE t ADD (a INT, id INT)',
1940+
"SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'id'"
1941+
);
1942+
$this->assertQueryError(
1943+
'ALTER TABLE t ADD (a INT, a INT)',
1944+
"SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'a'"
1945+
);
1946+
$this->assertQueryError(
1947+
'ALTER TABLE t ADD (a INT, INDEX (b))',
1948+
"SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'b' doesn't exist in table"
1949+
);
1950+
1951+
// The table is unchanged.
1952+
$this->assertQuery( 'SHOW COLUMNS FROM t' );
1953+
$this->assertEquals(
1954+
array(
1955+
(object) array(
1956+
'Field' => 'id',
1957+
'Type' => 'int',
1958+
'Null' => 'YES',
1959+
'Key' => '',
1960+
'Default' => null,
1961+
'Extra' => '',
1962+
),
1963+
),
1964+
$this->last_result
1965+
);
1966+
}
1967+
17811968
public function testColumnWithOnUpdate() {
17821969
// CREATE TABLE with ON UPDATE
17831970
$this->assertQuery(

0 commit comments

Comments
 (0)