aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_db/src/lib.rs
blob: 07098b35de069c555207cc0200e1fd8a3c208898 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

//! Database utilities, to interact with the database.

#![feature(
  type_ascription,
  hash_set_entry,
  type_name_of_val,
  decl_macro,
  proc_macro_hygiene
)]
#![deny(
  warnings,
  nonstandard_style,
  unused,
  future_incompatible,
  rust_2018_idioms,
  unsafe_code
)]
#![deny(clippy::all, clippy::nursery, clippy::pedantic)]
#![recursion_limit = "128"]
#![doc(
  html_logo_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png",
  html_favicon_url = "https://raw.githubusercontent.com/Whirlsplash/assets/master/Whirl.png"
)]

#[macro_use]
extern crate diesel;

mod models;
mod schema;

use diesel::prelude::*;

// use crate::db::models::*;

/// Establish a connection to the `SQLite` database.
///
/// # Panics
/// - May panic if the database URL is inaccessible.
#[must_use]
pub fn establish_connection() -> SqliteConnection {
  let database_url =
    std::env::var("DATABASE_URL").unwrap_or_else(|_| ".whirl/db.sqlite3".to_string());
  SqliteConnection::establish(&database_url)
    .unwrap_or_else(|_| panic!("error connecting to {}", database_url))
}

/// Only works if you have a valid database already setup!
#[cfg(test)]
#[test]
#[ignore]
fn show_serials() {
  use crate::{models::SerialNumber, schema::serial_numbers::dsl::*};

  dotenv::dotenv().ok();

  let results = serial_numbers
    .limit(5)
    .load::<SerialNumber>(&establish_connection())
    .expect("error loading serial numbers table");

  println!("found {} results", results.len());
  for result in results {
    println!("{}", result.user_name);
  }
}