@@ -1875,6 +1875,120 @@ fn parse_delete_with_limit() {
18751875 }
18761876}
18771877
1878+ #[ test]
1879+ fn parse_alter_table_add_column ( ) {
1880+ match mysql ( ) . verified_stmt ( "ALTER TABLE tab ADD COLUMN b INT FIRST" ) {
1881+ Statement :: AlterTable {
1882+ name,
1883+ if_exists,
1884+ only,
1885+ operations,
1886+ location : _,
1887+ } => {
1888+ assert_eq ! ( name. to_string( ) , "tab" ) ;
1889+ assert ! ( !if_exists) ;
1890+ assert ! ( !only) ;
1891+ assert_eq ! (
1892+ operations,
1893+ vec![ AlterTableOperation :: AddColumn {
1894+ column_keyword: true ,
1895+ if_not_exists: false ,
1896+ column_def: ColumnDef {
1897+ name: "b" . into( ) ,
1898+ data_type: DataType :: Int ( None ) ,
1899+ collation: None ,
1900+ options: vec![ ] ,
1901+ } ,
1902+ column_position: Some ( MySQLColumnPosition :: First ) ,
1903+ } , ]
1904+ ) ;
1905+ }
1906+ _ => unreachable ! ( ) ,
1907+ }
1908+
1909+ match mysql ( ) . verified_stmt ( "ALTER TABLE tab ADD COLUMN b INT AFTER foo" ) {
1910+ Statement :: AlterTable {
1911+ name,
1912+ if_exists,
1913+ only,
1914+ operations,
1915+ location : _,
1916+ } => {
1917+ assert_eq ! ( name. to_string( ) , "tab" ) ;
1918+ assert ! ( !if_exists) ;
1919+ assert ! ( !only) ;
1920+ assert_eq ! (
1921+ operations,
1922+ vec![ AlterTableOperation :: AddColumn {
1923+ column_keyword: true ,
1924+ if_not_exists: false ,
1925+ column_def: ColumnDef {
1926+ name: "b" . into( ) ,
1927+ data_type: DataType :: Int ( None ) ,
1928+ collation: None ,
1929+ options: vec![ ] ,
1930+ } ,
1931+ column_position: Some ( MySQLColumnPosition :: After ( Ident {
1932+ value: String :: from( "foo" ) ,
1933+ quote_style: None
1934+ } ) ) ,
1935+ } , ]
1936+ ) ;
1937+ }
1938+ _ => unreachable ! ( ) ,
1939+ }
1940+ }
1941+
1942+ #[ test]
1943+ fn parse_alter_table_add_columns ( ) {
1944+ match mysql ( )
1945+ . verified_stmt ( "ALTER TABLE tab ADD COLUMN a TEXT FIRST, ADD COLUMN b INT AFTER foo" )
1946+ {
1947+ Statement :: AlterTable {
1948+ name,
1949+ if_exists,
1950+ only,
1951+ operations,
1952+ location : _,
1953+ } => {
1954+ assert_eq ! ( name. to_string( ) , "tab" ) ;
1955+ assert ! ( !if_exists) ;
1956+ assert ! ( !only) ;
1957+ assert_eq ! (
1958+ operations,
1959+ vec![
1960+ AlterTableOperation :: AddColumn {
1961+ column_keyword: true ,
1962+ if_not_exists: false ,
1963+ column_def: ColumnDef {
1964+ name: "a" . into( ) ,
1965+ data_type: DataType :: Text ,
1966+ collation: None ,
1967+ options: vec![ ] ,
1968+ } ,
1969+ column_position: Some ( MySQLColumnPosition :: First ) ,
1970+ } ,
1971+ AlterTableOperation :: AddColumn {
1972+ column_keyword: true ,
1973+ if_not_exists: false ,
1974+ column_def: ColumnDef {
1975+ name: "b" . into( ) ,
1976+ data_type: DataType :: Int ( None ) ,
1977+ collation: None ,
1978+ options: vec![ ] ,
1979+ } ,
1980+ column_position: Some ( MySQLColumnPosition :: After ( Ident {
1981+ value: String :: from( "foo" ) ,
1982+ quote_style: None ,
1983+ } ) ) ,
1984+ } ,
1985+ ]
1986+ ) ;
1987+ }
1988+ _ => unreachable ! ( ) ,
1989+ }
1990+ }
1991+
18781992#[ test]
18791993fn parse_alter_table_drop_primary_key ( ) {
18801994 assert_matches ! (
@@ -1891,6 +2005,7 @@ fn parse_alter_table_change_column() {
18912005 new_name : Ident :: new ( "desc" ) ,
18922006 data_type : DataType :: Text ,
18932007 options : vec ! [ ColumnOption :: NotNull ] ,
2008+ column_position : None ,
18942009 } ;
18952010
18962011 let sql1 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL" ;
@@ -1904,6 +2019,80 @@ fn parse_alter_table_change_column() {
19042019 & expected_name. to_string ( ) ,
19052020 ) ;
19062021 assert_eq ! ( expected_operation, operation) ;
2022+
2023+ let expected_operation = AlterTableOperation :: ChangeColumn {
2024+ old_name : Ident :: new ( "description" ) ,
2025+ new_name : Ident :: new ( "desc" ) ,
2026+ data_type : DataType :: Text ,
2027+ options : vec ! [ ColumnOption :: NotNull ] ,
2028+ column_position : Some ( MySQLColumnPosition :: First ) ,
2029+ } ;
2030+ let sql3 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL FIRST" ;
2031+ let operation =
2032+ alter_table_op_with_name ( mysql ( ) . verified_stmt ( sql3) , & expected_name. to_string ( ) ) ;
2033+ assert_eq ! ( expected_operation, operation) ;
2034+
2035+ let expected_operation = AlterTableOperation :: ChangeColumn {
2036+ old_name : Ident :: new ( "description" ) ,
2037+ new_name : Ident :: new ( "desc" ) ,
2038+ data_type : DataType :: Text ,
2039+ options : vec ! [ ColumnOption :: NotNull ] ,
2040+ column_position : Some ( MySQLColumnPosition :: After ( Ident {
2041+ value : String :: from ( "foo" ) ,
2042+ quote_style : None ,
2043+ } ) ) ,
2044+ } ;
2045+ let sql4 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL AFTER foo" ;
2046+ let operation =
2047+ alter_table_op_with_name ( mysql ( ) . verified_stmt ( sql4) , & expected_name. to_string ( ) ) ;
2048+ assert_eq ! ( expected_operation, operation) ;
2049+ }
2050+
2051+ #[ test]
2052+ fn parse_alter_table_change_column_with_column_position ( ) {
2053+ let expected_name = ObjectName ( vec ! [ Ident :: new( "orders" ) ] ) ;
2054+ let expected_operation_first = AlterTableOperation :: ChangeColumn {
2055+ old_name : Ident :: new ( "description" ) ,
2056+ new_name : Ident :: new ( "desc" ) ,
2057+ data_type : DataType :: Text ,
2058+ options : vec ! [ ColumnOption :: NotNull ] ,
2059+ column_position : Some ( MySQLColumnPosition :: First ) ,
2060+ } ;
2061+
2062+ let sql1 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL FIRST" ;
2063+ let operation =
2064+ alter_table_op_with_name ( mysql ( ) . verified_stmt ( sql1) , & expected_name. to_string ( ) ) ;
2065+ assert_eq ! ( expected_operation_first, operation) ;
2066+
2067+ let sql2 = "ALTER TABLE orders CHANGE description desc TEXT NOT NULL FIRST" ;
2068+ let operation = alter_table_op_with_name (
2069+ mysql ( ) . one_statement_parses_to ( sql2, sql1) ,
2070+ & expected_name. to_string ( ) ,
2071+ ) ;
2072+ assert_eq ! ( expected_operation_first, operation) ;
2073+
2074+ let expected_operation_after = AlterTableOperation :: ChangeColumn {
2075+ old_name : Ident :: new ( "description" ) ,
2076+ new_name : Ident :: new ( "desc" ) ,
2077+ data_type : DataType :: Text ,
2078+ options : vec ! [ ColumnOption :: NotNull ] ,
2079+ column_position : Some ( MySQLColumnPosition :: After ( Ident {
2080+ value : String :: from ( "total_count" ) ,
2081+ quote_style : None ,
2082+ } ) ) ,
2083+ } ;
2084+
2085+ let sql1 = "ALTER TABLE orders CHANGE COLUMN description desc TEXT NOT NULL AFTER total_count" ;
2086+ let operation =
2087+ alter_table_op_with_name ( mysql ( ) . verified_stmt ( sql1) , & expected_name. to_string ( ) ) ;
2088+ assert_eq ! ( expected_operation_after, operation) ;
2089+
2090+ let sql2 = "ALTER TABLE orders CHANGE description desc TEXT NOT NULL AFTER total_count" ;
2091+ let operation = alter_table_op_with_name (
2092+ mysql ( ) . one_statement_parses_to ( sql2, sql1) ,
2093+ & expected_name. to_string ( ) ,
2094+ ) ;
2095+ assert_eq ! ( expected_operation_after, operation) ;
19072096}
19082097
19092098#[ test]
0 commit comments