@@ -157,7 +157,7 @@ pub enum ColumnOption {
157
157
}
158
158
159
159
impl ColumnOption {
160
- fn to_fragment ( & self , col_name : & str ) -> String {
160
+ fn to_fragment ( & self ) -> String {
161
161
return match self {
162
162
Self :: Null => "NULL" . to_string ( ) ,
163
163
Self :: NotNull => "NOT NULL" . to_string ( ) ,
@@ -176,7 +176,7 @@ impl ColumnOption {
176
176
on_update,
177
177
} => {
178
178
format ! (
179
- "FOREIGN KEY({col_name}) REFERENCES {foreign_table}({ref_col}) {on_delete} {on_update}" ,
179
+ "REFERENCES {foreign_table}({ref_col}) {on_delete} {on_update}" ,
180
180
ref_col = referred_columns. first( ) . unwrap( ) ,
181
181
on_delete = on_delete. as_ref( ) . map_or_else(
182
182
|| "" . to_string( ) ,
@@ -326,7 +326,7 @@ impl Column {
326
326
options = self
327
327
. options
328
328
. iter( )
329
- . map( |o| o. to_fragment( & self . name ) )
329
+ . map( |o| o. to_fragment( ) )
330
330
. collect:: <Vec <_>>( )
331
331
. join( " " ) ,
332
332
) ;
@@ -375,6 +375,7 @@ impl Table {
375
375
. iter ( )
376
376
. map ( |c| c. to_fragment ( ) )
377
377
. collect :: < Vec < _ > > ( ) ;
378
+ log:: info!( "COL DEFS: {column_defs:?}" ) ;
378
379
column_defs_and_table_constraints. extend ( column_defs) ;
379
380
380
381
// Example: UNIQUE (email),
@@ -391,6 +392,7 @@ impl Table {
391
392
. iter ( )
392
393
. map ( |fk| fk. to_fragment ( ) )
393
394
. collect :: < Vec < _ > > ( ) ;
395
+ log:: info!( "TABLE FKs: {fk_table_constraints:?}" ) ;
394
396
column_defs_and_table_constraints. extend ( fk_table_constraints) ;
395
397
396
398
return format ! (
@@ -403,7 +405,7 @@ impl Table {
403
405
}
404
406
}
405
407
406
- #[ derive( Clone , Debug , Serialize , Deserialize , TS , PartialEq ) ]
408
+ #[ derive( Clone , Default , Debug , Serialize , Deserialize , TS , PartialEq ) ]
407
409
pub struct TableIndex {
408
410
pub name : String ,
409
411
pub table_name : String ,
@@ -412,6 +414,7 @@ pub struct TableIndex {
412
414
pub predicate : Option < String > ,
413
415
414
416
#[ ts( skip) ]
417
+ #[ serde( default ) ]
415
418
pub if_not_exists : bool ,
416
419
}
417
420
@@ -1024,13 +1027,24 @@ mod tests {
1024
1027
use super :: * ;
1025
1028
use crate :: constants:: USER_TABLE ;
1026
1029
1027
- #[ test]
1028
- fn test_statement_to_table_schema_and_back ( ) -> Result < ( ) , Error > {
1030
+ async fn new_mem_conn ( ) -> libsql:: Connection {
1031
+ return libsql:: Builder :: new_local ( ":memory:" )
1032
+ . build ( )
1033
+ . await
1034
+ . unwrap ( )
1035
+ . connect ( )
1036
+ . unwrap ( ) ;
1037
+ }
1038
+
1039
+ #[ tokio:: test]
1040
+ async fn test_statement_to_table_schema_and_back ( ) {
1029
1041
lazy_static ! {
1030
1042
static ref SQL : String = format!(
1031
1043
r#"
1032
1044
CREATE TABLE test (
1033
1045
id BLOB PRIMARY KEY DEFAULT (uuid_v7()) NOT NULL,
1046
+ user BLOB DEFAULT '' REFERENCES _user(id),
1047
+ user_id BLOB,
1034
1048
email TEXT NOT NULL,
1035
1049
email_visibility INTEGER DEFAULT FALSE NOT NULL,
1036
1050
username TEXT,
@@ -1045,22 +1059,32 @@ mod tests {
1045
1059
) ;
1046
1060
}
1047
1061
1062
+ {
1063
+ // First Make sure the query is actually valid, as opposed to "only" parsable.
1064
+ let conn = new_mem_conn ( ) . await ;
1065
+ conn. execute ( & SQL , ( ) ) . await . unwrap ( ) ;
1066
+ }
1067
+
1048
1068
let statement1 = crate :: table_metadata:: sqlite3_parse_into_statement ( & SQL )
1049
1069
. unwrap ( )
1050
1070
. unwrap ( ) ;
1051
- let table1: Table = statement1. clone ( ) . try_into ( ) ? ;
1071
+ let table1: Table = statement1. clone ( ) . try_into ( ) . unwrap ( ) ;
1052
1072
1053
1073
let sql = table1. create_table_statement ( ) ;
1074
+ {
1075
+ // Same as above, make sure the constructed query is valid as opposed to "only" parsable.
1076
+ let conn = new_mem_conn ( ) . await ;
1077
+ conn. execute ( & sql, ( ) ) . await . unwrap ( ) ;
1078
+ }
1079
+
1054
1080
let statement2 = crate :: table_metadata:: sqlite3_parse_into_statement ( & sql)
1055
1081
. unwrap ( )
1056
1082
. unwrap ( ) ;
1057
1083
1058
- let table2: Table = statement2. clone ( ) . try_into ( ) ? ;
1084
+ let table2: Table = statement2. clone ( ) . try_into ( ) . unwrap ( ) ;
1059
1085
1060
1086
assert_eq ! ( statement1, statement2) ;
1061
1087
assert_eq ! ( table1, table2) ;
1062
-
1063
- Ok ( ( ) )
1064
1088
}
1065
1089
1066
1090
#[ test]
0 commit comments