Skip to content

Commit

Permalink
add default defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
trevyn committed Jan 10, 2024
1 parent 604e287 commit 44be762
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
19 changes: 19 additions & 0 deletions test.migrations.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,18 @@ migrations_append_only = [
"ALTER TABLE person ADD COLUMN name TEXT",
"ALTER TABLE person ADD COLUMN age INTEGER",
"ALTER TABLE person ADD COLUMN image_jpg BLOB",
"CREATE TABLE nooption (rowid INTEGER PRIMARY KEY) STRICT",
"ALTER TABLE nooption ADD COLUMN e INTEGER NOT NULL DEFAULT 0",
]
output_generated_schema_for_your_information_do_not_edit = """
CREATE TABLE _turbosql_migrations (
rowid INTEGER PRIMARY KEY,
migration TEXT NOT NULL
) STRICT
CREATE TABLE nooption (
rowid INTEGER PRIMARY KEY,
e INTEGER NOT NULL DEFAULT 0
) STRICT
CREATE TABLE person (
rowid INTEGER PRIMARY KEY,
name TEXT,
Expand Down Expand Up @@ -86,6 +92,19 @@ output_generated_schema_for_your_information_do_not_edit = """
) STRICT
"""

[output_generated_tables_do_not_edit.nooption]
name = "nooption"

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

[[output_generated_tables_do_not_edit.nooption.columns]]
name = "e"
rust_type = "u8"
sql_type = "INTEGER NOT NULL"

[output_generated_tables_do_not_edit.person]
name = "person"

Expand Down
9 changes: 5 additions & 4 deletions turbosql-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,11 +860,11 @@ fn extract_columns(fields: &FieldsNamed) -> Vec<Column> {
(_, "String") => ("TEXT NOT NULL", "\"\""),
// SELECT LENGTH(blob_column) ... will be null if blob is null
(_, "Option < Blob >") => ("BLOB", "b\"\""),
(_, "Blob") => ("BLOB NOT NULL", "b\"\""),
(_, "Blob") => ("BLOB NOT NULL", "\"\""),
(_, "Option < Vec < u8 > >") => ("BLOB", "b\"\""),
(_, "Vec < u8 >") => ("BLOB NOT NULL", "b\"\""),
(_, "Vec < u8 >") => ("BLOB NOT NULL", "\"\""),
(_, "Option < [u8; _] >") => ("BLOB", "b\"\\x00\\x01\\xff\""),
(_, "[u8; _]") => ("BLOB NOT NULL", "b\"\\x00\\x01\\xff\""),
(_, "[u8; _]") => ("BLOB NOT NULL", "\"\""),
_ => {
// JSON-serialized
if ty_str.starts_with("Option < ") {
Expand All @@ -876,7 +876,8 @@ fn extract_columns(fields: &FieldsNamed) -> Vec<Column> {
};

if sql_default.is_none() && sql_type.ends_with("NOT NULL") {
abort!(f, "Field {} has no default value and is not nullable. Either add a default value with e.g. #[turbosql(sql_default = {default_example})] or make it Option<{ty_str}>.", name);
sql_default = Some(default_example.into());
// abort!(f, "Field `{}` has no default value and is not nullable. Either add a default value with e.g. #[turbosql(sql_default = {default_example})] or make it Option<{ty_str}>.", name);
}

Some(Column {
Expand Down
6 changes: 0 additions & 6 deletions turbosql/tests/ui/macros.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
error: Field e has no default value and is not nullable. Either add a default value with e.g. #[turbosql(sql_default = 0)] or make it Option<u8>.
--> tests/ui/macros.rs:8:2
|
8 | e: u8,
| ^^^^^

error: derive(Turbosql) structs must include a 'rowid: Option<i64>' field
--> tests/ui/macros.rs:11:10
|
Expand Down

0 comments on commit 44be762

Please sign in to comment.