Skip to content

Commit

Permalink
add foreign_uuid method
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Sep 2, 2023
1 parent 7a51ba2 commit 877f0ef
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions ensemble/src/migrations/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ impl Table {
}
}

/// Create a foreign UUID column for the given model.
pub fn foreign_uuid(&mut self, name: &str) -> ForeignIndex {
Column::new(name.to_string(), Type::Uuid, self.sender.clone()).uuid(true);
let index = ForeignIndex::new(name.to_string(), self.name.clone(), self.sender.clone());

// if the column name is of the form `resource_id`, we extract and set the table name and foreign column name
if let Some((resource, column)) = name.split_once('_') {
index.on(&resource.to_plural()).references(column)
} else {
index
}
}

/// Add nullable creation and update timestamps to the table.
pub fn timestamps(&mut self) {
self.timestamp("created_at")
Expand Down

0 comments on commit 877f0ef

Please sign in to comment.