use glacier::db::migration::{Migration, Schema};
use glacier::db::{self, BelongsTo, HasMany, Repo};
use glacier::prelude::*;
#[glacier::model]
pub struct Shelf {
name: String,
#[glacier(has_many, dependent = "destroy")]
books: HasMany<Book>,
#[glacier(has_many, dependent = "delete_all")]
labels: HasMany<Label>,
}
#[glacier::model]
pub struct Book {
shelf_id: i64,
title: String,
#[glacier(has_many, dependent = "destroy")]
notes: HasMany<Note>,
}
#[glacier::model]
pub struct Note {
book_id: i64,
text: String,
}
#[glacier::model]
pub struct Sticker {
book_id: i64,
#[glacier(belongs_to, touch)]
book: BelongsTo<Book>,
}
#[glacier::model]
pub struct Label {
shelf_id: i64,
text: String,
}
fn schema(s: &mut Schema) {
s.create_table("shelves", |t| {
t.string("name").not_null();
t.timestamps();
});
s.create_table("stickers", |t| {
t.bigint("book_id").not_null();
t.timestamps();
});
for (table, parent, column) in [
("books", "shelf_id", "title"),
("notes", "book_id", "text"),
("labels", "shelf_id", "text"),
] {
s.create_table(table, |t| {
t.bigint(parent).not_null();
t.string(column).not_null();
t.timestamps();
});
}
}
async fn shelf(name: &str) -> Shelf {
let mut shelf = Shelf::new().tap(|s| s.name = name.into());
assert!(shelf.save().await.unwrap());
for title in ["Dune", "Emma"] {
let mut book = Book::new().tap(|b| {
b.shelf_id = shelf.id;
b.title = title.into();
});
assert!(book.save().await.unwrap());
let mut note = Note::new().tap(|n| {
n.book_id = book.id;
n.text = "good".into();
});
assert!(note.save().await.unwrap());
}
let mut label = Label::new().tap(|l| {
l.shelf_id = shelf.id;
l.text = "fiction".into();
});
assert!(label.save().await.unwrap());
shelf
}
#[tokio::test]
async fn destroying_a_parent_removes_its_dependents() {
let mut urls = vec!["sqlite::memory:".to_owned()];
if let Ok(url) = std::env::var("GLACIER_TEST_POSTGRES_URL") {
urls.push(url);
}
for url in urls {
let repo = Repo::connect(&url).await.unwrap();
db::sandbox(&repo, async {
for statement in Migration::new("m1", schema, |_| {}).up_sql(repo.dialect()) {
db::sql(&statement).execute().await.unwrap();
}
let kept = shelf("Kept").await;
let gone = shelf("Gone").await;
gone.destroy().await.unwrap();
assert_eq!(Shelf::count().await.unwrap(), 1);
assert_eq!(
Book::count().await.unwrap(),
2,
"its books, through destroy"
);
assert_eq!(Note::count().await.unwrap(), 2, "and their notes");
assert_eq!(Label::count().await.unwrap(), 1, "its labels, deleted");
assert!(
Book::filter(|b| b.shelf_id.eq(kept.id))
.count()
.await
.unwrap()
== 2,
"the other shelf keeps its books"
);
let destroyed = Shelf::all().destroy_all().await.unwrap();
assert_eq!(destroyed, 1);
for count in [
Shelf::count().await.unwrap(),
Book::count().await.unwrap(),
Note::count().await.unwrap(),
Label::count().await.unwrap(),
] {
assert_eq!(count, 0);
}
})
.await
.unwrap();
}
}
#[tokio::test]
async fn writes_touch_the_parent() {
let mut urls = vec!["sqlite::memory:".to_owned()];
if let Ok(url) = std::env::var("GLACIER_TEST_POSTGRES_URL") {
urls.push(url);
}
for url in urls {
let repo = Repo::connect(&url).await.unwrap();
db::sandbox(&repo, async {
for statement in Migration::new("m1", schema, |_| {}).up_sql(repo.dialect()) {
db::sql(&statement).execute().await.unwrap();
}
shelf("Shelf").await;
let book = Book::first().await.unwrap().unwrap();
let long_ago = "2001-01-01 00:00:00";
let age = || async {
db::sql(&format!("UPDATE books SET updated_at = '{long_ago}'"))
.execute()
.await
.unwrap();
};
let touched = || async { Book::find(book.id).await.unwrap().updated_at.year() > 2001 };
age().await;
let mut sticker = Sticker::new().tap(|s| s.book_id = book.id);
assert!(sticker.save().await.unwrap());
assert!(touched().await, "creating touches the book");
age().await;
sticker.destroy().await.unwrap();
assert!(touched().await, "destroying touches it too");
})
.await
.unwrap();
}
}