custom_arrays/
lib.rs
1use diesel::r2d2::R2D2Connection;
2use diesel::PgConnection;
3use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
4use std::error::Error;
5
6pub mod model;
7mod schema;
8pub type Connection = PgConnection;
13
14pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
15
16pub fn run_db_migration(
28 conn: &mut Connection,
29) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
30 match conn.ping() {
32 Ok(_) => {}
33 Err(e) => {
34 eprint!("[run_db_migration]: Error connecting to database: {}", e);
35 return Err(Box::new(e));
36 }
37 }
38
39 match conn.run_pending_migrations(MIGRATIONS) {
41 Ok(_) => Ok(()),
42 Err(e) => {
43 eprint!("[run_db_migration]: Error migrating database: {}", e);
44 Err(e)
45 }
46 }
47}
48
49pub fn revert_db_migration(
61 conn: &mut Connection,
62) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
63 if conn.ping().is_ok() {
65 } else if let Err(e) = conn.ping() {
66 eprint!("[pg_cmdb]: Error connecting to database: {}", e);
67 return Err(Box::new(e));
68 }
69
70 match conn.revert_all_migrations(MIGRATIONS) {
72 Ok(_) => Ok(()),
73 Err(e) => {
74 eprint!("[pg_cmdb]: Error reverting database migrations: {}", e);
75 Err(e)
76 }
77 }
78}