Skip to content

Commit 919c25d

Browse files
committed
Fix serialization of FK column constraint and deserialization of CreateIndex requests. Fixes #5.
1 parent 6580c15 commit 919c25d

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

trailbase-core/src/schema.rs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ pub enum ColumnOption {
157157
}
158158

159159
impl ColumnOption {
160-
fn to_fragment(&self, col_name: &str) -> String {
160+
fn to_fragment(&self) -> String {
161161
return match self {
162162
Self::Null => "NULL".to_string(),
163163
Self::NotNull => "NOT NULL".to_string(),
@@ -176,7 +176,7 @@ impl ColumnOption {
176176
on_update,
177177
} => {
178178
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}",
180180
ref_col = referred_columns.first().unwrap(),
181181
on_delete = on_delete.as_ref().map_or_else(
182182
|| "".to_string(),
@@ -326,7 +326,7 @@ impl Column {
326326
options = self
327327
.options
328328
.iter()
329-
.map(|o| o.to_fragment(&self.name))
329+
.map(|o| o.to_fragment())
330330
.collect::<Vec<_>>()
331331
.join(" "),
332332
);
@@ -375,6 +375,7 @@ impl Table {
375375
.iter()
376376
.map(|c| c.to_fragment())
377377
.collect::<Vec<_>>();
378+
log::info!("COL DEFS: {column_defs:?}");
378379
column_defs_and_table_constraints.extend(column_defs);
379380

380381
// Example: UNIQUE (email),
@@ -391,6 +392,7 @@ impl Table {
391392
.iter()
392393
.map(|fk| fk.to_fragment())
393394
.collect::<Vec<_>>();
395+
log::info!("TABLE FKs: {fk_table_constraints:?}");
394396
column_defs_and_table_constraints.extend(fk_table_constraints);
395397

396398
return format!(
@@ -403,7 +405,7 @@ impl Table {
403405
}
404406
}
405407

406-
#[derive(Clone, Debug, Serialize, Deserialize, TS, PartialEq)]
408+
#[derive(Clone, Default, Debug, Serialize, Deserialize, TS, PartialEq)]
407409
pub struct TableIndex {
408410
pub name: String,
409411
pub table_name: String,
@@ -412,6 +414,7 @@ pub struct TableIndex {
412414
pub predicate: Option<String>,
413415

414416
#[ts(skip)]
417+
#[serde(default)]
415418
pub if_not_exists: bool,
416419
}
417420

@@ -1024,13 +1027,24 @@ mod tests {
10241027
use super::*;
10251028
use crate::constants::USER_TABLE;
10261029

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() {
10291041
lazy_static! {
10301042
static ref SQL: String = format!(
10311043
r#"
10321044
CREATE TABLE test (
10331045
id BLOB PRIMARY KEY DEFAULT (uuid_v7()) NOT NULL,
1046+
user BLOB DEFAULT '' REFERENCES _user(id),
1047+
user_id BLOB,
10341048
email TEXT NOT NULL,
10351049
email_visibility INTEGER DEFAULT FALSE NOT NULL,
10361050
username TEXT,
@@ -1045,22 +1059,32 @@ mod tests {
10451059
);
10461060
}
10471061

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+
10481068
let statement1 = crate::table_metadata::sqlite3_parse_into_statement(&SQL)
10491069
.unwrap()
10501070
.unwrap();
1051-
let table1: Table = statement1.clone().try_into()?;
1071+
let table1: Table = statement1.clone().try_into().unwrap();
10521072

10531073
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+
10541080
let statement2 = crate::table_metadata::sqlite3_parse_into_statement(&sql)
10551081
.unwrap()
10561082
.unwrap();
10571083

1058-
let table2: Table = statement2.clone().try_into()?;
1084+
let table2: Table = statement2.clone().try_into().unwrap();
10591085

10601086
assert_eq!(statement1, statement2);
10611087
assert_eq!(table1, table2);
1062-
1063-
Ok(())
10641088
}
10651089

10661090
#[test]

0 commit comments

Comments
 (0)