mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-31 05:40:58 +00:00
* feat: create `sqlx.toml` format * feat: add support for ignored_chars config to sqlx_core::migrate * chore: test ignored_chars with `U+FEFF` (ZWNBSP/BOM) https://en.wikipedia.org/wiki/Byte_order_mark * refactor: make `Config` always compiled simplifies usage while still making parsing optional for less generated code * refactor: add origin information to `Column` * feat(macros): implement `type_override` and `column_override` from `sqlx.toml` * refactor(sqlx.toml): make all keys kebab-case, create `macros.preferred-crates` * feat: make macros aware of `macros.preferred-crates` * feat: make `sqlx-cli` aware of `database-url-var` * feat: teach macros about `migrate.table-name`, `migrations-dir` * feat: teach macros about `migrate.ignored-chars` * chore: delete unused source file `sqlx-cli/src/migration.rs` * feat: teach `sqlx-cli` about `migrate.defaults` * feat: teach `sqlx-cli` about `migrate.migrations-dir` * feat: teach `sqlx-cli` about `migrate.table-name` * feat: introduce `migrate.create-schemas` * WIP feat: create multi-tenant database example * fix(postgres): don't fetch `ColumnOrigin` for transparently-prepared statements * feat: progress on axum-multi-tenant example * feat(config): better errors for mislabeled fields * WIP feat: filling out axum-multi-tenant example * feat: multi-tenant example No longer Axum-based because filling out the request routes would have distracted from the purpose of the example. * chore(ci): test multi-tenant example * fixup after merge * fix(ci): enable `sqlx-toml` in CLI build for examples * fix: CI, README for `multi-tenant` * fix: clippy warnings * fix: multi-tenant README * fix: sequential versioning inference for migrations * fix: migration versioning with explicit overrides * fix: only warn on ambiguous crates if the invocation relies on it * fix: remove unused imports * fix: doctest * fix: `sqlx mig add` behavior and tests * fix: restore original type-checking order * fix: deprecation warning in `tests/postgres/macros.rs` * feat: create postgres/multi-database example * fix: examples/postgres/multi-database * fix: cargo fmt * chore: add tests for config `migrate.defaults` * fix: sqlx-cli/tests/add.rs * feat(cli): add `--config` override to all relevant commands * chore: run `sqlx mig add` test with `RUST_BACKTRACE=1` * fix: properly canonicalize config path for `sqlx mig add` test * fix: get `sqlx mig add` test passing * fix(cli): test `migrate.ignored-chars`, fix bugs * feat: create `macros.preferred-crates` example * fix(examples): use workspace `sqlx` * fix: examples * fix(sqlite): unexpected feature flags in `type_checking.rs` * fix: run `cargo fmt` * fix: more example fixes * fix(ci): preferred-crates setup * fix(examples): enable default-features for workspace `sqlx` * fix(examples): issues in `preferred-crates` * chore: adjust error message for missing param type in `query!()` * doc: mention new `sqlx.toml` configuration * chore: add `CHANGELOG` entry Normally I generate these when cutting the release, but I wanted to take time to editorialize this one. * doc: fix new example titles * refactor: make `sqlx-toml` feature non-default, improve errors * refactor: eliminate panics in `Config` read path * chore: remove unused `axum` dependency from new examples * fix(config): restore fallback to default config for macros * chore(config): remove use of `once_cell` (to match `main`)
111 lines
3.3 KiB
Rust
111 lines
3.3 KiB
Rust
use crate::opt::{ConnectOpts, MigrationSourceOpt};
|
|
use crate::{migrate, Config};
|
|
use console::{style, Term};
|
|
use dialoguer::Confirm;
|
|
use sqlx::any::Any;
|
|
use sqlx::migrate::MigrateDatabase;
|
|
use std::{io, mem};
|
|
use tokio::task;
|
|
|
|
pub async fn create(connect_opts: &ConnectOpts) -> anyhow::Result<()> {
|
|
// NOTE: only retry the idempotent action.
|
|
// We're assuming that if this succeeds, then any following operations should also succeed.
|
|
let exists = crate::retry_connect_errors(connect_opts, Any::database_exists).await?;
|
|
|
|
if !exists {
|
|
#[cfg(feature = "_sqlite")]
|
|
sqlx::sqlite::CREATE_DB_WAL.store(
|
|
connect_opts.sqlite_create_db_wal,
|
|
std::sync::atomic::Ordering::Release,
|
|
);
|
|
|
|
Any::create_database(connect_opts.expect_db_url()?).await?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn drop(connect_opts: &ConnectOpts, confirm: bool, force: bool) -> anyhow::Result<()> {
|
|
if confirm && !ask_to_continue_drop(connect_opts.expect_db_url()?.to_owned()).await {
|
|
return Ok(());
|
|
}
|
|
|
|
// NOTE: only retry the idempotent action.
|
|
// We're assuming that if this succeeds, then any following operations should also succeed.
|
|
let exists = crate::retry_connect_errors(connect_opts, Any::database_exists).await?;
|
|
|
|
if exists {
|
|
if force {
|
|
Any::force_drop_database(connect_opts.expect_db_url()?).await?;
|
|
} else {
|
|
Any::drop_database(connect_opts.expect_db_url()?).await?;
|
|
}
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn reset(
|
|
config: &Config,
|
|
migration_source: &MigrationSourceOpt,
|
|
connect_opts: &ConnectOpts,
|
|
confirm: bool,
|
|
force: bool,
|
|
) -> anyhow::Result<()> {
|
|
drop(connect_opts, confirm, force).await?;
|
|
setup(config, migration_source, connect_opts).await
|
|
}
|
|
|
|
pub async fn setup(
|
|
config: &Config,
|
|
migration_source: &MigrationSourceOpt,
|
|
connect_opts: &ConnectOpts,
|
|
) -> anyhow::Result<()> {
|
|
create(connect_opts).await?;
|
|
migrate::run(config, migration_source, connect_opts, false, false, None).await
|
|
}
|
|
|
|
async fn ask_to_continue_drop(db_url: String) -> bool {
|
|
// If the setup operation is cancelled while we are waiting for the user to decide whether
|
|
// or not to drop the database, this will restore the terminal's cursor to its normal state.
|
|
struct RestoreCursorGuard {
|
|
disarmed: bool,
|
|
}
|
|
|
|
impl Drop for RestoreCursorGuard {
|
|
fn drop(&mut self) {
|
|
if !self.disarmed {
|
|
Term::stderr().show_cursor().unwrap()
|
|
}
|
|
}
|
|
}
|
|
|
|
let mut guard = RestoreCursorGuard { disarmed: false };
|
|
|
|
let decision_result = task::spawn_blocking(move || {
|
|
Confirm::new()
|
|
.with_prompt(format!("Drop database at {}?", style(&db_url).cyan()))
|
|
.wait_for_newline(true)
|
|
.default(false)
|
|
.show_default(true)
|
|
.interact()
|
|
})
|
|
.await
|
|
.expect("Confirm thread panicked");
|
|
match decision_result {
|
|
Ok(decision) => {
|
|
guard.disarmed = true;
|
|
decision
|
|
}
|
|
Err(dialoguer::Error::IO(err)) if err.kind() == io::ErrorKind::Interrupted => {
|
|
// Sometimes CTRL + C causes this error to be returned
|
|
mem::drop(guard);
|
|
false
|
|
}
|
|
Err(err) => {
|
|
mem::drop(guard);
|
|
panic!("Confirm dialog failed with {err}")
|
|
}
|
|
}
|
|
}
|