use super::{type_map, FieldDef};
pub fn gen(
snake: &str,
fields: &[FieldDef],
id: Option<&str>,
soft_delete: bool,
timestamps: bool,
) -> String {
let pk = super::pk_ddl(id);
let mut cols = vec![pk.to_string()];
for f in fields {
let tm = type_map(&f.field_type);
let constraint = if f.optional { "" } else { " NOT NULL" };
cols.push(format!(" \"{}\" {}{}", f.name, tm.sql_type, constraint));
}
if timestamps {
cols.push(" \"created_at\" TIMESTAMPTZ NOT NULL DEFAULT NOW()".to_string());
cols.push(" \"updated_at\" TIMESTAMPTZ NOT NULL DEFAULT NOW()".to_string());
}
if soft_delete {
cols.push(" \"deleted_at\" TIMESTAMPTZ".to_string());
}
let cols_sql = cols.join(",\n");
let table = format!("{snake}s");
format!(
"-- up\nCREATE TABLE \"{table}\" (\n{cols_sql}\n);\n\n-- down\nDROP TABLE IF EXISTS \"{table}\";\n"
)
}