pub fn extract_relation_prefix(fk_column: &str, current_table: &str, ref_column: &str) -> String {
let ref_suffix = format!("_{ref_column}");
let without_ref = if let Some(stripped) = fk_column.strip_suffix(&ref_suffix) {
stripped
} else {
fk_column
};
let current_lower = current_table.to_lowercase();
let without_ref_lower = without_ref.to_lowercase();
if without_ref_lower == current_lower {
return String::new();
}
let table_suffix = format!("_{current_lower}");
if without_ref_lower.ends_with(&table_suffix) {
let prefix_chars = without_ref_lower.chars().count() - table_suffix.chars().count();
return without_ref.chars().take(prefix_chars).collect();
}
without_ref.to_string()
}
pub fn build_reverse_relation_field_name(
fk_columns: &[String],
current_table: &str,
source_table: &str,
ref_column: &str,
has_multiple_fks: bool,
is_one_to_one: bool,
) -> String {
let base_name = if is_one_to_one {
source_table.to_string()
} else {
pluralize(source_table)
};
if !has_multiple_fks || fk_columns.is_empty() {
return base_name;
}
let prefix = extract_relation_prefix(&fk_columns[0], current_table, ref_column);
if prefix.is_empty() {
base_name
} else {
format!("{prefix}_{base_name}")
}
}
pub fn build_relation_enum_name(
fk_columns: &[String],
current_table: &str,
ref_column: &str,
) -> String {
if fk_columns.is_empty() {
return String::new();
}
let prefix = extract_relation_prefix(&fk_columns[0], current_table, ref_column);
if prefix.is_empty() {
String::new()
} else {
to_pascal_case(&prefix)
}
}
pub fn to_pascal_case(s: &str) -> String {
let mut result = String::new();
let mut capitalize = true;
for c in s.chars() {
let is_separator = c == '_' || c == '-';
if is_separator {
capitalize = true;
continue;
}
let ch = if capitalize {
c.to_ascii_uppercase()
} else {
c
};
capitalize = false;
result.push(ch);
}
result
}
pub fn to_screaming_snake_case(s: &str) -> String {
let mut result = String::new();
let mut prev_lower = false;
for ch in s.chars() {
if ch.is_uppercase() && prev_lower {
result.push('_');
}
if ch.is_alphanumeric() {
result.push(ch.to_ascii_uppercase());
prev_lower = ch.is_lowercase();
} else {
result.push('_');
prev_lower = false;
}
}
result.trim_end_matches('_').to_string()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IdentifierStart {
Underscore,
Letter,
}
pub fn sanitize_identifier(name: &str, start: IdentifierStart) -> String {
let mut result: String = name
.chars()
.map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '_' })
.collect();
let needs_prefix = match start {
IdentifierStart::Underscore => {
result.is_empty() || result.starts_with(|c: char| c.is_ascii_digit())
}
IdentifierStart::Letter => !result.starts_with(|c: char| c.is_ascii_alphabetic()),
};
if !needs_prefix {
return result;
}
let prefix = match start {
IdentifierStart::Underscore => '_',
IdentifierStart::Letter => {
if result
.chars()
.find(char::is_ascii_alphabetic)
.is_some_and(char::is_uppercase)
{
'X'
} else {
'x'
}
}
};
result.insert(0, prefix);
result
}
pub fn seaorm_module_name(name: &str) -> String {
sanitize_identifier(name, IdentifierStart::Letter)
}
pub fn pluralize(name: &str) -> String {
if name.ends_with('s') || name.ends_with("es") {
name.to_string()
} else if name.ends_with('y')
&& !name.ends_with("ay")
&& !name.ends_with("ey")
&& !name.ends_with("oy")
&& !name.ends_with("uy")
{
format!("{}ies", name.strip_suffix('y').unwrap_or(name))
} else {
format!("{name}s")
}
}
pub fn build_index_name<T: AsRef<str>>(table: &str, columns: &[T], key: Option<&str>) -> String {
build_constraint_name("ix_", table, columns, key)
}
pub fn build_unique_constraint_name<T: AsRef<str>>(
table: &str,
columns: &[T],
key: Option<&str>,
) -> String {
build_constraint_name("uq_", table, columns, key)
}
pub fn build_foreign_key_name<T: AsRef<str>>(
table: &str,
columns: &[T],
key: Option<&str>,
) -> String {
build_constraint_name("fk_", table, columns, key)
}
fn build_constraint_name<T: AsRef<str>>(
prefix: &str,
table: &str,
columns: &[T],
key: Option<&str>,
) -> String {
if let Some(k) = key {
let mut out = String::with_capacity(prefix.len() + table.len() + 2 + k.len());
out.push_str(prefix);
out.push_str(table);
out.push_str("__");
out.push_str(k);
out
} else {
let cols_capacity: usize = columns
.iter()
.map(|c| c.as_ref().len() + 1)
.sum::<usize>()
.saturating_sub(1);
let mut out = String::with_capacity(prefix.len() + table.len() + 2 + cols_capacity);
out.push_str(prefix);
out.push_str(table);
out.push_str("__");
write_sorted_columns(&mut out, columns);
out
}
}
fn write_sorted_columns<T: AsRef<str>>(out: &mut String, columns: &[T]) {
let mut sorted: Vec<&str> = columns.iter().map(AsRef::as_ref).collect();
sorted.sort_unstable();
for (i, c) in sorted.iter().enumerate() {
if i > 0 {
out.push('_');
}
out.push_str(c);
}
}
pub fn build_check_constraint_name(table: &str, column: &str) -> String {
let mut out = String::with_capacity(4 + table.len() + 2 + column.len());
out.push_str("chk_");
out.push_str(table);
out.push_str("__");
out.push_str(column);
out
}
pub fn build_enum_type_name(table: &str, enum_name: &str) -> String {
let mut out = String::with_capacity(table.len() + 1 + enum_name.len());
out.push_str(table);
out.push('_');
out.push_str(enum_name);
out
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
use rstest::rstest;
#[test]
fn test_extract_relation_prefix_default_fk() {
assert_eq!(extract_relation_prefix("user_id", "user", "id"), "");
assert_eq!(extract_relation_prefix("org_id", "org", "id"), "");
assert_eq!(extract_relation_prefix("post_id", "post", "id"), "");
}
#[test]
fn test_extract_relation_prefix_different_ref_column() {
assert_eq!(extract_relation_prefix("user_idx", "user", "idx"), "");
assert_eq!(extract_relation_prefix("user_pk", "user", "pk"), "");
assert_eq!(extract_relation_prefix("user_key", "user", "key"), "");
}
#[test]
fn test_extract_relation_prefix_semantic_prefix() {
assert_eq!(
extract_relation_prefix("answered_by_user_id", "user", "id"),
"answered_by"
);
assert_eq!(
extract_relation_prefix("created_by_user_id", "user", "id"),
"created_by"
);
assert_eq!(
extract_relation_prefix("target_user_id", "user", "id"),
"target"
);
assert_eq!(
extract_relation_prefix("parent_org_id", "org", "id"),
"parent"
);
}
#[test]
fn test_extract_relation_prefix_role_fk() {
assert_eq!(extract_relation_prefix("author_id", "user", "id"), "author");
assert_eq!(extract_relation_prefix("owner_id", "user", "id"), "owner");
assert_eq!(
extract_relation_prefix("creator_id", "user", "id"),
"creator"
);
}
#[test]
fn test_extract_relation_prefix_no_suffix() {
assert_eq!(extract_relation_prefix("user", "user", "id"), "");
assert_eq!(extract_relation_prefix("admin_user", "user", "id"), "admin");
}
#[test]
fn test_extract_relation_prefix_unicode_table_name() {
assert_eq!(
extract_relation_prefix("작성자_한국어테이블_id", "한국어테이블", "id"),
"작성자"
);
assert_eq!(extract_relation_prefix("📊_stats_id", "📊_stats", "id"), "");
}
#[test]
fn test_pluralize_unicode_name_ending_with_ascii_y() {
assert_eq!(pluralize("café_category"), "café_categories");
}
proptest! {
#[test]
fn extract_relation_prefix_does_not_panic_on_unicode(
table in proptest::collection::vec(any::<char>(), 0..30).prop_map(|v| v.into_iter().collect::<String>()),
prefix in proptest::collection::vec(any::<char>(), 0..30).prop_map(|v| v.into_iter().collect::<String>())
) {
let fk_column = format!("{prefix}_{table}_id");
let _ = extract_relation_prefix(&fk_column, &table, "id");
}
}
#[test]
fn test_build_reverse_relation_field_name_single_fk() {
assert_eq!(
build_reverse_relation_field_name(
&["user_id".into()],
"user",
"inquiry",
"id",
false,
false
),
"inquiries"
);
assert_eq!(
build_reverse_relation_field_name(
&["author_id".into()],
"user",
"comment",
"id",
false,
false
),
"comments"
);
}
#[test]
fn test_build_reverse_relation_field_name_multiple_fks() {
assert_eq!(
build_reverse_relation_field_name(
&["user_id".into()],
"user",
"inquiry",
"id",
true,
false
),
"inquiries"
);
assert_eq!(
build_reverse_relation_field_name(
&["answered_by_user_id".into()],
"user",
"inquiry",
"id",
true,
false
),
"answered_by_inquiries"
);
}
#[test]
fn test_build_reverse_relation_field_name_one_to_one() {
assert_eq!(
build_reverse_relation_field_name(
&["user_id".into()],
"user",
"profile",
"id",
false,
true
),
"profile"
);
assert_eq!(
build_reverse_relation_field_name(
&["backup_user_id".into()],
"user",
"settings",
"id",
true,
true
),
"backup_settings"
);
}
#[test]
fn test_build_relation_enum_name() {
assert_eq!(build_relation_enum_name(&[], "user", "id"), "");
assert_eq!(
build_relation_enum_name(&["user_id".into()], "user", "id"),
""
);
assert_eq!(
build_relation_enum_name(&["answered_by_user_id".into()], "user", "id"),
"AnsweredBy"
);
assert_eq!(
build_relation_enum_name(&["target_user_id".into()], "user", "id"),
"Target"
);
assert_eq!(
build_relation_enum_name(&["author_id".into()], "user", "id"),
"Author"
);
}
#[test]
fn test_to_pascal_case() {
assert_eq!(to_pascal_case("hello_world"), "HelloWorld");
assert_eq!(to_pascal_case("answered_by"), "AnsweredBy");
assert_eq!(to_pascal_case("user"), "User");
assert_eq!(to_pascal_case("hello-world"), "HelloWorld");
assert_eq!(to_pascal_case(""), "");
}
#[rstest]
#[case::lowercase("pending", "PENDING")]
#[case::snake_case("not_started", "NOT_STARTED")]
#[case::camel_case("inProgress", "IN_PROGRESS")]
#[case::kebab_case("order-status", "ORDER_STATUS")]
#[case::empty("", "")]
#[case::already_screaming("ERROR_LEVEL", "ERROR_LEVEL")]
#[case::acronym_with_digits("HTTP_500", "HTTP_500")]
#[case::trailing_separator("status-", "STATUS")]
#[case::leading_digit("1critical", "1CRITICAL")]
fn test_to_screaming_snake_case(#[case] input: &str, #[case] expected: &str) {
assert_eq!(to_screaming_snake_case(input), expected);
}
#[rstest]
#[case::already_valid("email", IdentifierStart::Underscore, "email")]
#[case::internal_digits("table_99_ok", IdentifierStart::Underscore, "table_99_ok")]
#[case::hyphen("user-id", IdentifierStart::Underscore, "user_id")]
#[case::space("user id", IdentifierStart::Underscore, "user_id")]
#[case::non_ascii("사용자", IdentifierStart::Underscore, "___")]
#[case::leading_digit_underscore("1st_place", IdentifierStart::Underscore, "_1st_place")]
#[case::leading_digit_letter("1st_place", IdentifierStart::Letter, "x1st_place")]
#[case::leading_digit_screaming("1CRITICAL", IdentifierStart::Letter, "X1CRITICAL")]
#[case::existing_underscore_letter("_private", IdentifierStart::Letter, "x_private")]
#[case::existing_underscore_ok("_private", IdentifierStart::Underscore, "_private")]
#[case::empty_underscore("", IdentifierStart::Underscore, "_")]
#[case::empty_letter("", IdentifierStart::Letter, "x")]
fn test_sanitize_identifier(
#[case] input: &str,
#[case] start: IdentifierStart,
#[case] expected: &str,
) {
assert_eq!(sanitize_identifier(input, start), expected);
}
#[test]
fn test_pluralize() {
assert_eq!(pluralize("inquiry"), "inquiries");
assert_eq!(pluralize("category"), "categories");
assert_eq!(pluralize("comment"), "comments");
assert_eq!(pluralize("user"), "users");
assert_eq!(pluralize("status"), "status");
assert_eq!(pluralize("address"), "address");
}
type BuildFn = fn(&str, &[&'static str], Option<&str>) -> String;
type BuildStringFn = fn(&str, &[String], Option<&str>) -> String;
#[rstest]
#[case::index_with_key(build_index_name, "users", &["email"][..], Some("email_idx"), "ix_users__email_idx")]
#[case::index_without_key(build_index_name, "users", &["email"][..], None, "ix_users__email")]
#[case::index_multiple_columns(build_index_name, "users", &["first_name", "last_name"][..], None, "ix_users__first_name_last_name")]
#[case::unique_with_key(build_unique_constraint_name, "users", &["email"][..], Some("email_unique"), "uq_users__email_unique")]
#[case::unique_without_key(build_unique_constraint_name, "users", &["email"][..], None, "uq_users__email")]
#[case::foreign_key_with_key(build_foreign_key_name, "posts", &["user_id"][..], Some("fk_user"), "fk_posts__fk_user")]
#[case::foreign_key_without_key(build_foreign_key_name, "posts", &["user_id"][..], None, "fk_posts__user_id")]
fn constraint_name_builders_render_expected(
#[case] build: BuildFn,
#[case] table: &str,
#[case] columns: &[&'static str],
#[case] key: Option<&str>,
#[case] expected: &str,
) {
assert_eq!(build(table, columns, key), expected);
}
#[rstest]
#[case::index(build_index_name, "users", &["last_name", "first_name"][..])]
#[case::unique(build_unique_constraint_name, "users", &["last_name", "first_name"][..])]
#[case::foreign_key(build_foreign_key_name, "posts", &["tenant_id", "user_id"][..])]
fn constraint_name_builders_sort_str_columns_for_deterministic_name(
#[case] build: BuildFn,
#[case] table: &str,
#[case] columns: &[&'static str],
) {
let mut reversed = columns.to_vec();
reversed.reverse();
assert_eq!(build(table, columns, None), build(table, &reversed, None));
}
#[rstest]
#[case::index(build_index_name, "users", &["last_name", "first_name"][..])]
#[case::unique(build_unique_constraint_name, "order_items", &["product_id", "order_id"][..])]
#[case::foreign_key(build_foreign_key_name, "memberships", &["tenant_id", "account_id"][..])]
fn constraint_name_builders_sort_string_columns_for_deterministic_name(
#[case] build: BuildStringFn,
#[case] table: &str,
#[case] columns: &[&str],
) {
let columns: Vec<String> = columns.iter().map(ToString::to_string).collect();
let mut reversed = columns.clone();
reversed.reverse();
assert_eq!(build(table, &columns, None), build(table, &reversed, None));
}
#[test]
fn test_build_check_constraint_name() {
assert_eq!(
build_check_constraint_name("users", "status"),
"chk_users__status"
);
}
#[test]
fn test_build_enum_type_name() {
assert_eq!(build_enum_type_name("users", "status"), "users_status");
}
#[test]
fn test_build_enum_type_name_with_existing_prefix() {
assert_eq!(
build_enum_type_name("users", "user_status"),
"users_user_status"
);
}
#[test]
fn test_build_enum_type_name_prevents_conflicts() {
assert_eq!(build_enum_type_name("users", "gender"), "users_gender");
assert_eq!(
build_enum_type_name("employees", "gender"),
"employees_gender"
);
assert_eq!(build_enum_type_name("orders", "status"), "orders_status");
assert_eq!(
build_enum_type_name("shipments", "status"),
"shipments_status"
);
}
}