@@ -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+ INDEX idx_c (c),
1867+ c INT,
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