Skip to content

Commit

Permalink
chore: replace dotenv with dotenvy (#2003)
Browse files Browse the repository at this point in the history
* chore: replace `dotenv` with `dotenvy`

The former appears to be unmaintained and the latter is a drop-in replacement.

* chore: fix all warnings
  • Loading branch information
abonander authored Jul 28, 2022
1 parent 05d64fb commit a2eceec
Show file tree
Hide file tree
Showing 32 changed files with 70 additions and 62 deletions.
27 changes: 15 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ futures = "0.3.19"
env_logger = "0.9.0"
async-std = { version = "1.10.0", features = ["attributes"] }
tokio = { version = "1.15.0", features = ["full"] }
dotenv = "0.15.0"
dotenvy = "0.15.0"
trybuild = "1.0.53"
sqlx-rt = { path = "./sqlx-rt" }
sqlx-test = { path = "./sqlx-test" }
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ Differences from `query()`:
queries against; the database does not have to contain any data but must be the same
kind (MySQL, Postgres, etc.) and have the same schema as the database you will be connecting to at runtime.

For convenience, you can use [a `.env` file][dotenv] to set DATABASE_URL so that you don't have to pass it every time:
For convenience, you can use [a `.env` file][dotenv]<sup>1</sup> to set DATABASE_URL so that you don't have to pass it every time:

```
DATABASE_URL=mysql://localhost/my_database
Expand Down Expand Up @@ -423,6 +423,9 @@ putting the following in your `Cargo.toml` (More information in the
opt-level = 3
```

<sup>1</sup> The `dotenv` crate itself appears abandoned as of [December 2021](https://github.com/dotenv-rs/dotenv/issues/74)
so we now use the `dotenvy` crate instead. The file format is the same.

## Safety

This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% Safe Rust.
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/files/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ edition = "2021"
anyhow = "1.0"
sqlx = { path = "../../../", features = ["postgres", "offline", "runtime-tokio-native-tls"] }
tokio = { version = "1.20.0", features = ["macros"]}
dotenv = "0.15.0"
dotenvy = "0.15.0"
2 changes: 1 addition & 1 deletion examples/postgres/files/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Display for PostWithAuthorQuery {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let pool = PgPool::connect(&dotenv::var("DATABASE_URL")?).await?;
let pool = PgPool::connect(&dotenvy::var("DATABASE_URL")?).await?;

// we can use a tranditional wrapper around the `query!()` macro using files
query_file!("queries/insert_seed_data.sql")
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ workspace = "../../../"

[dependencies]
anyhow = "1.0"
dotenv = "0.15.0"
dotenvy = "0.15.0"
futures = "0.3"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/json/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct Row {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::from_args_safe()?;
let pool = PgPool::connect(&dotenv::var("DATABASE_URL")?).await?;
let pool = PgPool::connect(&dotenvy::var("DATABASE_URL")?).await?;

match args.cmd {
Some(Command::Add) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/mockable-todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ futures = "0.3"
sqlx = { path = "../../../", features = ["postgres", "offline", "runtime-tokio-native-tls"] }
structopt = "0.3"
tokio = { version = "1.20.0", features = ["macros"]}
dotenv = "0.15.0"
dotenvy = "0.15.0"
async-trait = "0.1.41"
mockall = "0.11"
3 changes: 1 addition & 2 deletions examples/postgres/mockable-todos/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use async_trait::async_trait;
use dotenv;
use sqlx::postgres::PgPool;
use std::{env, io::Write, sync::Arc};
use structopt::StructOpt;
Expand All @@ -18,7 +17,7 @@ enum Command {

#[tokio::main]
async fn main() -> anyhow::Result<()> {
dotenv::dotenv().ok();
dotenvy::dotenv().ok();
let args = Args::from_args_safe()?;
let pool = PgPool::connect(&env::var("DATABASE_URL")?).await?;
let todo_repo = PostgresTodoRepo::new(pool);
Expand Down
2 changes: 1 addition & 1 deletion examples/postgres/todos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ futures = "0.3"
sqlx = { path = "../../../", features = ["postgres", "offline", "runtime-tokio-native-tls"] }
structopt = "0.3"
tokio = { version = "1.20.0", features = ["macros"]}
dotenv = "0.15.0"
dotenvy = "0.15.0"
2 changes: 1 addition & 1 deletion sqlx-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ sqlite = ["sqlx/sqlite"]

[dependencies]
criterion = "0.3.3"
dotenv = "0.15.0"
dotenvy = "0.15.0"
once_cell = "1.4"
sqlx = { version = "0.6", path = "../", default-features = false, features = ["macros"] }
sqlx-rt = { version = "0.6", path = "../sqlx-rt", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion sqlx-bench/benches/pg_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn do_bench_acquire(b: &mut Bencher, concurrent: u32, fair: bool) {
.test_before_acquire(false)
.__fair(fair)
.connect(
&dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set to run benchmarks"),
&dotenvy::var("DATABASE_URL").expect("DATABASE_URL must be set to run benchmarks"),
),
)
.expect("failed to open PgPool");
Expand Down
2 changes: 1 addition & 1 deletion sqlx-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ name = "cargo-sqlx"
path = "src/bin/cargo-sqlx.rs"

[dependencies]
dotenv = "0.15.0"
dotenvy = "0.15.0"
tokio = { version = "1.15.0", features = ["macros", "rt", "rt-multi-thread"] }
sqlx = { version = "0.6.0", path = "..", default-features = false, features = [
"migrate",
Expand Down
3 changes: 1 addition & 2 deletions sqlx-cli/src/bin/cargo-sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use clap::Parser;
use console::style;
use dotenv::dotenv;
use sqlx_cli::Opt;
use std::process;

Expand All @@ -14,7 +13,7 @@ enum Cli {

#[tokio::main]
async fn main() {
dotenv().ok();
dotenvy::dotenv().ok();
let Cli::Sqlx(opt) = Cli::parse();

if let Err(error) = sqlx_cli::run(opt).await {
Expand Down
3 changes: 1 addition & 2 deletions sqlx-cli/src/bin/sqlx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use clap::Parser;
use console::style;
use dotenv::dotenv;
use sqlx_cli::Opt;

#[tokio::main]
async fn main() {
dotenv().ok();
dotenvy::dotenv().ok();
// no special handling here
if let Err(error) = sqlx_cli::run(Opt::parse()).await {
println!("{} {}", style("error:").bold().red(), error);
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/protocol/col_meta_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub(crate) struct ColumnData {
// The user type ID of the data type of the column. Depending on the TDS version that is used,
// valid values are 0x0000 or 0x00000000, with the exceptions of data type
// TIMESTAMP (0x0050 or 0x00000050) and alias types (greater than 0x00FF or 0x000000FF).
#[allow(dead_code)]
pub(crate) user_type: u32,

pub(crate) flags: Flags,
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/protocol/done.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub(crate) struct Done {

// The token of the current SQL statement. The token value is provided and controlled by the
// application layer, which utilizes TDS. The TDS layer does not evaluate the value.
#[allow(dead_code)]
cursor_command: u16,

// The count of rows that were affected by the SQL statement. The value of DoneRowCount is
Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/protocol/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::Error;

#[derive(Debug)]
pub(crate) struct Order {
#[allow(dead_code)]
columns: Bytes,
}

Expand Down
1 change: 1 addition & 0 deletions sqlx-core/src/mssql/protocol/return_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::error::Error;

#[derive(Debug)]
pub(crate) struct ReturnStatus {
#[allow(dead_code)]
value: i32,
}

Expand Down
3 changes: 1 addition & 2 deletions sqlx-core/src/sqlite/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ impl MigrateDatabase for Sqlite {
}

// Opening a connection to sqlite creates the database
let _ = SqliteConnectOptions::from_str(url)?
.create_if_missing(true)
let _ = opts
.connect()
.await?
// Ensure WAL mode tempfiles are cleaned up
Expand Down
12 changes: 6 additions & 6 deletions sqlx-core/src/sqlite/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl SqliteConnectOptions {
///
/// SQLx chooses to enable this by default so that foreign keys function as expected,
/// compared to other database flavors.
pub fn foreign_keys(mut self, on: bool) -> Self {
pub fn foreign_keys(self, on: bool) -> Self {
self.pragma("foreign_keys", if on { "ON" } else { "OFF" })
}

Expand Down Expand Up @@ -187,14 +187,14 @@ impl SqliteConnectOptions {
///
/// For consistency, any commands in `sqlx-cli` which create a SQLite database will create it
/// in WAL mode.
pub fn journal_mode(mut self, mode: SqliteJournalMode) -> Self {
pub fn journal_mode(self, mode: SqliteJournalMode) -> Self {
self.pragma("journal_mode", mode.as_str())
}

/// Sets the [locking mode](https://www.sqlite.org/pragma.html#pragma_locking_mode) for the database connection.
///
/// The default locking mode is NORMAL.
pub fn locking_mode(mut self, mode: SqliteLockingMode) -> Self {
pub fn locking_mode(self, mode: SqliteLockingMode) -> Self {
self.pragma("locking_mode", mode.as_str())
}

Expand Down Expand Up @@ -238,7 +238,7 @@ impl SqliteConnectOptions {
///
/// The default synchronous settings is FULL. However, if durability is not a concern,
/// then NORMAL is normally all one needs in WAL mode.
pub fn synchronous(mut self, synchronous: SqliteSynchronous) -> Self {
pub fn synchronous(self, synchronous: SqliteSynchronous) -> Self {
self.pragma("synchronous", synchronous.as_str())
}

Expand All @@ -248,7 +248,7 @@ impl SqliteConnectOptions {
///
/// For existing databases, a change to this value does not take effect unless a
/// [`VACUUM` command](https://www.sqlite.org/lang_vacuum.html) is executed.
pub fn auto_vacuum(mut self, auto_vacuum: SqliteAutoVacuum) -> Self {
pub fn auto_vacuum(self, auto_vacuum: SqliteAutoVacuum) -> Self {
self.pragma("auto_vacuum", auto_vacuum.as_str())
}

Expand All @@ -259,7 +259,7 @@ impl SqliteConnectOptions {
/// For existing databases, a change to this value does not take effect unless a
/// [`VACUUM` command](https://www.sqlite.org/lang_vacuum.html) is executed.
/// However, it cannot be changed in WAL mode.
pub fn page_size(mut self, page_size: u32) -> Self {
pub fn page_size(self, page_size: u32) -> Self {
self.pragma("page_size", page_size.to_string())
}

Expand Down
2 changes: 1 addition & 1 deletion sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ bit-vec = ["sqlx-core/bit-vec"]
json = ["sqlx-core/json", "serde_json"]

[dependencies]
dotenv = { version = "0.15.0", default-features = false }
dotenvy = { version = "0.15.0", default-features = false }
hex = { version = "0.4.3", optional = true }
heck = { version = "0.4", features = ["unicode"] }
either = "1.6.1"
Expand Down
6 changes: 2 additions & 4 deletions sqlx-macros/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::sync::{Arc, Mutex};
use once_cell::sync::Lazy;
use proc_macro2::TokenStream;
use syn::Type;
use url::Url;

pub use input::QueryMacroInput;
use quote::{format_ident, quote};
Expand Down Expand Up @@ -84,14 +83,14 @@ static METADATA: Lazy<Metadata> = Lazy::new(|| {

#[cfg_attr(not(procmacro2_semver_exempt), allow(unused_variables))]
let env_path = if env_path.exists() {
let res = dotenv::from_path(&env_path);
let res = dotenvy::from_path(&env_path);
if let Err(e) = res {
panic!("failed to load environment from {:?}, {}", env_path, e);
}

Some(env_path)
} else {
dotenv::dotenv().ok()
dotenvy::dotenv().ok()
};

// tell the compiler to watch the `.env` for changes, if applicable
Expand Down Expand Up @@ -189,7 +188,6 @@ pub fn expand_input(input: QueryMacroInput) -> crate::Result<TokenStream> {
))]
fn expand_from_db(input: QueryMacroInput, db_url: &str) -> crate::Result<TokenStream> {
use sqlx_core::any::{AnyConnectOptions, AnyConnection};
use std::str::FromStr;

let connect_opts = AnyConnectOptions::from_str(db_url)?;

Expand Down
2 changes: 1 addition & 1 deletion sqlx-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ publish = false
[dependencies]
sqlx = { default-features = false, path = ".." }
env_logger = "0.9.0"
dotenv = "0.15.0"
dotenvy = "0.15.0"
anyhow = "1.0.26"
async-std = { version = "1.8.0", features = [ "attributes" ] }
tokio = { version = "1.0.1", features = [ "full" ] }
Loading

0 comments on commit a2eceec

Please sign in to comment.