syn2mas/mas_writer/
checks.rs1use thiserror::Error;
12use thiserror_ext::ContextInto;
13use tracing::Instrument as _;
14
15use super::{MAS_TABLES_AFFECTED_BY_MIGRATION, is_syn2mas_in_progress, locking::LockedMasDatabase};
16
17#[derive(Debug, Error, ContextInto)]
18pub enum Error {
19 #[error("the MAS database is not empty: rows found in at least `{table}`")]
20 MasDatabaseNotEmpty { table: &'static str },
21
22 #[error("query against {table} failed — is this actually a MAS database?")]
23 MaybeNotMas {
24 #[source]
25 source: sqlx::Error,
26 table: &'static str,
27 },
28
29 #[error(transparent)]
30 Sqlx(#[from] sqlx::Error),
31
32 #[error("unable to check if syn2mas is already in progress")]
33 UnableToCheckInProgress(#[source] super::Error),
34}
35
36#[tracing::instrument(name = "syn2mas.mas_pre_migration_checks", skip_all)]
51pub async fn mas_pre_migration_checks(mas_connection: &mut LockedMasDatabase) -> Result<(), Error> {
52 if is_syn2mas_in_progress(mas_connection.as_mut())
53 .await
54 .map_err(Error::UnableToCheckInProgress)?
55 {
56 return Ok(());
58 }
59
60 for &table in MAS_TABLES_AFFECTED_BY_MIGRATION {
64 let query = format!("SELECT 1 AS dummy FROM {table} LIMIT 1");
65 let span = tracing::info_span!("db.query", db.query.text = query);
66 let row_present = sqlx::query(&query)
67 .fetch_optional(mas_connection.as_mut())
68 .instrument(span)
69 .await
70 .into_maybe_not_mas(table)?
71 .is_some();
72
73 if row_present {
74 return Err(Error::MasDatabaseNotEmpty { table });
75 }
76 }
77
78 Ok(())
79}