database_schema/
macros.rs

1//! This module contains a few useful macros in order to not need the builder.
2//!
3//! It provides the following macros:
4//!
5//! * `generate!` - Generate a `destination_path` file using migrations from the provided
6//! `migrations_path` folder.
7//!
8//! * `generate_using_defaults!` - Generate a `./structure.sql` file using migrations
9//! from the `./migrations` folder.
10
11/// Generate a `destination_path` file using migrations from the provided
12/// `migrations_path` folder.
13#[macro_export]
14macro_rules! generate_without_runtime {
15    ($migrations_path:expr, $structure_path:expr) => {
16        let migrations_path: ::std::path::PathBuf = $migrations_path.into();
17        let destination_path: ::std::path::PathBuf = $structure_path.into();
18        $crate::macros::__generate_within_runtime(migrations_path, destination_path)
19    };
20}
21
22/// Generate a `./structure.sql` file using migrations from the `./migrations` folder.
23#[macro_export]
24macro_rules! generate_without_runtime_using_defaults {
25    () => {
26        $crate::macros::__generate_within_runtime(
27            ::std::path::PathBuf::from("./migrations"),
28            ::std::path::PathBuf::from("./structure.sql"),
29        );
30    };
31}
32
33/// Generate a structure.sql file using migrations from the `migrations_path` folder.
34/// DO NOT USE THIS DIRECTLY. Use the `generate!` macro instead.
35#[doc(hidden)]
36pub fn __generate_within_runtime(
37    migrations_path: std::path::PathBuf,
38    destination_path: std::path::PathBuf,
39) {
40    run_runtime(migrations_path, destination_path);
41}
42
43fn run_runtime(migrations_path: std::path::PathBuf, destination_path: std::path::PathBuf) {
44    #[cfg(feature = "runtime-tokio")]
45    {
46        let rt = tokio::runtime::Runtime::new().expect("could not create tokio runtime");
47        let local = tokio::task::LocalSet::new();
48        local
49            .block_on(&rt, async {
50                crate::generate(None, migrations_path, destination_path).await
51            })
52            .expect("could not run tokio runtime");
53    }
54
55    #[cfg(feature = "runtime-async-std")]
56    async_std::task::block_on(async {
57        crate::generate(None, migrations_path, destination_path).await
58    })
59    .expect("could not run async-std runtime");
60}