Skip to main content

sqlx_cli/
database.rs

1use crate::opt::{ConnectOpts, MigrationSourceOpt};
2use crate::{migrate, Config};
3use console::{style, Term};
4use dialoguer::Confirm;
5use sqlx::any::Any;
6use sqlx::migrate::MigrateDatabase;
7use std::{io, mem};
8use tokio::task;
9
10pub async fn create(connect_opts: &ConnectOpts) -> anyhow::Result<()> {
11    // NOTE: only retry the idempotent action.
12    // We're assuming that if this succeeds, then any following operations should also succeed.
13    let exists = crate::retry_connect_errors(connect_opts, Any::database_exists).await?;
14
15    if !exists {
16        #[cfg(feature = "_sqlite")]
17        sqlx::sqlite::CREATE_DB_WAL.store(
18            connect_opts.sqlite_create_db_wal,
19            std::sync::atomic::Ordering::Release,
20        );
21
22        Any::create_database(connect_opts.expect_db_url()?).await?;
23    }
24
25    Ok(())
26}
27
28pub async fn drop(connect_opts: &ConnectOpts, confirm: bool, force: bool) -> anyhow::Result<()> {
29    if confirm && !ask_to_continue_drop(connect_opts.expect_db_url()?.to_owned()).await {
30        return Ok(());
31    }
32
33    // NOTE: only retry the idempotent action.
34    // We're assuming that if this succeeds, then any following operations should also succeed.
35    let exists = crate::retry_connect_errors(connect_opts, Any::database_exists).await?;
36
37    if exists {
38        if force {
39            Any::force_drop_database(connect_opts.expect_db_url()?).await?;
40        } else {
41            Any::drop_database(connect_opts.expect_db_url()?).await?;
42        }
43    }
44
45    Ok(())
46}
47
48pub async fn reset(
49    config: &Config,
50    migration_source: &MigrationSourceOpt,
51    connect_opts: &ConnectOpts,
52    confirm: bool,
53    force: bool,
54) -> anyhow::Result<()> {
55    drop(connect_opts, confirm, force).await?;
56    setup(config, migration_source, connect_opts).await
57}
58
59pub async fn setup(
60    config: &Config,
61    migration_source: &MigrationSourceOpt,
62    connect_opts: &ConnectOpts,
63) -> anyhow::Result<()> {
64    create(connect_opts).await?;
65    migrate::run(
66        config,
67        migration_source,
68        connect_opts,
69        false,
70        false,
71        None,
72        false,
73    )
74    .await
75}
76
77async fn ask_to_continue_drop(db_url: String) -> bool {
78    // If the setup operation is cancelled while we are waiting for the user to decide whether
79    // or not to drop the database, this will restore the terminal's cursor to its normal state.
80    struct RestoreCursorGuard {
81        disarmed: bool,
82    }
83
84    impl Drop for RestoreCursorGuard {
85        fn drop(&mut self) {
86            if !self.disarmed {
87                Term::stderr().show_cursor().unwrap()
88            }
89        }
90    }
91
92    let mut guard = RestoreCursorGuard { disarmed: false };
93
94    let decision_result = task::spawn_blocking(move || {
95        Confirm::new()
96            .with_prompt(format!("Drop database at {}?", style(&db_url).cyan()))
97            .wait_for_newline(true)
98            .default(false)
99            .show_default(true)
100            .interact()
101    })
102    .await
103    .expect("Confirm thread panicked");
104    match decision_result {
105        Ok(decision) => {
106            guard.disarmed = true;
107            decision
108        }
109        Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
110            // Sometimes CTRL + C causes this error to be returned
111            mem::drop(guard);
112            false
113        }
114        Err(err) => {
115            mem::drop(guard);
116            panic!("Confirm dialog failed with {err}")
117        }
118    }
119}