Skip to content

Commit

Permalink
refine #36
Browse files Browse the repository at this point in the history
  • Loading branch information
trevyn committed Dec 21, 2023
1 parent 47abc5a commit 549f15b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
35 changes: 0 additions & 35 deletions migrations.toml
Original file line number Diff line number Diff line change
@@ -1,36 +1 @@
# This file is auto-generated by Turbosql.
# It is used to create and apply automatic schema migrations.
# It should be checked into source control.
# Modifying it by hand may be dangerous; see the docs.

migrations_append_only = [
"CREATE TABLE person (rowid INTEGER PRIMARY KEY) STRICT",
"ALTER TABLE person ADD COLUMN name TEXT",
"ALTER TABLE person ADD COLUMN age INTEGER",
"ALTER TABLE person ADD COLUMN image_jpg BLOB",
]
output_generated_schema_for_your_information_do_not_edit = """
CREATE TABLE _turbosql_migrations (
rowid INTEGER PRIMARY KEY,
migration TEXT NOT NULL
) STRICT
CREATE TABLE person (
rowid INTEGER PRIMARY KEY,
name TEXT,
age INTEGER,
image_jpg BLOB
) STRICT
"""

[output_generated_tables_do_not_edit.person]
name = "person"

[[output_generated_tables_do_not_edit.person.columns]]
name = "rowid"
rust_type = "Option < i64 >"
sql_type = "INTEGER PRIMARY KEY"

[[output_generated_tables_do_not_edit.person.columns]]
name = "name"
rust_type = "Option < String >"
sql_type = "TEXT"
12 changes: 1 addition & 11 deletions turbosql-impl/src/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ use crate::Table;
pub(super) fn delete(table: &Table) -> proc_macro2::TokenStream {
let sql = makesql_delete(table);
super::validate_sql_or_abort(&sql);
let columns = table.columns.iter().filter(|c| c.name == "rowid").map(|c| {
let ident = &c.ident;
if c.sql_type == "TEXT" && c.rust_type != "Option < String >" {
quote_spanned!(c.span => &::turbosql::serde_json::to_string(&self.#ident)? as &dyn ::turbosql::ToSql)
} else {
quote_spanned!(c.span => &self.#ident as &dyn ::turbosql::ToSql)
}
})
.collect::<Vec<_>>();


quote_spanned! { table.span =>
fn delete(&self) -> Result<usize, ::turbosql::Error> {
assert!(self.rowid.is_some());
::turbosql::__TURBOSQL_DB.with(|db| {
let db = db.borrow_mut();
let mut stmt = db.prepare_cached(#sql)?;
Ok(stmt.execute(&[#( #columns ),*] as &[&dyn ::turbosql::ToSql])?)
Ok(stmt.execute([self.rowid])?)
})
}
}
Expand Down
11 changes: 7 additions & 4 deletions turbosql/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// cargo test --features test --manifest-path turbosql/Cargo.toml -- --nocapture
// cargo test --features test --manifest-path turbosql/Cargo.toml -- --nocapture --test-threads=1

#![allow(clippy::bool_assert_comparison, clippy::redundant_clone)]

Expand Down Expand Up @@ -58,13 +58,16 @@ fn integration_test() {
select!(Vec<String> "field_string FROM personintegrationtest").unwrap(),
vec!["Bob", "Bob"]
);
let mut r = PersonIntegrationTest {
..Default::default()
};
let mut r = PersonIntegrationTest::default();
let id = r.insert().unwrap();
let id2 = r.insert().unwrap();
r.rowid = Some(id);
r.delete().unwrap();
assert!(select!(PersonIntegrationTest "WHERE rowid = ?", id).is_err());
assert!(select!(PersonIntegrationTest "WHERE rowid = ?", id2).is_ok());
r.rowid = Some(id2);
r.delete().unwrap();
assert!(select!(PersonIntegrationTest "WHERE rowid = ?", id2).is_err());
execute!("DELETE FROM personintegrationtest WHERE rowid = 2").unwrap();
assert_eq!(select!(i64 "SELECT 1").unwrap(), 1);
assert_eq!(select!(bool "SELECT 1 > ? AS val", 0).unwrap(), true);
Expand Down

0 comments on commit 549f15b

Please sign in to comment.