database_migration/
config.rs

1use std::borrow::Cow;
2use std::path::Path;
3
4pub const DEFAULT_MIGRATIONS_FOLDER: &str = "migrations";
5pub const DEFAULT_MIGRATIONS_TABLE: &str = "migrations";
6
7pub const MIGRATION_KEY_FORMAT_STR: &str = "%Y%m%d_%H%M%S";
8
9#[must_use]
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct RunnerConfig<'a> {
12    pub migrations_folder: Cow<'a, Path>,
13    pub migrations_table: Cow<'a, str>,
14    pub ignore_checksum: bool,
15    pub ignore_order: bool,
16}
17
18impl Default for RunnerConfig<'_> {
19    fn default() -> Self {
20        Self {
21            migrations_folder: Path::new(DEFAULT_MIGRATIONS_FOLDER).into(),
22            migrations_table: DEFAULT_MIGRATIONS_TABLE.into(),
23            ignore_checksum: false,
24            ignore_order: false,
25        }
26    }
27}
28
29impl<'a> RunnerConfig<'a> {
30    pub fn with_migrations_folder(mut self, migrations_folder: impl Into<Cow<'a, Path>>) -> Self {
31        self.migrations_folder = migrations_folder.into();
32        self
33    }
34
35    pub fn with_migrations_table(mut self, migrations_table: impl Into<Cow<'a, str>>) -> Self {
36        self.migrations_table = migrations_table.into();
37        self
38    }
39
40    pub const fn with_ignore_checksum(mut self, ignore_checksum: bool) -> Self {
41        self.ignore_checksum = ignore_checksum;
42        self
43    }
44
45    pub const fn with_ignore_order(mut self, ignore_order: bool) -> Self {
46        self.ignore_order = ignore_order;
47        self
48    }
49}
50
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum DbAuthLevel {
53    Root,
54    Namespace,
55    Database,
56}
57
58#[must_use]
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub struct DbClientConfig<'a> {
61    /// Address of the database instance.
62    ///
63    /// Examples:
64    /// - `"ws://localhost:8000"`
65    /// - `"wss://cloud.surrealdb.com"`
66    ///
67    /// Default: `"ws://localhost:8000"`
68    pub address: Cow<'a, str>,
69
70    /// Namespace to use on the database instance.
71    ///
72    /// Default: `"test"`
73    pub namespace: Cow<'a, str>,
74
75    /// Database to use inside the database instance.
76    ///
77    /// Default: `"test"`
78    pub database: Cow<'a, str>,
79
80    /// The kind of the system user used for authentication.
81    ///
82    /// Default: `Root`
83    pub auth_level: DbAuthLevel,
84
85    /// Username used to authenticate to the database instance.
86    ///
87    /// Default: `"root"`
88    pub username: Cow<'a, str>,
89
90    /// Password used to authenticate to the database instance.
91    ///
92    /// Default: `"root"`
93    pub password: Cow<'a, str>,
94
95    /// Capacity of the channels to the database.
96    ///
97    /// Example:
98    /// - `0` (= unbounded)
99    /// - `200`
100    ///
101    /// Default: `20`
102    pub capacity: usize,
103}
104
105impl Default for DbClientConfig<'_> {
106    fn default() -> Self {
107        Self {
108            address: "ws://localhost:8000".into(),
109            namespace: "test".into(),
110            database: "test".into(),
111            auth_level: DbAuthLevel::Root,
112            username: "root".into(),
113            password: "root".into(),
114            capacity: 20,
115        }
116    }
117}
118
119impl<'a> DbClientConfig<'a> {
120    pub fn with_address(mut self, address: impl Into<Cow<'a, str>>) -> Self {
121        self.address = address.into();
122        self
123    }
124
125    pub fn with_namespace(mut self, namespace: impl Into<Cow<'a, str>>) -> Self {
126        self.namespace = namespace.into();
127        self
128    }
129
130    pub fn with_database(mut self, database: impl Into<Cow<'a, str>>) -> Self {
131        self.database = database.into();
132        self
133    }
134
135    pub const fn with_auth_level(mut self, auth_level: DbAuthLevel) -> Self {
136        self.auth_level = auth_level;
137        self
138    }
139
140    pub fn with_username(mut self, username: impl Into<Cow<'a, str>>) -> Self {
141        self.username = username.into();
142        self
143    }
144
145    pub fn with_password(mut self, password: impl Into<Cow<'a, str>>) -> Self {
146        self.password = password.into();
147        self
148    }
149
150    pub const fn with_capacity(mut self, capacity: usize) -> Self {
151        self.capacity = capacity;
152        self
153    }
154}