wtx/database/schema_manager/
commands.rs

1#[cfg(feature = "schema-manager-dev")]
2mod clear;
3mod migrate;
4mod rollback;
5#[cfg(feature = "schema-manager-dev")]
6mod seed;
7mod validate;
8
9use crate::{
10  database::{
11    Database, DatabaseTy,
12    executor::Executor,
13    schema_manager::{DEFAULT_BATCH_SIZE, UserMigration},
14  },
15  misc::Lease,
16};
17
18/// SQL commands facade
19#[derive(Debug)]
20pub struct Commands<E> {
21  batch_size: usize,
22  pub(crate) executor: E,
23}
24
25impl<E> Commands<E>
26where
27  E: Executor,
28{
29  /// Creates a new instance from a given Backend and batch size.
30  #[inline]
31  pub fn new(batch_size: usize, executor: E) -> Self {
32    Self { batch_size, executor }
33  }
34
35  /// Creates a new instance from a given Backend.
36  ///
37  /// Batch size will default to 128.
38  #[inline]
39  pub fn with_executor(database: E) -> Self {
40    Self { batch_size: DEFAULT_BATCH_SIZE, executor: database }
41  }
42
43  /// Batch size
44  #[inline]
45  pub fn batch_size(&self) -> usize {
46    self.batch_size
47  }
48
49  #[inline]
50  fn filter_by_db<'migration, DBS, I, S>(
51    migrations: I,
52  ) -> impl Clone + Iterator<Item = &'migration UserMigration<DBS, S>>
53  where
54    DBS: Lease<[DatabaseTy]> + 'migration,
55    I: Clone + Iterator<Item = &'migration UserMigration<DBS, S>>,
56    S: Lease<str> + 'migration,
57  {
58    migrations.filter(move |m| {
59      if m.dbs().is_empty() { true } else { m.dbs().contains(&<E::Database as Database>::TY) }
60    })
61  }
62}