@@ -61,6 +61,44 @@ fn validate_table(
6161 return Err ( PlannerError :: MissingPrimaryKey ( table. name . clone ( ) ) ) ;
6262 }
6363
64+ // Validate auto_increment columns have integer types
65+ for constraint in & table. constraints {
66+ if let TableConstraint :: PrimaryKey {
67+ auto_increment : true ,
68+ columns,
69+ } = constraint
70+ {
71+ for col_name in columns {
72+ if let Some ( column) = table. columns . iter ( ) . find ( |c| c. name == * col_name)
73+ && !column. r#type . supports_auto_increment ( ) {
74+ return Err ( PlannerError :: InvalidAutoIncrement (
75+ table. name . clone ( ) ,
76+ col_name. clone ( ) ,
77+ format ! ( "{:?}" , column. r#type) ,
78+ ) ) ;
79+ }
80+ }
81+ }
82+ }
83+
84+ // Validate auto_increment on inline primary_key definitions
85+ use vespertide_core:: schema:: primary_key:: PrimaryKeySyntax ;
86+ for column in & table. columns {
87+ if let Some ( pk_syntax) = & column. primary_key {
88+ let has_auto_increment = match pk_syntax {
89+ PrimaryKeySyntax :: Bool ( _) => false ,
90+ PrimaryKeySyntax :: Object ( pk_def) => pk_def. auto_increment ,
91+ } ;
92+ if has_auto_increment && !column. r#type . supports_auto_increment ( ) {
93+ return Err ( PlannerError :: InvalidAutoIncrement (
94+ table. name . clone ( ) ,
95+ column. name . clone ( ) ,
96+ format ! ( "{:?}" , column. r#type) ,
97+ ) ) ;
98+ }
99+ }
100+ }
101+
64102 // Validate columns (enum types)
65103 for column in & table. columns {
66104 validate_column ( column, & table. name ) ?;
@@ -463,6 +501,7 @@ pub fn find_missing_fill_with(plan: &MigrationPlan) -> Vec<FillWithRequired> {
463501mod tests {
464502 use super :: * ;
465503 use rstest:: rstest;
504+ use vespertide_core:: schema:: primary_key:: { PrimaryKeyDef , PrimaryKeySyntax } ;
466505 use vespertide_core:: {
467506 ColumnDef , ColumnType , ComplexColumnType , EnumValues , NumValue , SimpleColumnType ,
468507 TableConstraint ,
@@ -1826,4 +1865,61 @@ mod tests {
18261865 let missing = find_missing_fill_with ( & plan) ;
18271866 assert ! ( missing. is_empty( ) ) ;
18281867 }
1868+
1869+ #[ test]
1870+ fn validate_auto_increment_on_text_column_fails ( ) {
1871+ let table_def = table (
1872+ "users" ,
1873+ vec ! [ col( "id" , ColumnType :: Simple ( SimpleColumnType :: Text ) ) ] ,
1874+ vec ! [ TableConstraint :: PrimaryKey {
1875+ auto_increment: true ,
1876+ columns: vec![ "id" . into( ) ] ,
1877+ } ] ,
1878+ ) ;
1879+
1880+ let result = validate_table ( & table_def, & std:: collections:: HashMap :: new ( ) ) ;
1881+ assert ! ( result. is_err( ) ) ;
1882+ match result {
1883+ Err ( PlannerError :: InvalidAutoIncrement ( table_name, col_name, _) ) => {
1884+ assert_eq ! ( table_name, "users" ) ;
1885+ assert_eq ! ( col_name, "id" ) ;
1886+ }
1887+ _ => panic ! ( "Expected InvalidAutoIncrement error" ) ,
1888+ }
1889+ }
1890+
1891+ #[ test]
1892+ fn validate_auto_increment_on_integer_column_succeeds ( ) {
1893+ let table_def = table (
1894+ "users" ,
1895+ vec ! [ col( "id" , ColumnType :: Simple ( SimpleColumnType :: Integer ) ) ] ,
1896+ vec ! [ TableConstraint :: PrimaryKey {
1897+ auto_increment: true ,
1898+ columns: vec![ "id" . into( ) ] ,
1899+ } ] ,
1900+ ) ;
1901+
1902+ let result = validate_table ( & table_def, & std:: collections:: HashMap :: new ( ) ) ;
1903+ assert ! ( result. is_ok( ) ) ;
1904+ }
1905+
1906+ #[ test]
1907+ fn validate_inline_auto_increment_on_text_column_fails ( ) {
1908+ let mut col_def = col ( "id" , ColumnType :: Simple ( SimpleColumnType :: Text ) ) ;
1909+ col_def. primary_key = Some ( PrimaryKeySyntax :: Object ( PrimaryKeyDef {
1910+ auto_increment : true ,
1911+ } ) ) ;
1912+
1913+ let table_def = table ( "users" , vec ! [ col_def] , vec ! [ ] ) ;
1914+
1915+ let result = validate_table ( & table_def, & std:: collections:: HashMap :: new ( ) ) ;
1916+ assert ! ( result. is_err( ) ) ;
1917+ match result {
1918+ Err ( PlannerError :: InvalidAutoIncrement ( table_name, col_name, _) ) => {
1919+ assert_eq ! ( table_name, "users" ) ;
1920+ assert_eq ! ( col_name, "id" ) ;
1921+ }
1922+ _ => panic ! ( "Expected InvalidAutoIncrement error" ) ,
1923+ }
1924+ }
18291925}
0 commit comments