yang_base/database/
initializer.rs1use super::AnyDatabase;
7use crate::datasource::DatasourceId;
8use crate::error::BaseError;
9use crate::table::TableDefinition;
10use crate::tools::Tools;
11use std::collections::BTreeMap;
12
13pub async fn preflight_table_definitions_grouped_outcomes(
18 tools: &Tools,
19 definitions: &[&TableDefinition],
20) -> BTreeMap<DatasourceId, Result<super::SchemaPreflightReport, BaseError>> {
21 let mut outcomes = BTreeMap::new();
22 for (source, group) in group_definitions(definitions) {
23 let outcome = match tools.database_named(&source) {
24 Ok(database) => {
25 DatabaseInitializer::from_any(database.clone())
26 .preflight_table_definitions(&group)
27 .await
28 }
29 Err(error) => Err(error),
30 };
31 outcomes.insert(source, outcome);
32 }
33 outcomes
34}
35
36pub async fn preflight_table_definitions_grouped(
38 tools: &Tools,
39 definitions: &[&TableDefinition],
40) -> Result<BTreeMap<DatasourceId, super::SchemaPreflightReport>, BaseError> {
41 collect_outcomes(preflight_table_definitions_grouped_outcomes(tools, definitions).await)
42}
43
44pub async fn sync_table_definitions_grouped_outcomes(
49 tools: &Tools,
50 definitions: &[&TableDefinition],
51) -> BTreeMap<DatasourceId, Result<super::SchemaSyncReport, BaseError>> {
52 let mut outcomes = BTreeMap::new();
53 for (source, group) in group_definitions(definitions) {
54 let outcome = match tools.database_named(&source) {
55 Ok(database) => {
56 DatabaseInitializer::from_any(database.clone())
57 .sync_table_definitions(&group)
58 .await
59 }
60 Err(error) => Err(error),
61 };
62 outcomes.insert(source, outcome);
63 }
64 outcomes
65}
66
67pub async fn sync_table_definitions_grouped(
69 tools: &Tools,
70 definitions: &[&TableDefinition],
71) -> Result<BTreeMap<DatasourceId, super::SchemaSyncReport>, BaseError> {
72 collect_outcomes(sync_table_definitions_grouped_outcomes(tools, definitions).await)
73}
74
75fn collect_outcomes<T>(
76 outcomes: BTreeMap<DatasourceId, Result<T, BaseError>>,
77) -> Result<BTreeMap<DatasourceId, T>, BaseError> {
78 let mut reports = BTreeMap::new();
79 for (source, outcome) in outcomes {
80 reports.insert(source, outcome?);
81 }
82 Ok(reports)
83}
84
85fn group_definitions<'a>(
86 definitions: &[&'a TableDefinition],
87) -> BTreeMap<DatasourceId, Vec<&'a TableDefinition>> {
88 let mut groups: BTreeMap<DatasourceId, Vec<&'a TableDefinition>> = BTreeMap::new();
89 for definition in definitions {
90 groups
91 .entry(definition.datasource().clone())
92 .or_default()
93 .push(*definition);
94 }
95 groups
96}
97
98pub struct DatabaseInitializer {
100 db: AnyDatabase,
102}
103
104impl DatabaseInitializer {
105 #[cfg(feature = "mysql")]
110 pub fn new(db: yang_db::mysql::Database) -> Self {
111 Self::from_any(AnyDatabase::mysql(db))
112 }
113
114 #[cfg(feature = "postgres")]
118 pub fn new_postgres(db: yang_db::postgres::Database) -> Self {
119 Self::from_any(AnyDatabase::postgres(db))
120 }
121
122 pub fn from_any(db: AnyDatabase) -> Self {
124 Self { db }
125 }
126
127 pub(crate) fn db(&self) -> &AnyDatabase {
129 &self.db
130 }
131}