|
| 1 | +//! Naming conventions and helpers for vespertide database schema management. |
| 2 | +//! |
| 3 | +//! This crate provides consistent naming functions for database objects like |
| 4 | +//! indexes, constraints, and foreign keys. It has no dependencies and can be |
| 5 | +//! used by any other vespertide crate. |
| 6 | +
|
| 7 | +/// Generate index name from table name, columns, and optional user-provided key. |
| 8 | +/// Always includes table name to avoid conflicts across tables. |
| 9 | +/// Uses double underscore to separate table name from the rest. |
| 10 | +/// Format: ix_{table}__{key} or ix_{table}__{col1}_{col2}... |
| 11 | +pub fn build_index_name(table: &str, columns: &[String], key: Option<&str>) -> String { |
| 12 | + match key { |
| 13 | + Some(k) => format!("ix_{}__{}", table, k), |
| 14 | + None => format!("ix_{}__{}", table, columns.join("_")), |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/// Generate unique constraint name from table name, columns, and optional user-provided key. |
| 19 | +/// Always includes table name to avoid conflicts across tables. |
| 20 | +/// Uses double underscore to separate table name from the rest. |
| 21 | +/// Format: uq_{table}__{key} or uq_{table}__{col1}_{col2}... |
| 22 | +pub fn build_unique_constraint_name(table: &str, columns: &[String], key: Option<&str>) -> String { |
| 23 | + match key { |
| 24 | + Some(k) => format!("uq_{}__{}", table, k), |
| 25 | + None => format!("uq_{}__{}", table, columns.join("_")), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/// Generate foreign key constraint name from table name, columns, and optional user-provided key. |
| 30 | +/// Always includes table name to avoid conflicts across tables. |
| 31 | +/// Uses double underscore to separate table name from the rest. |
| 32 | +/// Format: fk_{table}__{key} or fk_{table}__{col1}_{col2}... |
| 33 | +pub fn build_foreign_key_name(table: &str, columns: &[String], key: Option<&str>) -> String { |
| 34 | + match key { |
| 35 | + Some(k) => format!("fk_{}__{}", table, k), |
| 36 | + None => format!("fk_{}__{}", table, columns.join("_")), |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/// Generate CHECK constraint name for SQLite enum column. |
| 41 | +/// Uses double underscore to separate table name from the rest. |
| 42 | +/// Format: chk_{table}__{column} |
| 43 | +pub fn build_check_constraint_name(table: &str, column: &str) -> String { |
| 44 | + format!("chk_{}__{}", table, column) |
| 45 | +} |
| 46 | + |
| 47 | +#[cfg(test)] |
| 48 | +mod tests { |
| 49 | + use super::*; |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn test_build_index_name_with_key() { |
| 53 | + assert_eq!( |
| 54 | + build_index_name("users", &["email".into()], Some("email_idx")), |
| 55 | + "ix_users__email_idx" |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + #[test] |
| 60 | + fn test_build_index_name_without_key() { |
| 61 | + assert_eq!( |
| 62 | + build_index_name("users", &["email".into()], None), |
| 63 | + "ix_users__email" |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + #[test] |
| 68 | + fn test_build_index_name_multiple_columns() { |
| 69 | + assert_eq!( |
| 70 | + build_index_name("users", &["first_name".into(), "last_name".into()], None), |
| 71 | + "ix_users__first_name_last_name" |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + #[test] |
| 76 | + fn test_build_unique_constraint_name_with_key() { |
| 77 | + assert_eq!( |
| 78 | + build_unique_constraint_name("users", &["email".into()], Some("email_unique")), |
| 79 | + "uq_users__email_unique" |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + #[test] |
| 84 | + fn test_build_unique_constraint_name_without_key() { |
| 85 | + assert_eq!( |
| 86 | + build_unique_constraint_name("users", &["email".into()], None), |
| 87 | + "uq_users__email" |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + #[test] |
| 92 | + fn test_build_foreign_key_name_with_key() { |
| 93 | + assert_eq!( |
| 94 | + build_foreign_key_name("posts", &["user_id".into()], Some("fk_user")), |
| 95 | + "fk_posts__fk_user" |
| 96 | + ); |
| 97 | + } |
| 98 | + |
| 99 | + #[test] |
| 100 | + fn test_build_foreign_key_name_without_key() { |
| 101 | + assert_eq!( |
| 102 | + build_foreign_key_name("posts", &["user_id".into()], None), |
| 103 | + "fk_posts__user_id" |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_build_check_constraint_name() { |
| 109 | + assert_eq!( |
| 110 | + build_check_constraint_name("users", "status"), |
| 111 | + "chk_users__status" |
| 112 | + ); |
| 113 | + } |
| 114 | +} |
0 commit comments