| // Copyright 2022 The casbin Authors. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| use diesel::{ |
| pg::PgConnection, |
| r2d2::{self, ConnectionManager}, |
| }; |
| use std::time::Duration; |
| |
| embed_migrations!(); |
| |
| pub type Connection = PgConnection; |
| pub type Pool = r2d2::Pool<ConnectionManager<Connection>>; |
| |
| pub fn migrate_and_config_db(url: &str, pool_size: u32) -> Pool { |
| info!("Migrating and configurating database..."); |
| let manager = ConnectionManager::<Connection>::new(url); |
| let pool = r2d2::Pool::builder() |
| .connection_timeout(Duration::from_secs(10)) |
| .max_size(pool_size) |
| .build(manager) |
| .expect("Failed to create pool."); |
| embedded_migrations::run(&pool.get().expect("Failed to migrate.")).expect("Failed to migrate."); |
| |
| pool |
| } |